求助Java的一个小程序, 原本想小球向下移动的, 为什么变成拉长的!!!
代码:packagechap01;importjavax.swing.*;importjava.awt.*;publicclassTestextendsJFrame{in...
代码:
package chap01;
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame{
int y = 180;
Test()
{
super("测试!");
thread t = new thread(this);
setSize(500,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
t.start();
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(100,y,100,100);
}
public class thread extends Thread{
Test t;
public thread(Test t)
{
this.t = t;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(100);
}catch(Exception e){}
t.y += 10;
if(t.y>510) t.y = -100;
t.repaint();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
}
} 展开
package chap01;
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame{
int y = 180;
Test()
{
super("测试!");
thread t = new thread(this);
setSize(500,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
t.start();
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(100,y,100,100);
}
public class thread extends Thread{
Test t;
public thread(Test t)
{
this.t = t;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(100);
}catch(Exception e){}
t.y += 10;
if(t.y>510) t.y = -100;
t.repaint();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
}
} 展开
展开全部
这是由于你直接画在了Fram上面,这样会出现闪烁,下面是我重新写了一下的代码,repaint()的时候系统会自动调用update 方法
--------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
public class Test extends Frame {
int y = 180;
int width=500;
int height=700;
Image screenImage=null;
Test() {
super("测试!");
this.setSize(width, height);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
new thread().start();
}
@Override
public void update(Graphics g) {
// TODO Auto-generated method stub
if(screenImage==null){
screenImage=this.createImage(500, 700);
}
Graphics gra=screenImage.getGraphics();
Color c=gra.getColor();
gra.setColor(getBackground());
gra.fillRect(0, 0, width, height);
gra.setColor(c);
paint(gra);
g.drawImage(screenImage, 0, 0, null);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillOval(100, y, 100, 100);
}
private class thread extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(1000);
y += 10;
if (y > 510)
y = -100;
} catch (Exception e) {
}
repaint();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
}
}
--------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
public class Test extends Frame {
int y = 180;
int width=500;
int height=700;
Image screenImage=null;
Test() {
super("测试!");
this.setSize(width, height);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
new thread().start();
}
@Override
public void update(Graphics g) {
// TODO Auto-generated method stub
if(screenImage==null){
screenImage=this.createImage(500, 700);
}
Graphics gra=screenImage.getGraphics();
Color c=gra.getColor();
gra.setColor(getBackground());
gra.fillRect(0, 0, width, height);
gra.setColor(c);
paint(gra);
g.drawImage(screenImage, 0, 0, null);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillOval(100, y, 100, 100);
}
private class thread extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(1000);
y += 10;
if (y > 510)
y = -100;
} catch (Exception e) {
}
repaint();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询