Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:54:04+00:00 2026-06-13T05:54:04+00:00

Hi I am new to GUI and Canvas in Java. I am working on

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:54:05+00:00Added an answer on June 13, 2026 at 5:54 am

    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…)

    enter image description here

    public class Elevator {
    
    public static void main(String[] args) {
            new Elevator();
        }
    
        public Elevator() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new BuildingPane());
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    
        public enum ElevatorShaft {
            Left,
            Right
        }
    
        public interface ElevatorModel {
            public int getFloor();
            public void setFloor(int floor);
    
            public void addPropertyChangeListener(PropertyChangeListener listener);
            public void removePropertyChangeListener(PropertyChangeListener listener);
        }
    
        public class DefaultElevatorModel implements ElevatorModel {
    
            private int floor;
            private PropertyChangeSupport propertyChangeSupport;
    
            public DefaultElevatorModel(int floor) {
                this.floor = floor;
                propertyChangeSupport = new PropertyChangeSupport(this);
            }
    
            @Override
            public void addPropertyChangeListener(PropertyChangeListener listener) {
                propertyChangeSupport.addPropertyChangeListener(listener);
            }
    
            @Override
            public void removePropertyChangeListener(PropertyChangeListener listener) {
                propertyChangeSupport.removePropertyChangeListener(listener);
            }
    
            @Override
            public int getFloor() {
                return floor;
            }
    
            @Override
            public void setFloor(int value) {
                if (value != floor) {
                    int old = floor;
                    floor = value;
                    propertyChangeSupport.firePropertyChange("floor", old, floor);
                }
            }
    
        }
    
        public interface BuildingModel {
    
            public int getFloorCount();
            public int getFloor(ElevatorShaft shaft);
            public ElevatorModel getElevatorModel(ElevatorShaft shaft);
            public void call(int floor);
    
        }
    
        public class DefaultBuildingModel implements BuildingModel {
    
            private int floorCount;
    
            private Map<ElevatorShaft, ElevatorModel> shaftManager;
    
            public DefaultBuildingModel(int floorCount) {
                this.floorCount = floorCount;
                shaftManager = new HashMap<ElevatorShaft, ElevatorModel>(2);
                shaftManager.put(ElevatorShaft.Left, new DefaultElevatorModel((int)Math.round(Math.random() * (floorCount - 1))));
                shaftManager.put(ElevatorShaft.Right, new DefaultElevatorModel((int)Math.round(Math.random() * (floorCount - 1))));
            }
    
            @Override
            public ElevatorModel getElevatorModel(ElevatorShaft shaft) {
                return shaftManager.get(shaft);
            }
    
            @Override
            public int getFloorCount() {
                return floorCount;
            }
    
            @Override
            public int getFloor(ElevatorShaft shaft) {
                return shaftManager.get(shaft).getFloor();
            }
    
            @Override
            public void call(int floor) {
                // This will need to determine which elevator should be called
                // and call that elevators model "setFloor" method...
            }
    
        }
    
        public class BuildingPane extends JPanel {
    
            public BuildingPane() {
    
                setLayout(new GridBagLayout());
    
                BuildingModel model = new DefaultBuildingModel(3);
    
                Shaft leftShaft = new Shaft(model, ElevatorShaft.Left);
                Shaft rightShaft = new Shaft(model, ElevatorShaft.Right);
                ButtonsPane buttonsPane = new ButtonsPane(model);
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.fill = GridBagConstraints.VERTICAL;
    
                add(leftShaft, gbc);
                gbc.gridx++;
                add(buttonsPane, gbc);
                gbc.gridx++;
                add(rightShaft, gbc);
    
            }
    
        }
    
        public class ButtonsPane extends JPanel {
    
            private BuildingModel model;
    
            public ButtonsPane(BuildingModel model) {
    
                this.model = model;
    
                setLayout(new GridBagLayout());
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.weighty = 1;
                gbc.anchor = GridBagConstraints.CENTER;
    
                int floorCount = model.getFloorCount();
                ActionHandler handler = new ActionHandler();
    
                for (int floor = floorCount; floor > 0; floor--) {
    
                    JButton button = new JButton("Floor " + floor);
                    button.setActionCommand(Integer.toString(floor));
                    button.addActionListener(handler);
                    add(button, gbc);
    
                    gbc.gridy++;
    
                }
    
            }
    
            public BuildingModel getModel() {
                return model;
            }
    
            public class ActionHandler implements ActionListener {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String cmd = e.getActionCommand();
                    try {
                        int floor = Integer.parseInt(cmd);
                        getModel().call(floor);
                    } catch (NumberFormatException exp) {
                        exp.printStackTrace();
                    }
                }
    
            }
    
        }
    
        public class Shaft extends JPanel {
    
            private BufferedImage elevator;
            private BuildingModel buildingModel;
            private ElevatorShaft shaft;
    
            public Shaft(BuildingModel model, ElevatorShaft shaft) {
    
                this.buildingModel = model;
                this.shaft = shaft;
    
                buildingModel.getElevatorModel(shaft).addPropertyChangeListener(new PropertyChangeListener() {
    
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        if (evt.getPropertyName().equalsIgnoreCase("floor")) {
                            // Need to update our position, this could
                            // be done via animation or directly call something
                            // like repaint..
                        }
                    }
                });
    
                setBorder(new LineBorder(Color.DARK_GRAY));
                setBackground(Color.GRAY);
    
                try {
                    elevator = ImageIO.read(new File("elevator.png"));
                } catch (IOException ex) {
                    Logger.getLogger(Elevator.class.getName()).log(Level.SEVERE, null, ex);
                }
    
            }
    
            public BuildingModel getBuildingModel() {
                return buildingModel;
            }
    
            public ElevatorShaft getShaft() {
                return shaft;
            }
    
            public int getFloorCount() {
                return getBuildingModel().getFloorCount();
            }
    
            public int getFloor() {
                return getBuildingModel().getFloor(getShaft());
            }
    
            @Override
            public Dimension getPreferredSize() {
    
                Dimension size = new Dimension(elevator.getWidth(), elevator.getHeight() * getFloorCount());
                Insets insets = getInsets();
    
                size.width += (insets.left + insets.right);
                size.height += (insets.top + insets.bottom);
    
                return size;
    
            }
    
            @Override
            protected void paintComponent(Graphics g) {
    
                super.paintComponent(g);
    
                Insets insets = getInsets();
                int x = insets.left;
                int y = insets.top;
                int height = getHeight() - (insets.top + insets.bottom);
    
                int floor = getFloor();
                y = height - (elevator.getHeight() * (floor + 1));
                g.drawImage(elevator, x, y, this);
    
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a java file that has buttons to create new GUI windows.
I have a non-GUI thread that starts a JFrame using java.awt.EventQueue.invokeLater(new Runnable() { public
I'm new to GUI programming, but need to create a multiple window GUI. Does
I'm relatively new to coding; most of my work has been just simple GUI
I am working on a project where I need to have a buttonPanel in
Redmond has a good idea occasionally: The next-gen Windows will come with a new
I am working on a simple GUI app that just draws some graphics on
I need some basic stuff for working with the GUI library in Racket. How
I've just upgraded to TortoiseHG 2.0 (which has a brand new GUI) on a
I have a small problem, i started a new GUI project using MigLayout and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.