import java.awt.*;class Huayuan extends Frame
{
public void Kuang(){
setBounds(400,500,600,700);
setVisible(true);
setResizable(true);
}
public void paint(Graphics g){
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(100,100,50,60);
g.setColor(c); } }
public class Wancheng
{
public static void main(String[] args){
new Huayuan().Kuang();
}
}怎么样才能让圆每隔几秒就往下滚动 求大家帮忙解决 最好能有详细的讲解 谢谢了
{
public void Kuang(){
setBounds(400,500,600,700);
setVisible(true);
setResizable(true);
}
public void paint(Graphics g){
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(100,100,50,60);
g.setColor(c); } }
public class Wancheng
{
public static void main(String[] args){
new Huayuan().Kuang();
}
}怎么样才能让圆每隔几秒就往下滚动 求大家帮忙解决 最好能有详细的讲解 谢谢了
import java.awt.*;
import java.awt.event.*;
public class TankClient extends Frame{
int x = 50, y = 50;
@Override
/**
* 该方法自动在需要重画时调用
* 用来画圆
*/
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y += 5;
}
public void lauchFrame(){
this.setLocation(400, 300);
this.setSize(800, 600);
this.setTitle("坦克大战");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//设置窗体大小不能改变
this.setResizable(false);
//设置背景颜色
this.setBackground(Color.GREEN);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}
private class PaintThread implements Runnable{
//启动一个线程让不停的重画
@Override
public void run() {
while(true){
//访问外面包装类的repaint()方法
//实际是JFrame的repaint()方法
repaint(); //让重画
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}}运行结果
/**
* 该方法自动在需要重画时调用
* 用来画圆
*/
public void paint(Graphics g)
{
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
if (y <= 30)
{
down = true;
}
if (y >= 570)
{
down = false;
}
if (down)
{
y += 5;
}
else
{
y -= 5;
}
}