A Winda/src/winda/animation

A    Winda/src/winda/animation/ElevatorAnimation.java	Elementy Animacji
A    Winda/src/winda/animation/ElevatorMovement.java	Silnik Obsługujący Dane animacji
M    Winda/src/winda/gui/WindaView.form
M    Winda/src/winda/gui/WindaView.java			Podłączona animacja
M    Winda/src/winda/logic/AlgorytmGoraDol.java		Cośtam bo nie chciało się kompilować
This commit is contained in:
Przemek Grondek 2011-04-12 22:35:05 +00:00
parent 19bb51ba2b
commit 9f5a44a354
5 changed files with 283 additions and 23 deletions

View file

@ -0,0 +1,124 @@
package winda.animation;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JApplet;
/**
*
* @author Przemo
*/
public class ElevatorAnimation extends JApplet {
private int x_dimension = 200;
private int y_dmension;
private int floor_count;
private final int floor_size = 50;
private final int elevator_size_x = 40;
private final int elevator_size_y = 50;
private Font elevator_font;
private Font floor_font;
public int shift;
public int elevator_passangers;
public int[] floor_passangers;
public ElevatorAnimation(int floor_count){
this.floor_count = floor_count;
this.y_dmension = floor_size*this.floor_count;
this.elevator_font = new Font("Serif", Font.PLAIN, 30);
this.floor_font = new Font("Serif", Font.PLAIN, 16);
this.floor_passangers = new int[floor_count];
}
@Override
public Dimension getPreferredSize(){
return new Dimension(this.x_dimension, this.y_dmension);
}
@Override
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(this.buffImage(), null, 0, 0);
}
/*
* Buforowana klatka
*/
private BufferedImage buffImage(){
BufferedImage buffi = new BufferedImage(this.x_dimension, this.y_dmension, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D buffig = (Graphics2D) buffi.getGraphics();
Rectangle r = new Rectangle(0,0,this.x_dimension,this.y_dmension);
buffig.setBackground(Color.WHITE);
buffig.setColor(Color.BLACK);
buffig.clearRect(0, 0, this.x_dimension, this.y_dmension);
this.drawElevatorShaft(buffig);
this.drawFloors(buffig);
this.drawPassangers(buffig);
this.drawElevator(buffig);
return buffi;
}
/*
* Rysowanie szybu windy
*/
private void drawElevatorShaft(Graphics2D g2){
Line2D left = new Line2D.Double(45, 0, 45, this.y_dmension);
Line2D right = new Line2D.Double(95, 0, 95, this.y_dmension);
g2.draw(left);
g2.draw(right);
}
/*
* Rysowanie Pięter z numerami
*/
private void drawFloors(Graphics2D g2){
g2.setFont(this.floor_font);
for(int i=1;i<=this.floor_count;i++){
int tmp = this.floor_count-i;
g2.draw(new Line2D.Double(0, this.floor_size*i, this.x_dimension, this.floor_size*i));
g2.drawString(""+tmp, 100, (this.floor_size*i)-5);
}
}
/*
* Rysowanie ilosci pasażerów na piętrach
*/
private void drawPassangers(Graphics2D g2){
g2.setFont(elevator_font);
for(int i=1;i<=this.floor_count;i++){
g2.drawString(""+this.floor_passangers[this.floor_count-i], 120 ,(this.floor_size*i)-15);
}
}
/*
* Rysowanie windy
*/
private void drawElevator(Graphics2D g2){
Rectangle2D winda = new Rectangle2D.Double(50, 00, this.elevator_size_x, this.elevator_size_y);
g2.translate(0, this.shift);
g2.clearRect(50, 00, this.elevator_size_x, this.elevator_size_y);
g2.draw(winda);
g2.setFont(this.elevator_font);
if(this.elevator_passangers < 10)
g2.drawString("0"+this.elevator_passangers, 55, 35);
else
g2.drawString(""+this.elevator_passangers, 55, 35);
}
}

View file

@ -0,0 +1,107 @@
package winda.animation;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Przemo
*/
public class ElevatorMovement {
// private final int FPS = 50; /* Frames Per Second */
// private final int THREAD_WAIT_TIME = 1000/this.FPS;
private ElevatorAnimation ea;
private int floor_count;
private final int floor_size = 50;
private int time_for_floor; // Tymczasowo /* Miliseconds */
private int actual_floor;
private double jump_time;
public ElevatorMovement(int floor_count){
this.floor_count = floor_count;
this.init();
}
public void goToFloor(int number){
if(this.actual_floor<number)
this.goUp(number);
else if(this.actual_floor>number)
this.goDown(number);
this.actual_floor = number;
}
private void goUp(int number){
while(this.ea.shift>(this.floor_size*(this.floor_count-(number+1)))){
try {
this.ea.shift--;
this.ea.repaint();
Thread.sleep((long) jump_time);
} catch (InterruptedException ex) {
Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void goDown(int number) {
while(this.ea.shift<(this.floor_size*(this.floor_count-(number+1)))){
try {
this.ea.shift++;
this.ea.repaint();
Thread.sleep((long) jump_time);
} catch (InterruptedException ex) {
Logger.getLogger(ElevatorMovement.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public ElevatorAnimation getElevatorAnimation(){
return this.ea;
}
public void setPassangersOnFloor(int floor, int number){
this.ea.floor_passangers[floor]=number;
}
public void setPassangersOnFloors(int []passangers){
if(passangers.length == this.ea.floor_passangers.length)
this.ea.floor_passangers = passangers;
else
for(int i=0;i<passangers.length;i++)
this.ea.floor_passangers[i] = passangers[i];
}
public void setPassangersInElevator(int passangers){
this.ea.elevator_passangers = passangers;
}
public int getPassangersInElevator(){
return this.ea.elevator_passangers;
}
public int getPassangersOnFloor(int floor){
return this.ea.floor_passangers[floor];
}
public int getSpeed(){
return this.time_for_floor;
}
public void setSpeed(int speed){
this.time_for_floor = speed;
}
public void setFloorsCount(int floors){
this.floor_count = floors;
this.init();
}
private void init(){
this.time_for_floor = 1000;
this.actual_floor = 0;
ea = new ElevatorAnimation(this.floor_count);
ea.shift = this.floor_size * (this.floor_count-1);
this.jump_time = ((double)this.time_for_floor)/((double)(this.floor_size));
}
}

View file

@ -26,18 +26,18 @@
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="jSeparator2" min="-2" pref="18" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="103" groupAlignment="1" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="10" max="-2" attributes="0"/>
<Component id="jButton3" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="jButton4" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="28" max="-2" attributes="0"/>
<Component id="jSlider1" pref="354" max="32767" attributes="0"/>
<Component id="jSlider1" pref="389" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" min="-2" pref="520" max="-2" attributes="0"/>
<Component id="scrollPane1" min="-2" pref="551" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
@ -88,8 +88,9 @@
<Component id="jPanel3" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="jScrollPane1" min="-2" pref="433" max="-2" attributes="1"/>
<EmptySpace min="-2" pref="18" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="10" max="-2" attributes="0"/>
<Component id="scrollPane1" min="-2" pref="431" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jButton3" alignment="3" min="-2" max="-2" attributes="0"/>
@ -108,13 +109,6 @@
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<Properties>
<Property name="name" type="java.lang.String" value="jScrollPane1" noResource="true"/>
</Properties>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
</Container>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="text" type="java.lang.String" resourceKey="jLabel1.text"/>
@ -701,6 +695,13 @@
</Component>
</SubComponents>
</Container>
<Container class="java.awt.ScrollPane" name="scrollPane1">
<Properties>
<Property name="name" type="java.lang.String" value="scrollPane1" noResource="true"/>
</Properties>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.ScrollPaneSupportLayout"/>
</Container>
</SubComponents>
</Container>
<Container class="javax.swing.JMenuBar" name="menuBar">
@ -818,6 +819,6 @@
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,57,0,0,4,-74"/>
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,57,0,0,4,-39"/>
</AuxValues>
</Form>

View file

@ -17,12 +17,16 @@ import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import winda.animation.ElevatorAnimation;
import winda.animation.ElevatorMovement;
import winda.logic.Winda;
/**
* The application's main frame.
*/
public class WindaView extends FrameView {
private ElevatorMovement em;
private ElevatorAnimation ea;
Winda w = new Winda();
public WindaView(SingleFrameApplication app) {
super(app);
@ -82,6 +86,24 @@ public class WindaView extends FrameView {
}
}
});
this.drawAnimation();
}
private void drawAnimation(){
this.em = new ElevatorMovement(12);
this.ea = this.em.getElevatorAnimation();
this.scrollPane1.add(ea);
}
/**
* Not working yet :/
* @param floor_count
*/
private void newAnimation(int floor_count){
this.scrollPane1.remove(this.ea);
this.em.setFloorsCount(floor_count);
this.ea = this.em.getElevatorAnimation();
this.scrollPane1.add(this.ea);
}
@Action
@ -104,7 +126,6 @@ public class WindaView extends FrameView {
private void initComponents() {
mainPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
@ -148,6 +169,7 @@ public class WindaView extends FrameView {
jLabel12 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel13 = new javax.swing.JLabel();
scrollPane1 = new java.awt.ScrollPane();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
@ -161,8 +183,6 @@ public class WindaView extends FrameView {
mainPanel.setName("mainPanel"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(winda.gui.WindaApp.class).getContext().getResourceMap(WindaView.class);
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
@ -546,6 +566,8 @@ public class WindaView extends FrameView {
.addContainerGap(210, Short.MAX_VALUE))
);
scrollPane1.setName("scrollPane1"); // NOI18N
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
@ -564,17 +586,17 @@ public class WindaView extends FrameView {
.addComponent(jButton2)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addGap(28, 28, 28)
.addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
.addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))
.addGroup(mainPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 520, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(scrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 551, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
@ -614,8 +636,9 @@ public class WindaView extends FrameView {
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 433, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGap(10, 10, 10)
.addComponent(scrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 431, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
@ -784,7 +807,6 @@ public class WindaView extends FrameView {
private javax.swing.JPanel jPanel5;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
@ -803,6 +825,7 @@ public class WindaView extends FrameView {
private javax.swing.JTextField jTextField1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private java.awt.ScrollPane scrollPane1;
// End of variables declaration//GEN-END:variables
private final Timer messageTimer;

View file

@ -6,6 +6,7 @@
package winda.logic;
import java.util.ArrayList;
import java.util.List;
import sun.misc.Sort;
/**
@ -87,4 +88,8 @@ public class AlgorytmGoraDol implements IAlgorytm {
public void SetMaxPietro(int maxPietro) {
this.setFloorCount(maxPietro);
}
public List<Pietro> Trasa(List<Pasazer> pasazerowie) {
throw new UnsupportedOperationException("Not supported yet.");
}
}