简体   繁体   中英

How to keep aspect ratio in JPanel

I am kind of stuck at maintaining aspect ratio on my JPanel .

I have default generated JFrame from Netbeans with Border layout. Whole frame is filled with my JPanel (from my custom class GraphicsPanel.java extending JPanel ).

GraphicsPanel class have only one over ridden method for drawing graphics with some basic constructor. I have one rectangle and polygon on my 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). 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 ). But no luck and I still have no clue how to do it.

There's my code from my JPanel class. 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.

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.

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.

Well you can easily create a BufferedImage at some reasonable size, in the proper aspect ratio, and draw your shapes onto the 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. The StretchIcon class will scale the Icon proportionally within the space available to the label:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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