简体   繁体   English

清除 JPanel 或 JFrame

[英]Clear JPanel or JFrame

Without using super.paintComponent(g);不使用super.paintComponent(g); can i still clear my Jpanel or Jframe Screen?我还能清除我的 Jpanel 或 Jframe 屏幕吗? I have some shapes drawn on JPanel and i want to clear all the drawing when user presses the right click without Using this method.我在 JPanel 上绘制了一些形状,当用户在不使用此方法的情况下按下右键单击时,我想清除所有绘图。 or i say is there any alternate of super.paintCompenent(g) ;method or method like clrscr();或者我说是否有super.paintCompenent(g)的替代方法或类似clrscr(); In Java.在 Java。

EDIT编辑

  public void mousePressed(MouseEvent e) {
            super.paintComponents(null); //i want to use this method here?? how can i? 
            if(e.isPopupTrigger())
            {
                s=e.getX();
                as=e.getY();
              try {
                    Thread.sleep(10L);
                } catch (InterruptedException ex) {
                    Logger.getLogger(animate.class.getName()).log(Level.SEVERE, null, ex);
                }
        p.repaint();
            }
        }

i am painting the shape like this我正在画这样的形状

public class mypanel extends JPanel {

   @Override
    public void paintComponent(Graphics g)
    {

       super.paintComponent(g);
        Graphics2D g2=(Graphics2D)g ;

        Color[] c = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, 
                 Color.MAGENTA, Color.WHITE, Color.ORANGE, Color.PINK};
    for(int i=0; i<8; ++i){
        g2.setColor(c[i]);
        int start_angle=i*45;
    g2.fillArc(mx-100, my-100, 200, 200, start_angle,45);



    }

mre solution is actually a good idea, but you may have issues if another repaint comes in (because you move the JFrame, because you resize it, because another window comes on top of it and then leavs, etc...) mre 解决方案实际上是一个好主意,但如果出现另一个重绘,你可能会遇到问题(因为你移动了 JFrame,因为你调整了它的大小,因为另一个 window 出现在它上面然后离开,等等......)

Alternatively, you can have something like this to make the change permanent:或者,您可以使用类似这样的方法来使更改永久化:

public class mypanel extends JPanel {

    private boolean draw = true;

    @Override
    public void paintComponent(Graphics g)
    {

        super.paintComponent(g);
        if (draw) {
            Graphics2D g2=(Graphics2D)g ;

            Color[] c = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, 
                     Color.MAGENTA, Color.WHITE, Color.ORANGE, Color.PINK};
            for(int i=0; i<8; ++i){
                g2.setColor(c[i]);
            int start_angle=i*45;
            g2.fillArc(mx-100, my-100, 200, 200, start_angle,45);
        }
    }

    public void setDraw(boolean draw) {
        this.draw = draw;
        repaint();
    }
}

And then you can just draw or not in your JPanel simlpy by calling mypanel.setDraw(boolean)然后你可以通过调用 mypanel.setDraw(boolean) 在你的 JPanel simlpy 中绘制或不绘制

Perhaps Graphics#clearRect is what you're looking for?也许Graphics#clearRect是您要找的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM