12

如何绘制没有类(扩展JFrame)的对象?我找到getGraphics了方法,但它没有绘制对象。

import javax.swing.*;
import java.awt.*;
public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(600, 400);

        JPanel panel = new JPanel();
        frame.add(panel);

        Graphics g = panel.getGraphics();
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 100, 100);
    }
}
4

3 回答 3

26

如果要更改组件的绘制方式(添加矩形),则需要paintComponent()在该组件中重新定义。在您的代码中,您使用的是 getGraphics().

您不应该调用getGraphics()组件。您(对Graphics返回的)所做的任何绘制都将是临时的,并且在下次 Swing 确定需要重新绘制组件时将丢失。

相反,您应该覆盖paintComponent(Graphics)JComponentJPanel)方法,并使用Graphics作为参数接收的对象在此方法中进行绘制。

检查此链接以进一步阅读。

以下是您的代码,已更正。

import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(600, 400);

        JPanel panel = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.fillRect(0, 0, 100, 100);
            }
        };
        frame.add(panel);

        // Graphics g = panel.getGraphics();
        // g.setColor(Color.BLUE);
        // g.fillRect(0, 0, 100, 100);

        frame.validate(); // because you added panel after setVisible was called
        frame.repaint(); // because you added panel after setVisible was called
    }
}

另一个版本,做同样的事情,但你可能更清楚:

import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(600, 400);

        JPanel panel = new MyRectangleJPanel(); // changed this line
        frame.add(panel);

        // Graphics g = panel.getGraphics();
        // g.setColor(Color.BLUE);
        // g.fillRect(0, 0, 100, 100);

        frame.validate(); // because you added panel after setVisible was called
        frame.repaint(); // because you added panel after setVisible was called
    }
}

/* A JPanel that overrides the paintComponent() method and draws a rectangle */
class MyRectangleJPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 100, 100);
    }
}
于 2013-04-13T18:11:53.117 回答
4

您必须覆盖 JPanel 类中的paintComponent 方法。因此,您应该创建一个扩展 JPanel 并覆盖子类中的paintComponent 方法的类

于 2013-04-13T10:14:27.313 回答
2

java.awt.image.BufferedImage


为什么不只使用 的实例java.awt.image.BufferedImage?例如

BufferedImage output = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = output.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, output.getWidth(), output.getHeight());
g2.setColor(Color.BLUE);
g2.fillRect(0, 0, 100, 100);

JOptionPane.showMessageDialog(null, new ImageIcon(output));
于 2015-02-01T09:36:58.260 回答