为什么当我想重绘我的画时 repaint() 不起作用?

彼得雷克17

这是我的第一个 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();    
    }
}
WJS

你的想法是对的,但有些代码在错误的地方。我会在评论中解释。


    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(),您需要确保在绘制图形之前调用父方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Repaint()不起作用

来自分类Dev

为什么 draw() 方法中的 repaint() 不起作用(不调用paint())?

来自分类Dev

Java repaint()在调用中不起作用

来自分类Dev

Qt事件或repaint()函数不起作用

来自分类Dev

Java repaint()在调用中不起作用

来自分类Dev

Swing repaint()在循环或线程中不起作用

来自分类Dev

从不同的类调用时,Java repaint() 不起作用

来自分类Dev

当我有“体积”时,Docker Compose不起作用,否则它将起作用。为什么?

来自分类Dev

当我想使用bufexplorer或vim-buffergator插件时,为什么vim的<Leader>键不起作用?

来自分类Dev

为什么当我想使用哈希键和连接函数替换文本时它不起作用?

来自分类Dev

为什么当我放置偶数个元素时我的程序不起作用

来自分类Dev

为什么我的301重定向不起作用?

来自分类Dev

为什么我的printf不起作用?

来自分类Dev

为什么我的列表不起作用?

来自分类Dev

为什么我的for循环不起作用?

来自分类Dev

为什么我的代码不起作用?

来自分类Dev

为什么getchar()对我不起作用?

来自分类Dev

为什么我的排序不起作用?

来自分类Dev

为什么我的递归不起作用?

来自分类Dev

为什么我的线程不起作用?

来自分类Dev

为什么我的ItemListener不起作用?

来自分类Dev

为什么我的OpenGL不起作用?

来自分类Dev

为什么我的if(-f)不起作用?

来自分类Dev

为什么我的测试不起作用?

来自分类Dev

为什么我的阵列不起作用?

来自分类Dev

为什么我的lambda不起作用?

来自分类Dev

为什么我的setOnKeyListener()不起作用?

来自分类Dev

为什么我的菜单不起作用?

来自分类Dev

为什么我的TTS不起作用?

Related 相关文章

热门标签

归档