java for 重新,如何在Java中的for循环中的每个update()之后强...

这里的主要问题之一是您要在paint方法中调用Thread.sleep()-这不是一个好主意,因为这将停止在此期间进一步重新绘制应用程序. (事件分发线程/绘画线程不得用于任何慢速操作)

您要在此处实现的通常流程如下(可能比您需要的方式更详细):

>创建一个包含所有数字变量的模型类,尤其是仅包含数字(没有UI代码)的模型类,还会创建各种吸气剂,以允许UI以后访问这些数字.

>允许将此模型类传递给视图类(在您的情况下为Trip1)并设置为实例变量.

>从主控制类创建一个新的线程或计时器,该线程或计时器会根据需要定期调整型号/内部型号.

>为模型上的更改创建一个侦听器界面. (例如ModelChangedListener等)

>将侦听器列表添加到模型中-使用register方法将侦听器简单地添加到列表中.

>使其对模型的任何更改(即,当数字更新时)都会触发已注册的侦听器.

>在您的主控制类中,注册此模型的侦听器,该侦听器仅调用:

trip2Panel.repaint();

>在面板的paint()方法中,只需绘制当前模型即可.

完整代码发布:

package paranoid;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.Timer;

public class MasterFrame {

public static void main(String[] args) {

// TODO Auto-generated method stub

new MasterFrame();

}

public MasterFrame(){

JFrame f = new JFrame();

f.setTitle("Kaleidoscope");

final Trip2 trip2UI = new Trip2();

final TripModel model = new TripModel();

model.update();

Timer timer = new Timer(1, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

model.update();

}

});

timer.setRepeats(true);

timer.start();

model.addListener(new TripModelListener() {

@Override

public void modelChanged() {

trip2UI.repaint();

}

});

trip2UI.setModel(model);

f.add(trip2UI);

f.setSize(500,300);

f.setLocationRelativeTo(null);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

TripModelListener

package paranoid;

public interface TripModelListener {

void modelChanged();

}

Trip2(UI)

package paranoid;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JPanel;

public class Trip2 extends JPanel {

private static final long serialVersionUID = 1L;

private TripModel model;

public void paint(Graphics g){

g.setColor(Color.BLACK);

g.fillRect(model.getXMin(), model.getYMin(), model.getXMax(), model.getYMax());

for (int j = 0; j < 280; j++) {

for (int i = 0; i < 500; i++) {

if (model.getScreen()[i][j] == 0) {

g.setColor(Color.BLACK);

} else {

g.setColor(Color.GREEN);

}

g.drawLine(i, j, i, j); //plots pixel

}

}

}

public void setModel(TripModel model) {

this.model = model;

}

}//en

旅行模式

package paranoid;

import java.awt.Color;

import java.awt.Graphics;

import java.util.List;

import java.util.concurrent.CopyOnWriteArrayList;

public class TripModel {

private List listeners = new CopyOnWriteArrayList();

private int xmin = 0,

xmax = 499,

ymin = 0,

ymax = 279;

private int x = 120;

private int y = 80;

private int dx = 1;

private int dy = 1;

private int temp = 0;

private int update_counter = 0;

private int x_pos[] = new int[6];

private int y_pos[] = new int[6];

private int screen[][] = new int[500][280];

public TripModel() {

initialisation();

}

public void initialisation(){

System.out.println("initialising...");

x_pos[0] = x;

y_pos[0] = y;

x_pos[1] = xmax - x;

y_pos[1] = y;

x_pos[2] = x;

y_pos[2] = ymax - y;

x_pos[3] = xmax - x;

y_pos[3] = ymax - y;

x_pos[4] = (int)(xmax/2)-50;

y_pos[4] = (int)(ymax/2);

x_pos[5] = (int)(xmax/2)+50;

y_pos[5] = (int)(ymax/2);

for(int j = 0; j<280; j++){

for(int i = 0; i<500; i++){

screen[i][j] = 0;

}

}

} //end of initialisation()

public void update(){

//System.out.println("updating... for the "+update_counter+"th time");

temp = (int)Math.floor(Math.random()*100);

if(temp < 40){ // 40% chance that the direction is changed

dx = (int)Math.floor(Math.random()*3);

dy = (int)Math.floor(Math.random()*3);

dx = dx - 1;

dy = dy - 1;

}

x_pos[0] = x_pos[0]+dx;

y_pos[0] = y_pos[0]+dy;

x_pos[1] = x_pos[1]-dx;

y_pos[1] = y_pos[1]+dy;

x_pos[2] = x_pos[2]+dx;

y_pos[2] = y_pos[2]-dy;

x_pos[3] = x_pos[3]-dx;

y_pos[3] = y_pos[3]-dy;

x_pos[4] = x_pos[4]-dy;

y_pos[4] = y_pos[4]-dx;

x_pos[5] = x_pos[5]+dy;

y_pos[5] = y_pos[5]+dx;

for(int k = 0; k < 6; k++){

if(x_pos[k] < 0){

x_pos[k] = 0;

}

if(x_pos[k] > 499){

x_pos[k] = 499;

}

}

for(int k = 0; k < 6; k++){

if(y_pos[k] < 0){

y_pos[k] = 0;

}

if(y_pos[k] > 279){

y_pos[k] = 279;

}

}

screen[x_pos[0]][y_pos[0]] = 1;

screen[x_pos[1]][y_pos[1]] = 1;

screen[x_pos[2]][y_pos[2]] = 1;

screen[x_pos[3]][y_pos[3]] = 1;

screen[x_pos[4]][y_pos[4]] = 1;

screen[x_pos[5]][y_pos[5]] = 1;

update_counter = update_counter + 1;

fireModelChangedListener();

} //end of update()

private void fireModelChangedListener() {

for (TripModelListener listener : listeners) {

listener.modelChanged();

}

}

public int getXMin() {

return xmin;

}

public int getYMin() {

return ymin;

}

public int getYmin() {

return ymin;

}

public void setYmin(int ymin) {

this.ymin = ymin;

}

public int getXMax() {

return xmax;

}

public int getXmax() {

return xmax;

}

public void setXmax(int xmax) {

this.xmax = xmax;

}

public int getYMax() {

return ymax;

}

public int[][] getScreen() {

return screen;

}

public void addListener( TripModelListener listener) {

listeners.add(listener);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值