JAVA实现一个圆形的移动,画出来的圆形移动,我的圆它不动呀 100
packagekeylistenermove;importjava.awt.*;importjava.awt.Event.*;importjava.awt.event.K...
package keylistenermove;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class joinDemo1 extends JFrame
{
//继承
private int x=100, y=100, r=100;
//初始值
public joinDemo1()
{
super("小图形");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setVisible(true);
Thread thread=new Thread(new Graphicss());
thread.start();
}
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(x, y, r, r);
}
public static void main(String[] args)
{
new joinDemo1();
}
class Graphicss extends JFrame implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int j = 0; j <= 240; j++) {
try {
Thread.sleep(10);// 当前线程休眠0.01秒
} catch (InterruptedException e) {
e.printStackTrace();
}
y=y+j;
super.repaint();
}
}
}
} 展开
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class joinDemo1 extends JFrame
{
//继承
private int x=100, y=100, r=100;
//初始值
public joinDemo1()
{
super("小图形");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setVisible(true);
Thread thread=new Thread(new Graphicss());
thread.start();
}
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(x, y, r, r);
}
public static void main(String[] args)
{
new joinDemo1();
}
class Graphicss extends JFrame implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int j = 0; j <= 240; j++) {
try {
Thread.sleep(10);// 当前线程休眠0.01秒
} catch (InterruptedException e) {
e.printStackTrace();
}
y=y+j;
super.repaint();
}
}
}
} 展开
2个回答
展开全部
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
public class JoinDemo extends JFrame {
public static void main(String[] args) {
new JoinDemo();
}
private int x = 100, y = 100, r = 100;
public JoinDemo() {
this.setTitle("小图形");
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
new UpdateThread().start();
}
public void paint(Graphics g) {
Image image = createImage(getWidth(), getHeight());
Graphics gg = image.getGraphics();
bufferedPaint(gg);
gg.dispose();
g.drawImage(image, 0, 0, null);
}
private void bufferedPaint(Graphics gg) {
gg.fillOval(x, y, r, r);
}
class UpdateThread extends Thread {
public void run() {
for (int j = 0; j <= 240; j++) {
y++;
repaint();
sleep(10);
}
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
说明:
第一:存为JoinDemo.java
第二:加入了一个缓冲,以免画面闪烁。
展开全部
public class joinDemo1 extends JFrame
{
//继承
private int x=100, y=100, r=100;
//初始值
public joinDemo1()
{
super("小图形");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setVisible(true);
Thread thread=new Thread(new Graphicss());
thread.start();
}
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(x, y, r, r);
}
public static void main(String[] args)
{
new joinDemo1();
}
class Graphicss implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int j = 0; j <= 240; j++) {
revalidate();
// System.out.println(j);
try {
Thread.sleep(1000);// 当前线程休眠0.01秒
} catch (InterruptedException e) {
e.printStackTrace();
}
y=y+j;
repaint();
}
}
}
}
现在再跑,试试,自己分析一下原因
个人意见,仅供参考!希望帮得到你
{
//继承
private int x=100, y=100, r=100;
//初始值
public joinDemo1()
{
super("小图形");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setVisible(true);
Thread thread=new Thread(new Graphicss());
thread.start();
}
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(x, y, r, r);
}
public static void main(String[] args)
{
new joinDemo1();
}
class Graphicss implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int j = 0; j <= 240; j++) {
revalidate();
// System.out.println(j);
try {
Thread.sleep(1000);// 当前线程休眠0.01秒
} catch (InterruptedException e) {
e.printStackTrace();
}
y=y+j;
repaint();
}
}
}
}
现在再跑,试试,自己分析一下原因
个人意见,仅供参考!希望帮得到你
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询