簡體   English   中英

當我想要重繪我的畫時,為什么重繪()不起作用?

[英]Why repaint() doesn't work when i want repaint my painting?

這是我的第一個java動畫,我希望我的標志開始使用repaint(),但該方法什么都不做,我不知道為什么。 這是關於moveFlag方法,它應該讓旗幟在運動中讓人聯想到風中飄揚的旗幟。 應用程序只顯示標志但不移動它。

public class Testing extends JPanel implements ActionListener {
    private int x,y, xinc, yinc;

    public void paintComponent(Graphics g){
        xinc = 10;
        yinc = 10;
        x = 101;
        y = 151;

        Graphics2D gimg = (Graphics2D) g;

        Rectangle2D rect = new Rectangle2D.Double(50,50,10,300);

        CubicCurve2D cub1 = new 
        CubicCurve2D.Double(60,50,x,10,y,100,200,50);
        gimg.draw(cub1);

        CubicCurve2D cub2 = new 
        CubicCurve2D.Double(60,100,x,60,y,150,200,100);
        gimg.draw(cub2);

        CubicCurve2D cub3 = new 
        CubicCurve2D.Double(60,150,x,110,y,200,200,150);
        gimg.draw(cub3);

        Line2D l1 = new Line2D.Double(200,50,200,150);
        gimg.draw(l1);

        GeneralPath gp1 = new GeneralPath();    
        GeneralPath gp2 = new GeneralPath();

        gp1.moveTo(60,50);
        gp1.curveTo(x,10,y,100,200,50);
        gp1.lineTo(200,100);
        gp1.curveTo(y,150,x,60,60,100);
        gp1.lineTo(60,50);

        gimg.setColor(Color.WHITE);

        gimg.fill(gp1);

        gimg.setColor(Color.GRAY);

        gimg.fill(rect);    

        gp2.moveTo(60,100);   
        gp2.curveTo(x,60,y,150,200,100);
        gp2.lineTo(200,150);
        gp2.curveTo(y,200,x,110,60,150);
        gp2.lineTo(60,100);

        gimg.setColor(Color.RED);

        gimg.fill(gp2);
    }    
    public void moveFlag(){  //waving animation    
        Timer timer = new Timer(20, this);
        timer.start();

        x = x + xinc;
        y = y + yinc;

        if(x<=60||(x+xinc)>=130)
            xinc*=-1;

        if(y<=50||(y+yinc)>=230)
            yinc*=-1;

        //revalidate();
        repaint();
    }       
    @Override
    public void actionPerformed(ActionEvent e) {

    }  
    public static void main(String [] args){
        JFrame frame = new JFrame();

        Testing test = new Testing();
        frame.setContentPane(test);
        frame.setSize(500,500);
        frame.setVisible(true);

        test.moveFlag();    
    }
}

你有正確的想法,但有些代碼是在錯誤的地方。 我會在評論中解釋。


    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;

    public class Testing extends JPanel implements ActionListener {
       private int x, y, xinc, yinc;



       public void paintComponent(Graphics g) {
          // added this statement.
          super.paintComponent(g);
          Graphics2D gimg = (Graphics2D) g;

          Rectangle2D rect = new Rectangle2D.Double(50, 50, 10, 300);

          CubicCurve2D cub1 =
                new CubicCurve2D.Double(60, 50, x, 10, y, 100, 200, 50);
          gimg.draw(cub1);

          CubicCurve2D cub2 =
                new CubicCurve2D.Double(60, 100, x, 60, y, 150, 200, 100);
          gimg.draw(cub2);

          CubicCurve2D cub3 =
                new CubicCurve2D.Double(60, 150, x, 110, y, 200, 200, 150);
          gimg.draw(cub3);

          Line2D l1 = new Line2D.Double(200, 50, 200, 150);
          gimg.draw(l1);

          GeneralPath gp1 = new GeneralPath();
          GeneralPath gp2 = new GeneralPath();

          gp1.moveTo(60, 50);
          gp1.curveTo(x, 10, y, 100, 200, 50);
          gp1.lineTo(200, 100);
          gp1.curveTo(y, 150, x, 60, 60, 100);
          gp1.lineTo(60, 50);

          gimg.setColor(Color.WHITE);

          gimg.fill(gp1);

          gimg.setColor(Color.GRAY);

          gimg.fill(rect);

          gp2.moveTo(60, 100);
          gp2.curveTo(x, 60, y, 150, 200, 100);
          gp2.lineTo(200, 150);
          gp2.curveTo(y, 200, x, 110, 60, 150);
          gp2.lineTo(60, 100);

          gimg.setColor(Color.RED);

          gimg.fill(gp2);
       }

       public void moveFlag() { // waving animation
          Timer timer = new Timer(20, this);
          timer.start();
          // moved the next four statements from paintComponents.  They
          // are initializations.  In paintComponent you kept resetting them.
          xinc = 10;
          yinc = 10;
          x = 101;
          y = 151;

          repaint();
       }

       @Override
       public void actionPerformed(ActionEvent e) {
         //moved all of these from moveFlag to here. This is called by
         //the timer event to update the x and y coordinates.
          x = x + xinc;
          y = y + yinc;

          if (x <= 60 || (x + xinc) >= 130)
             xinc *= -1;

          if (y <= 50 || (y + yinc) >= 230)
             yinc *= -1;

          // revalidate();
          repaint();
       }

      public static void main(String[] args) {
          JFrame frame = new JFrame();

          Testing test = new Testing();
          frame.setContentPane(test);
          frame.setSize(500, 500);
          frame.setVisible(true);

          test.moveFlag();
       }
    }

我必須添加的唯一代碼是super.paintComponent(g)調用。 這是必要的,因為您要覆蓋paintComponent(),您需要確保在執行圖形之前調用父方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM