JPanel设计问题



我有一个在mouseClick事件上绘制椭圆形的Form。这对我来说很好。圆圈是画的。但是,当我最小化窗体并再次最大化窗体时,面板会被刷新,圆圈会被删除(即面板为空)。

代码为:我有一个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);
    }

在这种情况下,不仅覆盖JPanel的paintComponent方法很重要,而且还需要存储要绘制的圆的信息。在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;
        }
    }
}

您不必在JPanelpaintComponent方法之外显式调用draw函数。

您应该扩展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);
    }
}

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

所有的图纸都必须用面板的油漆方法完成。因此,您必须在面板中覆盖此方法,并将绘图代码放在那里

相关内容

  • 没有找到相关文章

最新更新