java - JPanel 设计问题

标签 java swing graphics jpanel paintcomponent

我有一个表单,我在 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);
    }

最佳答案

在这种情况下,不仅重写 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;
        }
    }

}

关于java - JPanel 设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12074497/

相关文章:

java - 用Java测试Hadoop HDFS

java - 将堆栈跟踪打印到文本区域

java - 我得到这个窗口没有任何内容为什么?

javascript - 为什么这个简单的平移矩阵不起作用

c# - 如何沿轴旋转字符串列表?

java - 使用放心的 XML 映射

java - 如何在jsp中将数组打印到输入字段?

Java Swing 看起来像 Windows

java - 两个不同的 JFrame 之间的通信?

c - 如何在 C 控制台窗口中制作图形矩形?