Skip to content
Snippets Groups Projects
Commit 908b629f authored by NGUYEN Duc duong's avatar NGUYEN Duc duong
Browse files

create incomplete GUI

parent eedd0c98
Branches
No related tags found
No related merge requests found
Pipeline #18677 pending
package elevator.GUI;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Building extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Timer timer;
private boolean isRunning;
private int counter;
private int timeElapsedInSecs;
private Elevator elevator1;
private Elevator elevator2;
private List<Floor> floors;
private Random random;
public Building(int refreshRate)
{
this.isRunning = false;
this.counter = 0;
this.timeElapsedInSecs = 0;
elevator1 = new Elevator(100, 400);
elevator2 = new Elevator(250, 400);
floors = Collections.synchronizedList(new ArrayList());
for (int i = 0; i < 5; i++)
{
floors.add(new Floor(100, 200, 250, 350, 100 * (5 - i), 750, i));
}
random = new Random();
timer = new Timer(refreshRate, (e) -> {
counter += refreshRate;
if (counter / 1000 == 1)
{
counter = 0;
++timeElapsedInSecs;
}
if (isRunning)
{
repaint();
}
});
timer.start();
}
public Building()
{
this(10);
}
public void start()
{
if (!isRunning)
{
isRunning = true;
}
}
public void stop()
{
if (isRunning)
{
isRunning = false;
}
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// this.setBackground(Color.WHITE);
for (Floor floor : floors)
{
floor.draw(g);
}
elevator1.draw(g);
}
}
package elevator.GUI;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Elevator
{
private int xAxis;
private int yAxis;
private int width;
private int height;
private int doorWidth;
private int floor;
public Elevator(int xAxis, int yAxis)
{
this(xAxis, yAxis, 100, 100, 0);
}
public Elevator(int xAxis, int yAxis, int width, int height, int floor)
{
this.xAxis = xAxis;
this.yAxis = yAxis;
this.width = width;
this.height = height;
this.doorWidth = width / 2;
this.floor = floor;
}
public void draw(Graphics g)
{
g.drawRect(xAxis, yAxis, width, height);
g.setColor(Color.WHITE);
g.fillRect(xAxis, yAxis, doorWidth, height);
g.fillRect(xAxis + width - doorWidth, yAxis, doorWidth, height);
g.setColor(Color.BLACK);
g.drawRect(xAxis, yAxis, doorWidth, height);
g.drawRect(xAxis + width - doorWidth, yAxis, doorWidth, height);
}
public int getFloor()
{
return this.floor;
}
public int getX()
{
return this.xAxis;
}
public int getY()
{
return this.yAxis;
}
}
\ No newline at end of file
package elevator.GUI;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Floor
{
private int x1;
private int x2;
private int x3;
private int x4;
private int y;
private int l;
private int floor;
public Floor(int x1, int x2, int x3, int x4, int y, int l, int floor)
{
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.x4 = x4;
this.y = y;
this.l = l;
this.floor = floor;
}
public void draw(Graphics g)
{
g.drawLine(0, y, x1, y);
g.drawLine(x2, y, x3, y);
g.drawLine(x4, y, x3,y);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(222, y - 50, 8, 10);
// g.fillRect(372, y - 50, 8, 10);
g.drawLine(x1, y, x2, y);
// g.drawLine(x3, y, x4, y);
g.setColor(Color.BLACK);
g.drawString("Etage " + floor, 30, y - 45);
}
public int getFloor()
{
return this.floor;
}
}
\ No newline at end of file
package elevator.GUI;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main
{
private final JFrame frame;
private final JPanel panel1;
private final JPanel panel2;
private final JButton btnStart;
private final JButton btnStop;
private Building building;
public Main()
{
building = new Building();
building.setBounds(0, 0, 750, 500);
btnStart = new JButton("Start");
btnStart.setBounds(290, 512, 80, 25);
btnStart.setFocusPainted(false);
btnStart.addActionListener((e) -> {
building.start();
System.out.println("pressed start");
});
btnStop = new JButton("Stop");
btnStop.setBounds(380, 512, 80, 25);
btnStop.setFocusPainted(false);
btnStop.addActionListener((e) -> {
building.stop();
System.out.println("pressed stop");
});
panel1 = new JPanel(null);
//w=750
panel1.setPreferredSize(new Dimension(750, 550));
panel1.add(building);
panel1.add(btnStart);
panel1.add(btnStop);
//test
// Tạo panel con 2 và đặt vị trí của nó
panel2 = new JPanel();
panel2.setBackground(Color.BLUE);
panel2.setBounds(150, 150, 100, 100); // Đặt vị trí và kích thước của panel2
panel1.add(panel2);
frame = new JFrame("Elevator Simulator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setResizable(false);
frame.add(panel1);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
Main main = new Main();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment