简体   繁体   English

组件Paint方法无法绘制到JPanel的中间吗?

[英]Component Paint method not painting to middle of JPanel?

Alright, so I have a JPanel with the paint method below, and it works just fine at first, but when the JPanel is resized(is in JFrame) is doesn't paint it to the center of the Frame. 好了,所以我有一个JPanel带有下面的paint方法,起初效果很好,但是当调整JPanel的大小(在JFrame中)时,不会将其绘制到Frame的中心。

 @Override
    protected void paintComponent(Graphics g) {
        Graphics2D graphics = (Graphics2D) g;
        Dimension dimension = frame.getSize();
        Insets insets = getInsets();
        int w = (int) ((dimension.getWidth() - insets.left - insets.right) / 2);
        int h = (int) ((dimension.getHeight() - insets.top - insets.bottom) / 2);
        graphics.translate(w, h);
        graphics.drawString("Origin", 0, 0);
        double y = 0;
        for (double x = -25; x <= 25; x += .01) {
            y = -Math.pow(x, 2);
            int gx = (int) x;
            int gy = (int) y;
            System.out.println("Parabola Coordinate: " + x + ", " + y);
            g.drawRect(gx, gy, 0, 0);
        }
    }

Change your paintComponent to look more like 更改您的paintComponent使其看起来更像

@Override
protected void paintComponent(Graphics g) {
    // Create a copy of the graphics context...
    Graphics2D graphics = (Graphics2D) g.create();
    // Don't rely on the frame, rely on your own components size...
    //Dimension dimension = frame.getSize();
    Insets insets = getInsets();
    int w = (int) ((getWidth() - insets.left - insets.right) / 2);
    int h = (int) ((getHeight() - insets.top - insets.bottom) / 2);
    graphics.translate(w, h);
    graphics.drawString("Origin", 0, 0);
    double y = 0;
    for (double x = -25; x <= 25; x += .01) {
        y = -Math.pow(x, 2);
        int gx = (int) x;
        int gy = (int) y;
        System.out.println("Parabola Coordinate: " + x + ", " + y);
        // You were using the "un-translated" reference, don't know if that was deliberate
        graphics.drawRect(gx, gy, 0, 0);
    }
    // Dispose of the copy and safe resources...
    graphics.dispose();
}

Make sure you're also using a suitable layout manager! 确保您还使用了合适的布局管理器!

在此处输入图片说明在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BadPaint21 {

    public static void main(String[] args) {
        new BadPaint21();
    }

    public BadPaint21() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D graphics = (Graphics2D) g.create();
//            Dimension dimension = frame.getSize();
            Insets insets = getInsets();
            int w = (int) ((getWidth() - insets.left - insets.right) / 2);
            int h = (int) ((getHeight() - insets.top - insets.bottom) / 2);
            graphics.translate(w, h);
            graphics.drawString("Origin", 0, 0);
            double y = 0;
            for (double x = -25; x <= 25; x += .01) {
                y = -Math.pow(x, 2);
                int gx = (int) x;
                int gy = (int) y;
                System.out.println("Parabola Coordinate: " + x + ", " + y);
                graphics.drawRect(gx, gy, 0, 0);
            }
            graphics.dispose();
        }
    }
}

The size of the painting area should be based on the size of the panel not the frame: 绘画区域的大小应基于面板的大小而不是框架的大小:

//Dimension dimension = frame.getSize();
Dimension dimension = getSize();

Also if you want to center a painting then you need to know the size of the painting. 同样,如果您想使绘画居中,则需要知道绘画的大小。 So for example the height will range from -625 to 0. You need to take into account the maximum size of 625 for the height so to center on the y axis the code should be: 因此,例如,高度范围为-625到0。您需要考虑高度的最大尺寸625,因此以y轴为中心的代码应为:

int h = (int) ((dimension.getHeight() - insets.top - insets.bottom - 625) / 2);

Also, because the calculated values are all negative, you need to do an additional translation to make sure the coordinates are all positive. 另外,由于计算的值均为负,因此您需要执行其他转换以确保坐标均为正。 So the translate would be: 所以翻译将是:

graphics.translate(w, h + 625);

The same basic logic will apply for the horizontal translation as well. 同样的基本逻辑也适用于水平平移。

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

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