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

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


        ubicCurve2D 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) {


    }  



隔江千里
浏览 100回答 1
1回答

蝴蝶不菲

你的想法是对的,但有些代码放错了地方。我会在评论中解释。&nbsp; &nbsp; import java.awt.*;&nbsp; &nbsp; import java.awt.event.*;&nbsp; &nbsp; import java.awt.geom.*;&nbsp; &nbsp; import javax.swing.*;&nbsp; &nbsp; public class Testing extends JPanel implements ActionListener {&nbsp; &nbsp; &nbsp; &nbsp;private int x, y, xinc, yinc;&nbsp; &nbsp; &nbsp; &nbsp;public void paintComponent(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // added this statement.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Graphics2D gimg = (Graphics2D) g;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle2D rect = new Rectangle2D.Double(50, 50, 10, 300);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CubicCurve2D cub1 =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CubicCurve2D.Double(60, 50, x, 10, y, 100, 200, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.draw(cub1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CubicCurve2D cub2 =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CubicCurve2D.Double(60, 100, x, 60, y, 150, 200, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.draw(cub2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CubicCurve2D cub3 =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CubicCurve2D.Double(60, 150, x, 110, y, 200, 200, 150);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.draw(cub3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Line2D l1 = new Line2D.Double(200, 50, 200, 150);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.draw(l1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GeneralPath gp1 = new GeneralPath();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GeneralPath gp2 = new GeneralPath();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp1.moveTo(60, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp1.curveTo(x, 10, y, 100, 200, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp1.lineTo(200, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp1.curveTo(y, 150, x, 60, 60, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp1.lineTo(60, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.setColor(Color.WHITE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.fill(gp1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.setColor(Color.GRAY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.fill(rect);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp2.moveTo(60, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp2.curveTo(x, 60, y, 150, 200, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp2.lineTo(200, 150);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp2.curveTo(y, 200, x, 110, 60, 150);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp2.lineTo(60, 100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.setColor(Color.RED);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gimg.fill(gp2);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;public void moveFlag() { // waving animation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Timer timer = new Timer(20, this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // moved the next four statements from paintComponents.&nbsp; They&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // are initializations.&nbsp; In paintComponent you kept resetting them.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xinc = 10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yinc = 10;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = 101;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = 151;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp;public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//moved all of these from moveFlag to here. This is called by&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//the timer event to update the x and y coordinates.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = x + xinc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = y + yinc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x <= 60 || (x + xinc) >= 130)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xinc *= -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (y <= 50 || (y + yinc) >= 230)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;yinc *= -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // revalidate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Testing test = new Testing();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setContentPane(test);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(500, 500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.moveFlag();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }我必须添加的唯一一段代码是 super.paintComponent(g) 调用。这是必要的,因为您要覆盖 paintComponent(),您需要确保在绘制图形之前调用父方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java