This must be a kind of elevator. If I click a button ( on the left or on the right side of the GUI ) the rectangle must move floor by floor with a certain delay. My problem is that my GUI simply refuse to display sequentially; instead, it display all the "floors" in the same time. :(
//this is the applet
import javax.swing.JApplet;
@SuppressWarnings("serial")
public class applet extends JApplet{
public applet(){
add(new elevator());
}}
//this is the main class
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.LineBorder;
@SuppressWarnings("serial")
public class elevator extends JPanel implements ActionListener{
private JButton[] externalButtons=new JButton[11];
private JButton[] internalButtons=new JButton[13];
private JLabel[] floors=new JLabel[11];
private JLabel o1=new JLabel();
private JLabel o2=new JLabel();
private JPanel p1=new JPanel();
private JPanel p2=new JPanel();
private JPanel p3=new JPanel();
private JLabel[] labelBlueLeft=new JLabel[11];
private JLabel[] labelBlueRight=new JLabel[11];
private JLabel[] labelRedLeft=new JLabel[11];
private JLabel[] labelRedRight=new JLabel[11];
private int firstOption=0, lastOption;
public elevator(){
repaint();
p1.setLayout(new GridLayout(11,1));
p2.setLayout(new GridLayout(11,1));
p3.setLayout(new GridLayout(15,1));
for(int i=1; i<11;i++){
externalButtons[i]=new JButton("Floor " + i);
externalButtons[i].addActionListener(this);
}
externalButtons[0]=new JButton("Ground");
externalButtons[0].addActionListener(this);
for(int i=3; i<13; i++){
internalButtons[i]=new JButton("Floor " + (i-2));
internalButtons[i].addActionListener(this);
}
internalButtons[0]=new JButton("Alarm!");
internalButtons[1]=new JButton("Stop");
internalButtons[2]=new JButton("Ground");
internalButtons[0].addActionListener(this);
internalButtons[1].addActionListener(this);
internalButtons[2].addActionListener(this);
for(int i=0; i<11;i++){
floors[i]=new JLabel("");
floors[i].setLayout( new GridLayout());
floors[i].setBorder(new LineBorder(new Color(150,150,150)));
floors[i].setPreferredSize(new Dimension(200,25));
if((i%2)==0) {
floors[i].setBackground(Color.lightGray);
floors[i].setOpaque(true);
}
labelBlueLeft[i]=new CustomLabel(25,0,35,25,0,0);
labelBlueRight[i]=new CustomLabel(25,0,35,25,0,1);
labelRedLeft[i]=new CustomLabel(25,0,35,25,1,0);
labelRedRight[i]=new CustomLabel(25,0,35,25,1,1);
}
floors[firstOption].add(labelBlueLeft[0]);
for(int i=10; i>-1; i--){
p1.add(externalButtons[i]);
p2.add(floors[i]);
}
p3.add(o1); p3.add(o2);
for(int i=12; i>-1; i--)
p3.add(internalButtons[i]);
add(p1, BorderLayout.WEST);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.EAST);
}
//ACTION PERFORMED
public void actionPerformed(ActionEvent e){
for(int i=0; i<11; i++){
if(e.getSource()==externalButtons[i]) lastOption=i;
}
for(int i=2; i<13; i++){
if(e.getSource()==internalButtons[i]) lastOption=i-2;
}
floors[firstOption].remove(labelBlueLeft[0]);
if(firstOption<lastOption){
for(int i=firstOption; i<lastOption+1; i++){
floors[i].add(labelRedLeft[i]);
floors[i].validate();
floors[i].repaint();
}
}
else if(firstOption>lastOption){
for(int i=firstOption; i>lastOption-1; i--){
floors[i].add(labelRedLeft[i]);
floors[i].revalidate();
floors[i].repaint();
}}
firstOption=lastOption;
}
}
//this is my CustomLabel class
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class CustomLabel extends JLabel{
private int x=25, y=0, width=35, height=20;
public CustomLabel(int x1, int y1, int width1, int height1, int clr, int ind){
if(ind==1){x1=150; y1=0;}
if(clr==1)
{setForeground(Color.red);}
else
{setForeground(Color.blue);}
x=x1; y=y1;
width=width1;
height=height1;
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(x, y, width, height);
repaint();
}
}