无法使用repaint()?

发布于 2024-10-10 22:32:58 字数 2065 浏览 2 评论 0原文

//I want to paint a ball in a animation
//I can't seem to find a way to repaint the ball
// Any help or tips on how to use repaint here?
//

      // Ball class
     import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.util.*;
        public class Ball implements Runnable {
        protected Point loc;
        protected int dx;
        protected int dy;
        protected Color color;
        protected boolean flag;
        private Graphics gra;
        public Ball(Point loc,int dx,int dy,Graphics st)
        {
            this.loc=loc;
            this.dx=1;
            this.dy=1;
            color=Color.blue;
            this.gra=st;
            flag=false;
        }
        public void paint(Graphics g)
        {
            g.fillOval((int)this.loc.getX(),(int)this.loc.getY(),20,20);
        }
        public void move()
        {
            this.loc.translate(this.dx,this.dy);
        }

        @Override
        public void run() {
            while(flag==false)
            {
                this.paint(gra);
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                move();
            }
        }

        }


//Myframe class
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class myframe extends JFrame {
    private Ball b;
    public myframe()
    {
        super("My Frame");
        setSize(800,600);
    }
    public void run()
    {   
        b=new Ball(new Point(100,100),10,10,getGraphics());
        b.run();
    }
}

//Main class
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame
{

    /**
     * @param args
     */
    public static void main(String[] args) {
        myframe jwin = new myframe();
        jwin.setSize(600, 600);
        jwin.setVisible(true);
        jwin.run();
    }
}
//I want to paint a ball in a animation
//I can't seem to find a way to repaint the ball
// Any help or tips on how to use repaint here?
//

      // Ball class
     import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.util.*;
        public class Ball implements Runnable {
        protected Point loc;
        protected int dx;
        protected int dy;
        protected Color color;
        protected boolean flag;
        private Graphics gra;
        public Ball(Point loc,int dx,int dy,Graphics st)
        {
            this.loc=loc;
            this.dx=1;
            this.dy=1;
            color=Color.blue;
            this.gra=st;
            flag=false;
        }
        public void paint(Graphics g)
        {
            g.fillOval((int)this.loc.getX(),(int)this.loc.getY(),20,20);
        }
        public void move()
        {
            this.loc.translate(this.dx,this.dy);
        }

        @Override
        public void run() {
            while(flag==false)
            {
                this.paint(gra);
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                move();
            }
        }

        }


//Myframe class
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class myframe extends JFrame {
    private Ball b;
    public myframe()
    {
        super("My Frame");
        setSize(800,600);
    }
    public void run()
    {   
        b=new Ball(new Point(100,100),10,10,getGraphics());
        b.run();
    }
}

//Main class
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame
{

    /**
     * @param args
     */
    public static void main(String[] args) {
        myframe jwin = new myframe();
        jwin.setSize(600, 600);
        jwin.setVisible(true);
        jwin.run();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-10-17 22:32:58

你应该尝试使用repaint()而不是this.paint(gra),并将其放在线程内,你还需要将组件添加到你的图形界面

You should try using repaint() instead of this.paint(gra), and put it inside the thread, you also need to add the component to you graphic interface

薄荷→糖丶微凉 2024-10-17 22:32:58

您需要重写 JComponent 类的 PaintComponent() 方法。让它完成您的绘画并将该组件添加到您的 GUI 中。

You need to override the paintComponent() method of the JComponent class. Have it do your painting and add that component to your GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文