项目做完了,想找个东西练练,觉得电梯挺有意思的,就动手写了一个,写的不成样子...
样子很丑,不过已经可以跑了,但是需要添加的东西还很多(为每个楼层添加上下按钮及监听事件,界面美化,电梯内部视图等等)
有兴趣的C友来完善一下咯,代码拷回去可以直接跑的,注意包结构就是了...
我自己也在改代码,权当抛砖引玉咯,O(∩_∩)O~/**
 * Class Lift
 * 
 * File Lift.java
 * 
 * @author Wintree
 * 
 * @email [email protected]
 * 
 * date 2010-01-20
 */
package com.lk.lift.lift;public class Lift{
public static final int width = 80;
public static final int height = 100; 
private int pauseTime = 0;
private boolean isMove = false;
public Lift(boolean isMove) {
this.isMove = isMove;
}
public int getPauseTime() {
return pauseTime;
}
public void setPauseTime(int pauseTime) {
this.pauseTime = pauseTime;
}
public boolean isMove() {
return isMove;
}
public void setMove(boolean isMove) {
this.isMove = isMove;
}
}
/**
 * Class Building
 * 
 * File Building.java
 * 
 * @author Wintree
 * 
 * @email [email protected]
 * 
 * date 2010-01-20
 */
package com.lk.lift.building;public class Building {
/**
 * The Floors Of The Building
 */
public static final int FLOORS = 6;

/**
 * The Distance Between The Two NeighBouring Floors Of The Building
 */
public static final int FLOORS_DISTANCE = 100;
}
/**
 * Class MainFrame
 * 
 * File MainFrame.java
 * 
 * @author Wintree
 * 
 * @email [email protected]
 * 
 * date 2010-01-20
 */
package com.lk.lift.main;import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import com.lk.lift.building.Building;
import com.lk.lift.lift.Lift;@SuppressWarnings("serial")
public class MainFrame extends Frame{
private Lift lift = new Lift(true);
private boolean direction = true; //true: up; flase: down
private static final int TITLE = 100;
private int x = 300;
private int y = (Building.FLOORS-1)*Building.FLOORS_DISTANCE + TITLE;
// private Image offScreenImage = null;


public int getCurrentFloor() {
return 6 - (this.y - TITLE) / Building.FLOORS_DISTANCE;
} @SuppressWarnings("static-access")
@Override
public void paint(Graphics g) {
Color c = g.getColor();
//Draw Floors

int floor = 1;
g.setColor(Color.ORANGE);
g.fillRect(x+1, 1, Lift.width-2, (Building.FLOORS)*Building.FLOORS_DISTANCE + TITLE-2);
g.setColor(Color.BLACK);
g.drawRect(x, 0, Lift.width, (Building.FLOORS)*Building.FLOORS_DISTANCE + TITLE);
for(int start=Building.FLOORS*Building.FLOORS_DISTANCE + TITLE; start > TITLE; start -= Building.FLOORS_DISTANCE) {
g.setColor(Color.BLUE);
g.setFont(Font.getFont("黑体"));
String move = this.direction ? "\u2191" : "\u2193";
g.drawString("第" + floor++ + "层", x/2, start);

g.setColor(Color.RED);
g.drawString(this.getCurrentFloor() + "F\t" + move, 3*x/4, start);
g.drawLine(0, start, x, start);
} //Draw Lift
g.setColor(Color.BLACK);
g.drawRect(x, y, Lift.width, Lift.height);
g.setColor(Color.GRAY);
g.fillRect(x+1, y+1, Lift.width-2, Lift.height-2);
g.setColor(Color.BLACK);
g.drawLine(x + Lift.width/2, y, x + Lift.width/2, y + Lift.height);
if(0 == (y-TITLE) % Building.FLOORS_DISTANCE) {
if((int)(Math.random()*10) > 5) {
lift.setMove(false);
}
}
g.setColor(c);
//Lift Move
if(lift.isMove()) {
if(y >= (Building.FLOORS-1)*Building.FLOORS_DISTANCE + TITLE){
this.direction = true;
}else if(y <= TITLE) {
this.direction = false;
}
if(this.direction) {
y -=10;
}else {
y +=10;
}
}else {
try {

long time = 2000 + (long)(Math.random() * 3000);
//Open door
g.setColor(Color.RED);
g.drawString("暂停" + time/1000 + "s", x-50, y+Building.FLOORS_DISTANCE);
Thread.sleep(1000);
g.setColor(Color.green);
g.fillRect(x+Lift.width/10+1, y+1, 8*Lift.width/10-2, Lift.height-2);
g.setColor(Color.BLACK);
g.drawRect(x+Lift.width/10, y, 8*Lift.width/10, Lift.height);
//Pause
Thread.sleep(time-1000);
lift.setMove(true);
if(this.direction) {
y -=10;
}else {
y +=10;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public void launchFrame() {
this.setVisible(true);
this.setBounds(0, 0, 600, 800); //Best Way to a public static final variable
this.setBackground(Color.GRAY);
this.setTitle("VirtualLift");
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
new PaintTread().run();
}

//inner class to called method to repaint;
private class PaintTread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
mainFrame.launchFrame();

}
}

解决方案 »

  1.   

    关闭的时候应该用windowClosing 窗口资源未注销前比如dispose貌似是不会触发windowClosed
    一个小问题
      

  2.   

    机器没装,还是内网...
    找个安静的Q群,有进展讨论一下?...
    O(∩_∩)O~
      

  3.   

    我试了用双缓冲还是有用的,可能你没用对吧,不过一些逻辑得从绘图区分开if(bgImage==null){
        bgImage=createImage(getSize().width,getSize().height);
        bg=bgImage.getGraphics();
    }
    bg.drawImage(viewImage,0,0,this);
    g.drawImage(bgImage,0,0,this);
      

  4.   

    嗯,我找了双缓冲代码,下面是改过的MainFrame代码,/**
     * Class MainFrame
     * 
     * File MainFrame.java
     * 
     * @author Wintree
     * 
     * @email [email protected]
     * 
     * date 2010-01-20
     */
    package com.lk.lift.main;import java.awt.Color;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import com.lk.lift.building.Building;
    import com.lk.lift.lift.Lift;@SuppressWarnings("serial")
    public class MainFrame extends Frame{
    private Lift lift = new Lift(true);
    private boolean direction = true; //true: up; flase: down
    private static final int TITLE = 100;
    private int x = 300;
    private int y = (Building.FLOORS-1)*Building.FLOORS_DISTANCE + TITLE;
    private Image offScreenImage = null;


    @Override
    public void update(Graphics g) {
    Color c = g.getColor();
    if(this.offScreenImage == null) {
    this.offScreenImage = this.createImage(this.getSize().width, this.getSize().height);
    }
    Graphics goffScreen = this.offScreenImage.getGraphics();
    g.setColor(Color.GRAY);
    goffScreen.fillRect(0, 0, this.getSize().width, this.getSize().height);
    g.setColor(c);
    paint(goffScreen);
    g.drawImage(this.offScreenImage, 0, 0, null);
    } public int getCurrentFloor() {
    return 6 - (this.y - TITLE) / Building.FLOORS_DISTANCE;
    } @SuppressWarnings("static-access")
    @Override
    public void paint(Graphics g) {
    Color c = g.getColor();
    //Draw Floors

    int floor = 1;
    g.setColor(Color.ORANGE);
    g.fillRect(x+1, 1, Lift.width-2, (Building.FLOORS)*Building.FLOORS_DISTANCE + TITLE-2);
    g.setColor(Color.BLACK);
    g.drawRect(x, 0, Lift.width, (Building.FLOORS)*Building.FLOORS_DISTANCE + TITLE);
    for(int start=Building.FLOORS*Building.FLOORS_DISTANCE + TITLE; start > TITLE; start -= Building.FLOORS_DISTANCE) {
    g.setColor(Color.BLUE);
    g.setFont(Font.getFont("黑体"));
    String move = this.direction ? "\u2191" : "\u2193";
    g.drawString("第" + floor++ + "层", x/2, start);

    g.setColor(Color.RED);
    g.drawString(this.getCurrentFloor() + "F\t" + move, 3*x/4, start);
    g.drawLine(0, start, x, start);
    } //Draw Lift
    g.setColor(Color.BLACK);
    g.drawRect(x, y, Lift.width, Lift.height);
    g.setColor(Color.GRAY);
    g.fillRect(x+1, y+1, Lift.width-2, Lift.height-2);
    g.setColor(Color.BLACK);
    g.drawLine(x + Lift.width/2, y, x + Lift.width/2, y + Lift.height);
    if(0 == (y-TITLE) % Building.FLOORS_DISTANCE) {
    if((int)(Math.random()*10) > 5) {
    lift.setMove(false);
    }
    }
    g.setColor(c);
    //Lift Move
    if(lift.isMove()) {
    if(y >= (Building.FLOORS-1)*Building.FLOORS_DISTANCE + TITLE){
    this.direction = true;
    }else if(y <= TITLE) {
    this.direction = false;
    }
    if(this.direction) {
    y -=2;
    }else {
    y +=2;
    }
    }else {
    try {

    long time = 2000 + (long)(Math.random() * 8000);
    //Open door
    g.setColor(Color.RED);
    g.drawString("暂停" + time/1000 + "s", x-50, y+Building.FLOORS_DISTANCE);
    Thread.sleep(1000);
    g.setColor(Color.green);
    g.fillRect(x+Lift.width/10+1, y+1, 8*Lift.width/10-2, Lift.height-2);
    g.setColor(Color.BLACK);
    g.drawRect(x+Lift.width/10, y, 8*Lift.width/10, Lift.height);
    //Pause
    Thread.sleep(time-1000);
    lift.setMove(true);
    if(this.direction) {
    y -=2;
    }else {
    y +=2;
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    } public void launchFrame() {
    this.setVisible(true);
    this.setBounds(0, 0, 600, 800); //Best Way to a public static final variable
    this.setBackground(Color.GRAY);
    this.setTitle("VirtualLift");
    this.setResizable(false);
    this.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
    System.exit(0);
    }
    });
    new PaintTread().run();
    }

    //inner class to called method to repaint;
    private class PaintTread implements Runnable {
    public void run() {
    while(true) {
    repaint();
    try {
    Thread.sleep(20);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    public static void main(String[] args) {
    MainFrame mainFrame = new MainFrame();
    mainFrame.launchFrame();

    }
    }电梯门好像不会开了,有时候闪那么一下...
      

  5.   

    感兴趣的人都哪儿去了,在研究java2D...