When I try to add a second instance of an elevator class, the first one disappears. How can I rearrange this code in order to see both objects?
Here is my main class:
public class MainClass extends JPanel implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Component component1 = frame.add(new elevator(0,0));
Component component2 = frame.add(new elevator(505,50));
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
And this is the elevator class:
public class elevator extends JPanel implements ActionListener {
Timer timer;
private double angle = 0;
private double scale = 1;
private double delta = 0.01;
private int x=0;
private int y=0;
Rectangle.Float r = new Rectangle.Float(0, 0, 3, 3);
public elevator(int input_x, int input_y) {
x=input_x;
y=input_y;
timer = new Timer(10, this);
timer.start();
}
public elevator() {
timer = new Timer(10, this);
timer.start();
}
public void paint(Graphics g) {
this.setSize(100,200);
this.setLocation(x, y);
int h = getHeight();
int w = getWidth();
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.translate(w / 5, scale*140);
g2d.scale(20, 20);
g2d.fill(r);
}
public void actionPerformed(ActionEvent e) {
if (scale < 0.01 || scale > 0.99) {
delta = -delta;
}
scale += delta;
angle += 0.01;
repaint();
}
}