Question
I keep getting this error message and I do not know what it means or how to fix it: Exception in thread AWT-EventQueue-0 java.lang.NullPointerException at
I keep getting this error message and I do not know what it means or how to fix it:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at CirclePanel$MoveListener.actionPerformed(CirclePanel.java:104) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(Unknown Source) at javax.swing.SwingUtilities.notifyAction(Unknown Source) at javax.swing.JComponent.processKeyBinding(Unknown Source) at javax.swing.JComponent.processKeyBindings(Unknown Source) at javax.swing.JComponent.processKeyEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
This is my code associated with it and the assignment associated with it.
Assignment:
File MoveCircle contains a program that uses CirclePanel to draw a circle and let the user move it by pressing buttons. Save these files to your directory and compile and run MoveCircle to see how it works. Then modify the code in CirclePanel as follows:
1. Add mnemonics to the buttons so that the user can move the circle by pressing the ALT-l, ALT-r, ALT-u, or ALT-d keys.
2. Add tooltips to the buttons that indicate what happens when the button is pressed, including how far it is moved.
3. When the circle gets all the way to an edge, disable the corresponding button. When it moves back in, enable the button again. Note that you will need instance variables (instead of local variables in the constructor) to hold the buttons and the panel size to make them visible to the listener. Bonus: In most cases the circle wont hit the edge exactly; check for this (e.g., x<0) and adjust the coordinates so it does.
Deliverables:
CirclePanel.java
Code:
// ***************************************************************
// MoveCircle.java
//
// Uses CirclePanel to display a GUI that lets the user move
// a circle by pressing buttons.
// ***************************************************************
import java.awt.*;
import javax.swing.*;
public class MoveCircle
{
public static void main(String[] args)
{
JFrame frame = new JFrame("MoveCircle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.getContentPane().add(new CirclePanel(400,300));
frame.setVisible(true);
}
}
// ******************************************************************
// CirclePanel.java
//
// A panel with a circle drawn in the center and buttons on the
// bottom that move the circle.
// ******************************************************************
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CirclePanel extends JPanel
{
private final int CIRCLE_SIZE = 50;
private int x, y;
private Color c;
//Buttons are declared to be acessed
JButton left,right,up,down;
JPanel buttonPanel;
//---------------------------------------------------------------
// Set up circle and buttons to move it.
//---------------------------------------------------------------
public CirclePanel(int width, int height)
{
// Set coordinates so circle starts in middle
x = (width/2)-(CIRCLE_SIZE/2);
y = (height/2)-(CIRCLE_SIZE/2);
c = Color.green;
// Need a border layout to get the buttons on the bottom
this.setLayout(new BorderLayout());
// Create buttons to move the circle
JButton left = new JButton("Left");
JButton right = new JButton("Right");
JButton up = new JButton("Up");
JButton down = new JButton("Down");
// Add listeners to the buttons
left.addActionListener(new MoveListener(-20, 0));
right.addActionListener(new MoveListener(20, 0));
up.addActionListener(new MoveListener(0, -20));
down.addActionListener(new MoveListener(0, 20));
// Need a panel to put the buttons on or they'll be on
// top of each other.
JPanel buttonPanel = new JPanel();
buttonPanel.add(left);
buttonPanel.add(right);
buttonPanel.add(up);
buttonPanel.add(down);
// Add the button panel to the bottom of the main panel
this.add (buttonPanel, "South");
//adding mnumonics
left.setMnemonic(KeyEvent.VK_L);
right.setMnemonic(KeyEvent.VK_R);
up.setMnemonic(KeyEvent.VK_U);
down.setMnemonic(KeyEvent.VK_D);
//adding tooltips
left.setToolTipText("Moves circle left");
right.setToolTipText("Moves circle right");
up.setToolTipText("Moves circle up");
down.setToolTipText("Moves circle down");
}
//----------------------------------------------------------------
// Draw circle on CirclePanel
//----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(c);
page.fillOval(x,y, CIRCLE_SIZE, CIRCLE_SIZE);
}
//----------------------------------------------------------------
// Class to listen for button clicks that move circle.
//----------------------------------------------------------------
private class MoveListener implements ActionListener
{
private int dx;
private int dy;
//----------------------------------------------------------------
// Parameters tell how to move circle at click.
//----------------------------------------------------------------
public MoveListener(int dx, int dy)
{
this.dx = dx;
this.dy = dy;
}
//----------------------------------------------------------------
// Change x and y coordinates and repaint.
//----------------------------------------------------------------
public void actionPerformed(ActionEvent e)
{
x += dx;
y += dy;
//stops buttons when circle hits the border
if(x<0){
left.setEnabled(true);
}
if(y>0){
up.setEnabled(true);
}
if(x>getWidth()){
right.setEnabled(true);
}
if(y>(getHeight()-buttonPanel.getHeight())){
down.setEnabled(true);
}
repaint();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started