M Winda/src/winda/animation/ElevatorAnimation.java

M    Winda/src/winda/animation/ElevatorMovement.java
M    Winda/src/winda/gui/WindaView.form
M    Winda/src/winda/gui/WindaView.java
M    Winda/src/winda/logic/Pietro.java

Próba uruchomienia animacji
This commit is contained in:
Przemek Grondek 2011-04-13 01:36:49 +00:00
parent 9f5a44a354
commit 1c4a2c25f0
5 changed files with 99 additions and 32 deletions

View file

@ -53,7 +53,7 @@ public class ElevatorAnimation extends JApplet {
} }
/* /*
* Buforowana klatka * Buforowana klatki
*/ */
private BufferedImage buffImage(){ private BufferedImage buffImage(){
BufferedImage buffi = new BufferedImage(this.x_dimension, this.y_dmension, BufferedImage.TYPE_BYTE_GRAY); BufferedImage buffi = new BufferedImage(this.x_dimension, this.y_dmension, BufferedImage.TYPE_BYTE_GRAY);

View file

@ -1,16 +1,16 @@
package winda.animation; package winda.animation;
import java.awt.ScrollPane;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import winda.gui.WindaApp;
import winda.logic.Pietro;
/** /**
* *
* @author Przemo * @author Przemo
*/ */
public class ElevatorMovement { public class ElevatorMovement {
// private final int FPS = 50; /* Frames Per Second */
// private final int THREAD_WAIT_TIME = 1000/this.FPS;
private ElevatorAnimation ea; private ElevatorAnimation ea;
private int floor_count; private int floor_count;
@ -18,26 +18,32 @@ public class ElevatorMovement {
private int time_for_floor; // Tymczasowo /* Miliseconds */ private int time_for_floor; // Tymczasowo /* Miliseconds */
private int actual_floor; private int actual_floor;
private double jump_time; private double jump_time;
private double speed;
private int enter_exit_time;
public ElevatorMovement(int floor_count){ public ElevatorMovement(int floor_count){
this.floor_count = floor_count; this.floor_count = floor_count;
this.init(); this.init();
} }
public void goToFloor(int number){ public void goToFloor(Pietro pietro){
if(this.actual_floor<number) if(this.actual_floor<pietro.numerPietra)
this.goUp(number); this.goUp(pietro.numerPietra);
else if(this.actual_floor>number) else if(this.actual_floor>pietro.numerPietra)
this.goDown(number); this.goDown(pietro.numerPietra);
this.actual_floor = number; this.actual_floor = pietro.numerPietra;
this.exitElevator(pietro.pasazerowieWysiadajacy.size());
this.enterElevator(pietro.pasazerowieWsiadający.size(), pietro.numerPietra);
} }
private void goUp(int number){ private void goUp(int number){
System.out.println(""+(this.jump_time*this.speed));
while(this.ea.shift>(this.floor_size*(this.floor_count-(number+1)))){ while(this.ea.shift>(this.floor_size*(this.floor_count-(number+1)))){
try { try {
this.ea.shift--; this.ea.shift--;
this.ea.repaint(); this.ea.repaint();
Thread.sleep((long) jump_time); Thread.sleep((long) (this.jump_time*this.speed));
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex);
} }
@ -49,13 +55,36 @@ public class ElevatorMovement {
try { try {
this.ea.shift++; this.ea.shift++;
this.ea.repaint(); this.ea.repaint();
Thread.sleep((long) jump_time); Thread.sleep((long) (this.jump_time));
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
} }
private void exitElevator(int number){
for(int i=0;i<number;i++){
try {
Thread.sleep((long) (this.enter_exit_time*this.speed));
} catch (InterruptedException ex) {
Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex);
}
this.ea.elevator_passangers--;
}
}
private void enterElevator(int number, int floor){
for(int i=0;i<number;i++){
try {
Thread.sleep((long) (this.enter_exit_time*this.speed));
} catch (InterruptedException ex) {
Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex);
}
this.ea.elevator_passangers++;
this.ea.floor_passangers[floor]--;
}
}
public ElevatorAnimation getElevatorAnimation(){ public ElevatorAnimation getElevatorAnimation(){
return this.ea; return this.ea;
} }
@ -84,12 +113,23 @@ public class ElevatorMovement {
return this.ea.floor_passangers[floor]; return this.ea.floor_passangers[floor];
} }
public int getSpeed(){ public void setTimeForFloor(int time){
return this.time_for_floor; this.time_for_floor = time;
this.jump_time = ((double)time)/((double)(this.floor_size));
}
public double getSpeed(){
return this.speed;
} }
public void setSpeed(int speed){ public void setSpeed(int speed){
this.time_for_floor = speed; if(speed == 50)
this.speed = 1;
else if(speed <50){
this.speed = (((double)speed+50)/2)/100;
}
else
this.speed = (((double)speed+50)*2)/100;
} }
public void setFloorsCount(int floors){ public void setFloorsCount(int floors){
@ -97,11 +137,17 @@ public class ElevatorMovement {
this.init(); this.init();
} }
public void setEnterExitTime(int time){
this.enter_exit_time = time;
}
private void init(){ private void init(){
this.time_for_floor = 1000; this.time_for_floor = 1000;
this.actual_floor = 0; this.actual_floor = 0;
ea = new ElevatorAnimation(this.floor_count); this.ea = new ElevatorAnimation(this.floor_count);
ea.shift = this.floor_size * (this.floor_count-1); this.ea.shift = this.floor_size * (this.floor_count-1);
this.jump_time = ((double)this.time_for_floor)/((double)(this.floor_size)); this.jump_time = ((double)this.time_for_floor)/((double)(this.floor_size));
this.speed = 1;
} }
} }

View file

@ -605,6 +605,9 @@
<Properties> <Properties>
<Property name="name" type="java.lang.String" value="jSlider1" noResource="true"/> <Property name="name" type="java.lang.String" value="jSlider1" noResource="true"/>
</Properties> </Properties>
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="jSlider1StateChanged"/>
</Events>
</Component> </Component>
<Container class="javax.swing.JPanel" name="jPanel5"> <Container class="javax.swing.JPanel" name="jPanel5">
<Properties> <Properties>
@ -699,6 +702,10 @@
<Properties> <Properties>
<Property name="name" type="java.lang.String" value="scrollPane1" noResource="true"/> <Property name="name" type="java.lang.String" value="scrollPane1" noResource="true"/>
</Properties> </Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_SerializeTo" type="java.lang.String" value="WindaView_scrollPane1"/>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="129"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.ScrollPaneSupportLayout"/> <Layout class="org.netbeans.modules.form.compat2.layouts.support.ScrollPaneSupportLayout"/>
</Container> </Container>

View file

@ -17,8 +17,9 @@ import javax.swing.Icon;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JFrame; import javax.swing.JFrame;
import winda.animation.ElevatorAnimation;
import winda.animation.ElevatorMovement; import winda.animation.ElevatorMovement;
import winda.logic.Pasazer;
import winda.logic.Pietro;
import winda.logic.Winda; import winda.logic.Winda;
/** /**
@ -26,7 +27,6 @@ import winda.logic.Winda;
*/ */
public class WindaView extends FrameView { public class WindaView extends FrameView {
private ElevatorMovement em; private ElevatorMovement em;
private ElevatorAnimation ea;
Winda w = new Winda(); Winda w = new Winda();
public WindaView(SingleFrameApplication app) { public WindaView(SingleFrameApplication app) {
super(app); super(app);
@ -91,19 +91,15 @@ public class WindaView extends FrameView {
private void drawAnimation(){ private void drawAnimation(){
this.em = new ElevatorMovement(12); this.em = new ElevatorMovement(12);
this.ea = this.em.getElevatorAnimation(); this.scrollPane1.add(em.getElevatorAnimation());
this.scrollPane1.add(ea);
} }
/**
* Not working yet :/
* @param floor_count
*/
private void newAnimation(int floor_count){ private void newAnimation(int floor_count){
this.scrollPane1.remove(this.ea); this.scrollPane1.remove(this.em.getElevatorAnimation());
this.em.setFloorsCount(floor_count); this.em.setFloorsCount(floor_count);
this.ea = this.em.getElevatorAnimation(); this.scrollPane1.add(this.em.getElevatorAnimation());
this.scrollPane1.add(this.ea); this.scrollPane1.doLayout();
this.scrollPane1.repaint();
} }
@Action @Action
@ -510,6 +506,11 @@ public class WindaView extends FrameView {
jButton4.setName("jButton4"); // NOI18N jButton4.setName("jButton4"); // NOI18N
jSlider1.setName("jSlider1"); // NOI18N jSlider1.setName("jSlider1"); // NOI18N
jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSlider1StateChanged(evt);
}
});
jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel5.setName("jPanel5"); // NOI18N jPanel5.setName("jPanel5"); // NOI18N
@ -733,6 +734,7 @@ public class WindaView extends FrameView {
String ip = (String) jSpinner1.getValue().toString(); String ip = (String) jSpinner1.getValue().toString();
int iloscPieter = Integer.parseInt(ip); int iloscPieter = Integer.parseInt(ip);
w.SetIloscPieter(iloscPieter); w.SetIloscPieter(iloscPieter);
this.newAnimation(iloscPieter);
}//GEN-LAST:event_jSpinner1StateChanged }//GEN-LAST:event_jSpinner1StateChanged
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
@ -756,6 +758,12 @@ public class WindaView extends FrameView {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here: // TODO add your handling code here:
Pietro p = new Pietro();
p.numerPietra = 2;
p.pasazerowieWsiadający.add(new Pasazer(0,2,2));
this.em.setPassangersOnFloor(2, 1);
em.setTimeForFloor(1000);
this.em.goToFloor(p);
}//GEN-LAST:event_jButton1ActionPerformed }//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
@ -774,6 +782,11 @@ public class WindaView extends FrameView {
w.ZapiszPasazerow(fc.getSelectedFile().getPath()); w.ZapiszPasazerow(fc.getSelectedFile().getPath());
}//GEN-LAST:event_jMenuItem2ActionPerformed }//GEN-LAST:event_jMenuItem2ActionPerformed
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
this.em.setSpeed(this.jSlider1.getValue());
// TODO add your handling code here:
}//GEN-LAST:event_jSlider1StateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2; private javax.swing.ButtonGroup buttonGroup2;
@ -825,7 +838,7 @@ public class WindaView extends FrameView {
private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField1;
private javax.swing.JPanel mainPanel; private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar; private javax.swing.JMenuBar menuBar;
private java.awt.ScrollPane scrollPane1; public transient java.awt.ScrollPane scrollPane1;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
private final Timer messageTimer; private final Timer messageTimer;

View file

@ -5,6 +5,7 @@
package winda.logic; package winda.logic;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -12,7 +13,7 @@ import java.util.List;
* @author Tomek * @author Tomek
*/ */
public class Pietro { public class Pietro {
public List<Pasazer> pasazerowieWsiadający; public List<Pasazer> pasazerowieWsiadający = new ArrayList();
public List<Pasazer> pasazerowieWysiadajacy; public List<Pasazer> pasazerowieWysiadajacy = new ArrayList();
public int numerPietra; public int numerPietra;
} }