【问题标题】:Drawing an object using getGraphics() without extending JFrame使用 getGraphics() 绘制对象而不扩展 JFrame
【发布时间】:2013-04-05 21:14:21
【问题描述】:

如何绘制没有类的对象(扩展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);
    }
}

【问题讨论】:

    标签: java swing graphics2d paintcomponent


    【解决方案1】:

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

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

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

    查看this link 以进一步阅读。

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

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

    【讨论】:

    • 非常感谢!帮助了我
    【解决方案2】:

    你必须重写 JPanel 类中的paintComponent 方法。所以你应该创建一个扩展 JPanel 的类并覆盖子类中的 paintComponent 方法

    【讨论】:

    • 但是我需要在按钮点击时添加一个形状
    • 您可以创建扩展JPanel的静态内部类,然后单击按钮创建此静态类实例,然后您可以调用instanceName.repaint()方法
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      相关资源
      最近更新 更多