简体   繁体   English

如何在JPanel中保持长宽比

[英]How to keep aspect ratio in JPanel

I am kind of stuck at maintaining aspect ratio on my JPanel . 我有点坚持在JPanel上保持宽高比。

I have default generated JFrame from Netbeans with Border layout. 我已经从Netbeans使用边框布局默认生成了JFrame Whole frame is filled with my JPanel (from my custom class GraphicsPanel.java extending JPanel ). 整个框架充满了我的JPanel (来自我的自定义类GraphicsPanel.java,扩展了JPanel )。

GraphicsPanel class have only one over ridden method for drawing graphics with some basic constructor. GraphicsPanel类只有一种覆盖方法,可以使用一些基本构造函数来绘制图形。 I have one rectangle and polygon on my JPanel . 我的JPanel上有一个矩形和多边形。 What I'm trying to achieve is when I re-size the frame I want to keep my JPanel on aspect ratio 4:3 (or something similar to it). 我想要实现的是,当我调整框架的大小时,我希望将JPanel的纵横比保持在4:3(或类似的比例)。 When the frame is too big for aspect ratio it will fill the frame back ground with some default color. 当框架对于宽高比而言太大时,它将使用某些默认颜色填充框架背景。

I've read some topics about aspect ratio (for example this ). 我已经阅读了一些有关宽高比的主题(例如this )。 But no luck and I still have no clue how to do it. 但是没有运气,我仍然不知道如何去做。

There's my code from my JPanel class. 这是我的JPanel类中的代码。 I'm beginner and I'm trying to learn how to work with Java graphics (draw, fill, resize etc) so please go easy on me. 我是新手,我想学习如何使用Java图形(绘制,填充,调整大小等),所以请对我轻松一点。

import java.awt.Color;
import java.awt.Graphics;
public class GraphicsPanel extends javax.swing.JPanel {

private int x;
private int z;
private int y;

public GraphicsPanel() { //constructor
    initComponents();
    x = getHeight() / 2;
    z = getWidth() / 2;
    y = getHeight() - 1;
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

        x = getHeight() / 2;
        z = getWidth() / 2;
        y = getHeight() - 1;

    setBackground(new Color(255, 255, 255));

    g.setColor(Color.red);
    g.fillRect(0, x, getWidth() - 1, y);

    g.setColor(Color.blue);
    int[] poleX = {0, 0, z};
    int[] poleY = {0, y, x};
    g.fillPolygon(poleX, poleY, 3);
  }
}

框架

I think that what you want is to paint your image (which looks a lot like the Czech flag!) in your panel, but not filling all of the space. 我认为您想要的是在面板中绘制图像(看起来很像捷克国旗!),但不填充所有空间。 For example, if the frame is very wide, the image will not fill the whole width. 例如,如果边框非常宽,则图像将不会填满整个宽度。 If I understood correctly, this isn't so hard. 如果我理解正确,这并不难。

In you code, you get the width and height of the panel. 在代码中,您将获得面板的宽度和高度。 When you've done that, do some arithmetic. 完成后,请进行一些算术运算。 If the aspect ratio is "too wide" then your image will fill the height, otherwise it will fill the width. 如果纵横比“太宽”,则图像将填充高度,否则将填充宽度。 Now you know one dimension, so you can calculate the other, and you only draw the image big enough to fit inside those dimensions. 现在您知道了一个尺寸,因此您可以计算另一个尺寸,并且只绘制足够大的图像以适合这些尺寸。

Edit : example calculations... 编辑 :示例计算...

if panel width / panel height > 4 / 3
    // too wide
    // use panel height as image height
    // calculate image width from image height
else
    // use panel width as image width
    // calculate image height from image width

The size and shape of a panel is hard (impossible?) to fix. 面板的尺寸和形状很难固定(不可能?)。 You must add the panel to a container (JFrame in your example, but could be anything), and the container will use a layout manager to set the panel size. 您必须将面板添加到容器中(示例中为JFrame,但可以是任何东西),容器将使用布局管理器来设置面板大小。

I need to maintain image that I'm drawing (but I guess it will be easier to maintain component's ratio). 我需要维护要绘制的图像(但是我想维护组件的比例会更容易)。 Because my image is not classic image (png, jpg) but a few shapes from draw/fill that looks like an image. 因为我的图像不是经典图像(png,jpg),而是绘制/填充中的一些形状看起来像图像。

Well you can easily create a BufferedImage at some reasonable size, in the proper aspect ratio, and draw your shapes onto the BufferedImage 好了,您可以轻松地以适当的宽高比以合理的大小创建BufferedImage ,并将形状绘制到BufferedImage上

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();

//  paint image background

g2d.setColor( ... );
g2d.fillRect(0, 0, width, height);

//  draw shapes

g2d.fillOval(....);

The you can use Darryls Stretch Icon to display the image in a JLabel. 您可以使用Darryls Stretch Icon在JLabel中显示图像。 The StretchIcon class will scale the Icon proportionally within the space available to the label: StretchIcon类将在标签可用的空间内按比例缩放Icon:

JLabel label = new JLabel( new StretchIcon(image) );
frame.add(label);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM