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 7408749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:58:04+00:00 2026-05-29T05:58:04+00:00

A summary of my homework assignment was to create a display and a GUI

  • 0

A summary of my homework assignment was to create a display and a GUI to interact with that display allowing users to add shapes and move them around along with a few other specifications. I completed that assignment, and added some extra features.

One of the features was to assign hot keys to each of the buttons. Once I got that functional, I then decided to create a short video tutorial explaining the features of the program. I attached the URL to a button and placed it on the screen. Currently it’s just pointing to Google.

The problem is, once I got that button functional, I lost the hot key utility that I previously implemented. I can’t figure out why I would lose the hot key utility.

Below is the somewhat lengthy code for the controller class. If more (or less) information is needed, please let me know.

Here is a link to what the display looks like: https://i.stack.imgur.com/3fPag.png

I appreciate any advice possible.

public class DrawingBoardControl extends JPanel {

private DrawingBoardView viewPane;
private DrawingBoardTextView textPane;
private DrawingBoard board;
private BoardButtonListener buttonListener;
private BoardMouseListener mouseListener;
private BoardKeyListener keyListener;
private int desiredShape;
private JLabel count = new JLabel();
private JLabel selectedShape = new JLabel();
private final URI uri;



/** Construct a viewer/controller for the given DrawingBoard
* @param board The DrawingBoard object to be controlled and viewed.
*/
public DrawingBoardControl(DrawingBoard b) throws URISyntaxException{ 

// Initializes panel with no shape selected    
desiredShape = 3;

// URL for Instruction Video
uri = new URI("http://google.com");

// model
board = b;

// layout
setLayout(new BorderLayout());

// create panel
viewPane = new DrawingBoardView(board);
viewPane.setPreferredSize(new Dimension(board.getWidth(),board.getHeight()));
viewPane.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
viewPane.setBackground(Color.darkGray);
add(viewPane, BorderLayout.CENTER);

textPane = new DrawingBoardTextView(board);

// register view with model
board.addViewer(viewPane);
board.addViewer(textPane);

// create bottom panel
JButton circle = new JButton("(C)ircle");
JButton delta = new JButton("(D)elta");
JButton tee = new JButton("(T)ee");
JButton edit = new JButton("(E)dit Mode");
JButton reset = new JButton("(R)eset");
count.setText("Total shapes: " + board.getShapeCount());
count.setForeground(Color.white);
selectedShape.setText("Currently there is no selected Shape.");
selectedShape.setForeground(Color.white);
JPanel buttons = new JPanel();
buttons.add(selectedShape);
buttons.add(circle);
buttons.add(delta);
buttons.add(tee);
buttons.add(edit);
buttons.add(reset);
buttons.add(count);    
circle.setBackground(Color.cyan);
delta.setBackground(Color.red);
tee.setBackground(Color.green);
edit.setBackground(Color.yellow);
reset.setBackground(Color.white);
buttons.setBackground(Color.black);
this.add(buttons, BorderLayout.SOUTH);

// create top panel
JButton tutorial = new JButton();
tutorial.setText("Click here to view a web tutorial explaining the "
                                 + "options available in this program.");
tutorial.setToolTipText(uri.toString());
tutorial.addActionListener(new OpenUrlAction());
tutorial.setBackground(Color.black);
tutorial.setForeground(Color.white);
add(tutorial, BorderLayout.NORTH);

// setup listeners
buttonListener = new BoardButtonListener(board);
circle.addActionListener(buttonListener);
delta.addActionListener(buttonListener);
tee.addActionListener(buttonListener);
edit.addActionListener(buttonListener);
reset.addActionListener(buttonListener);

mouseListener = new BoardMouseListener(board);
viewPane.addMouseListener(mouseListener);

// I don't fully understand why, but all three of these are needed.
keyListener = new BoardKeyListener(board);
viewPane.addKeyListener(keyListener);
circle.addKeyListener(keyListener);
}

/**
* Handle button clicks for the DrawingBoardControl window.
* 
* @author Justin Ashburn
* @version 2/4/2012
*/  
class BoardButtonListener implements ActionListener {

private DrawingBoard board;

/**Constructor for objects of class BoardButton.
  * @param board The model object of the DrawingBoard.
  */

public BoardButtonListener(DrawingBoard board) {
  this.board = board;
}

/** Process button clicks by choosing the shape to be created.
  * @param The button click event.
  */
public void actionPerformed(ActionEvent e) {
  count.setText("Total shapes: " + board.getShapeCount());
  if(e.getActionCommand().equals("(C)ircle")) {
    desiredShape = 0;
  }
  else if(e.getActionCommand().equals("(D)elta")) {
    desiredShape = 1;
  }
  else if (e.getActionCommand().equals("(T)ee")){
    desiredShape = 2;
  }
  else if (e.getActionCommand().equals("(E)dit Mode")){
    desiredShape = 3;
    if (board.getHasSelected() == true){
      board.setHasSelected(false);
    }
  }
  else if (e.getActionCommand().equals("(R)eset")){
    desiredShape =4;
    for (int k = board.getShapeCount()-1; k >= 0; k--) {
      board.setHasSelected(true);
      board.removeShape();
      count.setText("Total shapes: " + board.getShapeCount());
    }
  }

  if (board.getHasSelected()) {
    selectedShape.setText(board.getSelectedShape().toString());
  }
  else
    selectedShape.setText("Currently there is no selected Shape.");
 }
 }


 /**
 * Handle mouse clicks for the DrawingBoardControl window.
 * 
 * @author Justin Ashburn
 * @version 2/4/2012
 */
 class BoardMouseListener implements MouseListener, MouseMotionListener {

 private DrawingBoard board;

 /**
 * Constructor for objects of class BoardMouseListener.
 * @param board The model object containing the state of the DrawingBoard.
 */
 public BoardMouseListener(DrawingBoard board) {
  this.board = board;    
 }

 /**
 * Process mouse press by adding a designated Shape 
 * to the DrawingBoard at the location of the click.
 * @param e The mouse press event.
 */    
 public void mousePressed (MouseEvent e) {
  if (e.getModifiers()==16) {
    if (desiredShape == 0) {
      Circle c = new Circle(e.getX(), e.getY(), 100);
      c.setColor(Color.cyan);          
      board.addShape(c);
    }
    else if (desiredShape == 1) {
      Delta d = new Delta(e.getX(), e.getY(), 100);
      d.setColor(Color.red);
      board.addShape(d);
    }
    else if (desiredShape == 2) {
      Tee t = new Tee(e.getX(), e.getY(), 100);
      t.setColor(Color.green);
      board.addShape(t);
    }
    else if (desiredShape == 3) {
      if (board.getShapeCount() > 0)
        board.selectShape(e.getX(),e.getY());
    }
  }
  else if(e.getModifiers()==4) {
    if (desiredShape == 3) {
      if (board.getShapeCount() > 0) 
        board.selectShape(e.getX(),e.getY());
    }
  }
  count.setText("Total shapes: " + board.getShapeCount());
  if (board.getHasSelected()) {
    selectedShape.setText(board.getSelectedShape().toString());
  }
  else
    selectedShape.setText("Currently there is no selected Shape.");
}

/**
* Process mouse release by adding a designated Shape 
* to the DrawingBoard at the location of the click.
* @param e The mouse release event.
*/
public void mouseReleased(MouseEvent e) {
  if (board.getHasSelected()) {
    if (e.getModifiers()==16) {
      if (desiredShape == 3) {
        board.moveShape(e.getX() - board.getSelectedShape().getX(), 
                        e.getY() - board.getSelectedShape().getY());
        board.setHasSelected(false);
      }
    }        
    else if (e.getModifiers()==4) {
      if (desiredShape >= 0 && desiredShape <= 2) {
        board.removeShape();
      }
      if (desiredShape == 3) {
        board.removeShape();
        if (board.getHasSelected()) {
          board.setHasSelected(false);
        }
      }
    }
  }
  count.setText("Total shapes: " + board.getShapeCount());
  if (board.getHasSelected()) {
    selectedShape.setText(board.getSelectedShape().toString());
  }
  else
    selectedShape.setText("Currently there is no selected Shape.");
}

// dummy implementations for other events in mouselistener
public void mouseDragged (MouseEvent e) { }
public void mouseClicked (MouseEvent e) { }
public void mouseEntered (MouseEvent e) { }
public void mouseExited  (MouseEvent e) { }
public void mouseMoved   (MouseEvent e) { }
}

/**
* Handle keyboard entries for the DrawingBoardControl window.
* 
* @author Justin Ashburn
* @version 2/4/2012
*/

class BoardKeyListener implements KeyListener {

private DrawingBoard board;

/**Constructor for objects of class BoardKey.
  * @param board The model object of the DrawingBoard.
  */    
public BoardKeyListener(DrawingBoard board) {
  this.board = board;
}

/** Process key presses by choosing the shape to be created.
  * @param The key press event.
  */
public void keyPressed(KeyEvent e) {
  switch(e.getKeyCode()) {
    case KeyEvent.VK_C:
      desiredShape = 0;
      break;
    case KeyEvent.VK_D:
      desiredShape = 1;
      break;
    case KeyEvent.VK_T:
      desiredShape = 2;
      break;
    case KeyEvent.VK_E:
      desiredShape = 3;
      if (board.getHasSelected() == true){
        board.setHasSelected(false);          
        break;
      }
    case KeyEvent.VK_R:
      desiredShape = 4;
      for (int k = board.getShapeCount()-1; k >= 0; k--) {
        board.setHasSelected(true);
        board.removeShape();
        count.setText("Total shapes: " + board.getShapeCount());
      }
      if (board.getHasSelected()) {
        selectedShape.setText(board.getSelectedShape().toString());
      }
      else
        selectedShape.setText("Currently there is no selected Shape.");
  }
}
//dummy implementations for keyboard listener
public void keyReleased(KeyEvent e) { }
public void keyTyped  (KeyEvent e) { }
}

/**
* Handle URL actions for the DrawingBoardControl window.
* 
* @author Justin Ashburn
* @version 2/4/2012
*/
class OpenUrlAction implements ActionListener {

/** Process URL events.
  * @param The URL event.
  */
public void actionPerformed(ActionEvent e) {
  open(uri);
}
public void open(URI uri) {
  if (Desktop.isDesktopSupported()) {
    try {
      Desktop.getDesktop().browse(uri);
    } catch (IOException e) { /* TODO: error handling */ }
  } else { /* TODO: error handling */ }
}
}
}
  • 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-05-29T05:58:05+00:00Added an answer on May 29, 2026 at 5:58 am

    From what you posted, the only possible problem i see is that maybe no part of your application which has a keyListener registered is currently focussed when you pressed the URI Button.

    viewPane.addKeyListener(keyListener);
    circle.addKeyListener(keyListener);
    

    So if neither viewPane nor the circle has focus when you push a key, the key Event won’t fire. So you possibly got to make sure they have the focus again. That’s the only explanation i would have for this behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary We have an ASP.NET application that allows users to query a SQL Server
Summary I'm repurposing Apple's Page Control project. In loadScrollViewWithPage:, the view controllers that I'm
Summary: ASP.Net website with a couple hundred users. Data is exported to Excel files
Summary : I am looking a fast XML parser (most likely a wrapper around
Summary: DataAnnotation's automatic handling of an int? is making me rethink using them at
I'm writing an HTTP server for a homework assignment, and I'm trying to make
Summary: I am having trouble connecting to an exchange 2007 mailbox that is running
Summary I recently had a conversation with the creator of a framework that one
Summary: I want to to create a templateField resulting from a database after calling
Summary: I have a web app that executes workflows on business objects and sometimes

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.