4

在这种情况下,第一个坐标应该是 0,0 而不是 8,30。我在做什么错(我正在使用 NetBeans)

import java.awt.Color;
import java.awt.Graphics;
public class TEST extends javax.swing.JFrame {

@Override
public void paint(Graphics g){
    super.paint(g);
    g.setColor(Color.blue);
    g.drawRect(8, 30, 200, 200);
    repaint();
}}
4

2 回答 2

4

在框架中添加一个JPanel并在其中绘画。框架的坐标包括装饰(标题栏、边框等)。它看起来像这样:

public class Test extends JFrame {
    public static void main(String[] args) {
        new Test();
    }
    private Test() {
        add(new MyPanel());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 600);
        setVisible(true);
    }
    private class MyPanel extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.drawRect(8, 30, 200, 200);
        }
    }
}

另外,不要打电话repaint();paint();这将导致无限循环并冻结整个程序。

于 2013-01-06T15:24:51.313 回答
3

问题是您的paint(..)方法没有考虑到JFrame Insetsby 调用getInsetswhich as docs state

如果已在此组件上设置了边框,则返回边框的插图。

此代码工作正常:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {
        createAndShowGui();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.setColor(Color.blue);
                g.drawRect(0 + getInsets().left, 0 + getInsets().top, 200, 200);
            }
        };
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}

但这不是最佳做法。

而是添加JPanelJFrame覆盖不要忘记调用作为被覆盖方法中paintComponent(Graphics g)的第一次调用,而不是在那里绘制(不要忘记覆盖并返回正确的 s,以便适合其绘图/图形内容)这个问题将不再存在,因为添加在contentPane上的正确坐标如下:JPanelsuper.paintComponent(g)getPreferredSize()DimensionJPanelJPanel

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {
        createAndShowGui();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
                g2d.setColor(Color.blue);
                g2d.drawRect(0, 0, 200, 200);
            }

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

        frame.add(panel);

        frame.pack();
        frame.setVisible(true);
    }
}

以上包括Graphics2D和RenderingHints 即抗锯齿。只是为了一些更好看的图纸:)

于 2013-01-06T16:09:25.883 回答