2个回答
展开全部
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PaintOval {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(1024, 768);
MyPanel panel=new MyPanel();
frame.add(panel);
Thread thread =new Thread(panel);
thread.start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable{
int x=30,y=30;
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.drawOval(x, y, 50, 50);
}
public void run(){
while(true){
x++;
if(x>1034){
x=0;
}
try{
Thread.sleep(20);
}catch(Exception e){
e.printStackTrace();
}
repaint();
}
}
}
我不知道你要什么方向移动的圆,我就画了向右移动的圆
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PaintOval {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(1024, 768);
MyPanel panel=new MyPanel();
frame.add(panel);
Thread thread =new Thread(panel);
thread.start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable{
int x=30,y=30;
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.drawOval(x, y, 50, 50);
}
public void run(){
while(true){
x++;
if(x>1034){
x=0;
}
try{
Thread.sleep(20);
}catch(Exception e){
e.printStackTrace();
}
repaint();
}
}
}
我不知道你要什么方向移动的圆,我就画了向右移动的圆
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询