I need this to not let the circle leave the bounds of the JFrame. I added the if
ID: 3804616 • Letter: I
Question
I need this to not let the circle leave the bounds of the JFrame. I added the if statement that should keep them within the bound but they are not working.
If someone could help me understand why this isn't doing what it is supposed to it would be greatly appreciated
// ******************************************************************
// 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;
int lol = -20;
int ror = 20;
int uou= -20;
int dod =20;
int BpanelHIGHT;
//---------------------------------------------------------------
// 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);
if (x < 0 ){
lol = 0;
}
else if (x > width )
{
ror = 0;
}
else if (y > height )
{
uou = 0;
}
else if (y > BpanelHIGHT )
{
dod = 0;
}
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");
left.setMnemonic('l');
left.setToolTipText("move left 20 pixles.");
JButton right = new JButton("Right");
right.setMnemonic('r');
right.setToolTipText("moves circle right 20 pixles");
JButton up = new JButton("Up");
up.setMnemonic('u');
up.setToolTipText("moves circle up 20 pixles");
JButton down = new JButton("Down");
down.setMnemonic('d');
down.setToolTipText("moves circle down 20 pixles");
// Add listeners to the buttons
left.addActionListener(new MoveListener(lol, 0));
right.addActionListener(new MoveListener(ror, 0));
up.addActionListener(new MoveListener(0, uou));
down.addActionListener(new MoveListener(0, dod));
// 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");
BpanelHIGHT = buttonPanel.HEIGHT;
}
//----------------------------------------------------------------
// 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;
repaint();
}
}
}
**other class:**
// ***************************************************************
// 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);
}
}
Explanation / Answer
Solution: See updated code below:
1. Updated CirclePanel class:
------------------------------------------------
// ******************************************************************
// 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.*;
import java.awt.image.ImageObserver;
public class CirclePanel extends JPanel {
/**
* serial version id
*/
private static final long serialVersionUID = 1L;
private final int CIRCLE_SIZE = 50;
private int x, y;
private int width,height;
private Color c;
int lol = -20;
int ror = 20;
int uou = -20;
int dod = 20;
int BpanelHIGHT;
// ---------------------------------------------------------------
// Set up circle and buttons to move it.
// ---------------------------------------------------------------
public CirclePanel(int width, int height) {
this.width=width;
this.height=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");
left.setMnemonic('l');
left.setToolTipText("move left 20 pixles.");
JButton right = new JButton("Right");
right.setMnemonic('r');
right.setToolTipText("moves circle right 20 pixles");
JButton up = new JButton("Up");
up.setMnemonic('u');
up.setToolTipText("moves circle up 20 pixles");
JButton down = new JButton("Down");
down.setMnemonic('d');
down.setToolTipText("moves circle down 20 pixles");
// Add listeners to the buttons
left.addActionListener(new MoveListener(lol, 0));
right.addActionListener(new MoveListener(ror, 0));
up.addActionListener(new MoveListener(0, uou));
down.addActionListener(new MoveListener(0, dod));
// 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");
BpanelHIGHT = ImageObserver.HEIGHT;
}
// ----------------------------------------------------------------
// 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;
//check whether circle has reached boundaries.
if (x < 0) {
x=0;
} else if (x > height) {
x=height;
} else if (y < 0) {
y=0;
} else if (y > width/2) {
y=width/2;
}
repaint();
}
}
}
------------------------------------
2. Updated MoveCircle class:
-----------------------------------
// ***************************************************************
// MoveCircle.java
//
// Uses CirclePanel to display a GUI that lets the user move
// a circle by pressing buttons.
// ***************************************************************
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.setResizable(false);
frame.getContentPane().add(new CirclePanel(400, 300));
frame.setVisible(true);
}
}
--------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.