4

mouseClickイベントで楕円形を描いているフォームがあります。これは私にとってはうまくいきます。円が描かれています。しかし、フォームを最小化して再度最大化すると、パネルが更新され、円が削除されます (つまり、パネルは空白のままになります)。

コードは次のとおりです。jPanel1という名前のJpanelがあるJFrameがあります。このパネルには円が描かれています。

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

paintComponentのメソッドの外で描画関数を明示的に呼び出す必要はありませんJPanel

代わりに、メソッド内にコードを拡張JPanelして配置する必要があります。drawCirclepaintComponent

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

}

Component を再描画する必要がある場合 (つまり、最小化されたウィンドウを最大化した後)、 Swing は自動的にpaintComponentメソッドを呼び出します。

于 2012-08-22T13:45:39.780 に答える
2

すべての図面は、パネルのペイント方法で行う必要があります。したがって、パネルでこのメソッドをオーバーライドし、そこに描画コードを配置する必要があります

于 2012-08-22T13:45:02.197 に答える