4

我有窗体,我在 mouseClick 事件上绘制椭圆形。这对我来说很好。圆圈被绘制。但是当我最小化表单并再次最大化它时,面板被刷新并且圆圈被删除(即面板被留空)。

代码是:我有一个 JFrame,上面有一个名为 jPanel1 的 Jpanel,在这个面板上绘制了圆圈。

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
        count += 1;
        if (count <= clients) {
            drawCircle(evt.getX() - (radius / 2), evt.getY() - (radius / 2));
        }
    }

    public void drawCircle(int x, int y) {
        Graphics g = jPanel1.getGraphics();
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g.setColor(Color.BLACK);
        g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }
4

3 回答 3

7

在这种情况下,不仅为paintComponentJPanel 覆盖您的方法很重要,而且您还需要存储有关要绘制的圆圈的信息。paintComponent在通话期间,您可以使用存储的信息在屏幕上绘制所有圆圈。

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;



public class TempProject extends JPanel{
    /** Stores info about circles  */
    public ArrayList<CircleInfo> circles = new ArrayList<CircleInfo>();

    /** fields that were in example code */
    public int count = 0;
    public final int radius = 20;
    public final int clients = 20;

    public TempProject(){

        addMouseListener(new MouseAdapter(){

            @Override
            public void mouseClicked(MouseEvent evt) {
                count += 1;
                if (count <= clients) {
                        // Store info about the circle to draw
                    circles.add(new CircleInfo(evt.getX() - (radius / 2), evt.getY() - (radius / 2), radius));
                        // Tell swing to repaint asap
                    repaint();
                }
            }});
    }

    @Override
    public void paintComponent(Graphics g ) {
            super.paintComponent(g);

            //Iterates through saved circles and paints them
        for(CircleInfo circle : circles){
            g.drawOval(circle.x - circle.radius, circle.y - circle.radius, 2 * circle.radius, 2 * circle.radius);
            g.setColor(Color.BLACK);
            g.fillOval(circle.x - circle.radius, circle.y - circle.radius, 2 * circle.radius, 2 * circle.radius);
        }
    }

    public static void main(String args[])    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setContentPane(new TempProject());  
                frame.setPreferredSize(new Dimension(400, 300));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    /** Simple class for storing Circle info */
    public static class CircleInfo{
        int x = 0;
        int y = 0;
        int radius = 0;

        public CircleInfo(int x, int y, int radius){
            this.x = x; this.y = y; this.radius = radius;
        }
    }

}
于 2012-08-22T14:10:00.423 回答
3

您不必paintComponentJPanel.

您应该改为扩展JPanel并将drawCircle代码放入paintComponent方法中:

public class DrawCircleClass extends JPanel
{
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g.setColor(Color.BLACK);
        g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }

}

paintComponent当需要重绘组件时(例如,在最大化最小化窗口之后),Swing 将自动调用方法。

于 2012-08-22T13:45:39.780 回答
2

所有图纸必须以面板的油漆方法完成。所以你必须在面板中重写这个方法并将绘图代码放在那里

于 2012-08-22T13:45:02.197 回答