/**
 * 想写一个俄罗斯方快游戏 但是刚进入第一步就出问题了
 * 方快自由下降不均匀 有时候会下降一段好长的距离(DSPEED > 5);
 * 原来想法是让它每500毫秒下降 DSPEED = 5 的距离的;
 * 是不是线程问题, 但是查不出来 太菜了 哪位大哥谁帮我看看。
 *///BrickWindow.javaimport java.awt.*;
import java.awt.event.*;public class BrickWindow extends Frame {
public Prick pr; public MyPanel pa = new MyPanel(); public void windowRun() {
this.setBackground(Color.LIGHT_GRAY);
this.setBounds(200, 150, 300, 400);
this.setLayout(null); this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pa.laction(); // Panel pa = new Panel();
// pa.setBackground(Color.gray);
add(pa);
this.setVisible(true);
} public static void main(String[] agrs) {
BrickWindow tc = new BrickWindow();
tc.windowRun();
} public class MyPanel extends Panel {
Prick pr = new Prick(145, 0, this); Image ima; public void laction() {
setBackground(Color.gray);
setBounds(0, 80, 300, 320);
new Thread(new MyThread()).start();
} public void paint(Graphics e) {
pr.draw(e);
} public void update(Graphics e) {
ima = this.createImage(300, 320);
Graphics kk = ima.getGraphics();
kk.setColor(Color.gray);
kk.drawRect(0, 0, 300, 320);
kk.setColor(Color.black);
paint(kk);
e.drawImage(ima, 0, 0, null);
} class MyThread implements Runnable {
public void run() {
while (true) {
repaint();
//synchronized (this) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//}
}
}
}
}
}
//Prick.javaimport java.awt.*;public class Prick {
private int x;
private int y;
Panel tc;
static final int LSPEED = 5;
static final int RSPEED = 5;
static final int DSPEED = 5;
static final int WIDTH = 10;
static final int HEIGHT = 10;

public Prick(int x, int y, Panel tc) {
this.x = x;
this.y = y;
this.tc = tc;
}

public void draw(Graphics e){
Color col = e.getColor();
e.setColor(Color.black);
//e.fillRect(x, y, WIDTH, HEIGHT);
//e.fillRect(x, y + 10, WIDTH, HEIGHT);
//e.fillRect(x, y + 20, WIDTH, HEIGHT);
//e.fillRect(x, y + 30, WIDTH, HEIGHT);
e.fillRect(x, y, WIDTH, HEIGHT);
e.fillRect(x, y + 10, WIDTH, HEIGHT);
e.fillRect(x + 10, y + 10, WIDTH, HEIGHT);
e.fillRect(x, y + 20, WIDTH, HEIGHT);
move();
}

public void move() {
y += 5;
}
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【yangyongjie81】截止到2008-07-25 10:37:23的历史汇总数据(不包括此帖):
    发帖的总数量:22                       发帖的总分数:820                      每贴平均分数:37                       
    回帖的总数量:9                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:22                       结贴的总分数:820                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    敬礼!
      

  2.   

    试了20+遍 没发生异常 
    试着去掉update方法呢?
      

  3.   

    2楼 去掉update方法后 就很均匀的下落了不 会有时 一下掉好多了!(你看不到异常可能是机器原因吧 我的机子
    有点年份了 2005年买的) 但是我update 方法是让图片重画的时候看不到闪烁 去掉了就有点闪烁了,
      

  4.   

    /**
     * 想写一个俄罗斯方快游戏 但是刚进入第一步就出问题了 方快自由下降不均匀 有时候会下降一段好长的距离(DSPEED > 5);
     * 原来想法是让它每500毫秒下降 DSPEED = 5 的距离的;是不是线程问题, 但是查不出来 太菜了 哪位大哥谁帮我看看。
     */
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    //import java.awt.Panel;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    //BrickWindow.java
    @SuppressWarnings("serial")
    public class BrickWindow extends Frame { public Prick pr;
    public MyPanel pa = new MyPanel(); public void windowRun() {
    this.setBackground(Color.LIGHT_GRAY);
    this.setBounds(200, 150, 300, 400);
    this.setLayout(null);
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    this.pa.laction();
    //Panel pa = new Panel();
    //pa.setBackground(Color.gray);
    add(this.pa);
    this.setVisible(true);
    } public static void main(String[] agrs) {
    BrickWindow tc = new BrickWindow();
    tc.windowRun();
    } public class MyPanel extends /*Panel*/Canvas {  //用 Canvas 效率更高 Prick pr = new Prick(145, 0/*, this*/);
    Image ima; public void laction() {
    setBackground(Color.gray);
    setBounds(0, 80, 300, 320);
    new Thread(new MyThread()).start();
    } public void paint(Graphics e) {
    //this.pr.draw(e);
    e.drawImage(this.ima, 0, 0, null);
    } public void update(Graphics e) {  //双缓冲不是这样用的
    //this.ima = this.createImage(300, 320);
    //没必要每画一贞都创建一个新 Image 对象,你这样太浪费资源
    if (this.ima == null) this.ima = this.createImage(300, 320);
    Graphics kk = this.ima.getGraphics();
    kk.setColor(Color.gray);
    //kk.drawRect(0, 0, 300, 320);
    kk.fillRect(0, 0, 300, 320);
    //kk.setColor(Color.black);  //你在 Prick 的 draw 方法里有颜色设置代码
    this.pr.draw(kk);
    //paint(kk);
    paint(e);
    //e.drawImage(this.ima, 0, 0, null);
    } class MyThread implements Runnable { public void run() {
    while (true) {
    repaint();
    //synchronized (this) {
    try {
    Thread.sleep(100L);  //把贞频提高点能获得更流畅的动画效果
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    //}
    }
    } } }}
    //Prick.java
    class Prick { private int x;
    private int y;
    /*Panel*///Canvas tc;  //根本没用到
    static final int LSPEED = 5;
    static final int RSPEED = 5;
    static final int DSPEED = 3;
    static final int WIDTH = 10;
    static final int HEIGHT = 10; public Prick(int x, int y/*, Panel Canvas tc*/) {
    this.x = x;
    this.y = y;
    //this.tc = tc;
    } public void draw(Graphics e) {
    //Color col = e.getColor();
    e.setColor(Color.black);
    //e.fillRect(x, y, WIDTH, HEIGHT);
    //e.fillRect(x, y + 10, WIDTH, HEIGHT);
    //e.fillRect(x, y + 20, WIDTH, HEIGHT);
    //e.fillRect(x, y + 30, WIDTH, HEIGHT);
    e.fillRect(this.x, this.y, WIDTH, HEIGHT);
    e.fillRect(this.x, this.y + 10, WIDTH, HEIGHT);
    e.fillRect(this.x + 10, this.y + 10, WIDTH, HEIGHT);
    e.fillRect(this.x, this.y + 20, WIDTH, HEIGHT);
    move();
    } public void move() {
    //this.y += 5;
    this.y += DSPEED;  //你自己定义的常量为什么不用
    }}