Hi I am new to GUI and Canvas in Java. I am working on a project where I will need a GUI/Canvas (still confused on the difference) that has three frames I guess. Basically it is an elevator project where on either side of the canvas there is a rectangular elevator object, and in the middle are buttons (stacked on top of each other) that are used to represent floors (so if you click on the button, the elevator moves to the same row as the button). I am stuck on how you would design the canvas for this. I have had some ideas regarding gridLayout and broderLayouts, but it is all a jumbled mess right now.
thanks
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class UI extends JFrame implements ActionListener
{
ArrayList<JButton> buttonList = new ArrayList();
MyCanvas mainCanvas;
public UI()
{
super("Example Frame");
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
mainCanvas = new MyCanvas();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1,3));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(12,1));
//while(true)
//{
// myCanvas.repaint();
//}
for(int i=1; i<13; i++)
{ String s = Integer.toString(i);
buttonPanel.add(new JButton(s));
}
add(mainPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
}
This is a loaded question.
If it’s purely about layouts, then I’d use a series of compound components with there own layouts to achieve the result you need…
If it’s about approaches, then it gets more complicated…
Basically, you want to seperate the areas of responsibilities.
From the question, there are two distinct models, a elevator model and a building model.
The elevator model controls where the elevator for an individual shaft is where as the building model controls things like, the number of floors, the individual elevator models, the algorithm to determine how an elevator is called to a floor…
The following is REALLY basic example of an idea. It’s missing (amongst other things) the logic needed to call an elevator to a floor or stateful information about the elevators (moving, waiting, open…)