java - 组件 Paint 方法未绘制到 JPanel 的中间?

标签 java graphics resize drawing paint

好吧,我有一个带有下面的绘制方法的 JPanel,一开始它工作得很好,但是当调整 JPanel 的大小(在 JFrame 中)时,它不会将其绘制到框架的中心。

 @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);
        }
    }

最佳答案

更改您的 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();
}

确保您还使用合适的布局管理器!

enter image description here enter image description here

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();
        }
    }
}

关于java - 组件 Paint 方法未绘制到 JPanel 的中间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16351020/

相关文章:

java - Android:在不损失质量的情况下调整位图大小

Java返回语句困惑

java - 值必须存储在 EMF 模型中,但不应传播到 xml 文件

java - 为什么Java的Method类不是泛型的?

graphics - 帧缓冲区和交换链到底是什么?

javascript - 有没有办法改变 CSS 调整 Angular 的位置?

java - if 和 else 中的 Toast 在 else 条件为 true 时执行。代码首先给出 "signup successful"然后给出 "user already exists"

java - 使用 swing 在 Canvas 上绘制椭圆形

android - 跟随手指移动可绘制对象

Javascript - 如何检测元素宽度变化?