Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need the circle NOT to go beyond bottom edge!! Thanks // ****************************************************************** // CirclePanel.java // // A panel with a circle drawn in the

I need the circle NOT to go beyond bottom edge!! Thanks

// ******************************************************************

// 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;

private JButton left, right, up, down;

//---------------------------------------------------------------

// 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.red;

// Need a border layout to get the buttons on the bottom

this.setLayout(new BorderLayout());

// Create buttons to move the circle

left = new JButton("Left");

right = new JButton("Right");

up = new JButton("Up");

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

left.setMnemonic('l');

right.setMnemonic('r');

down.setMnemonic('d');

up.setMnemonic('u');

left.setToolTipText("Moves the circle 20 pixels to the left");

right.setToolTipText("Moves the circle 20 pixels to the right");

up.setToolTipText("Moves the circle 20 pixels up");

down.setToolTipText("Moves the circle 20 pixels down");

// 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");

}

//----------------------------------------------------------------

// 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;

if(x < 0)

{

x = 0;

left.setEnabled(false);

}

else if(x + CIRCLE_SIZE > getWidth())

{

x = getWidth() - CIRCLE_SIZE;

right.setEnabled(false);

}

else

{

left.setEnabled(true);

right.setEnabled(true);

}

if(y <0)

{

y = 0;

up.setEnabled(false);

}

else if(y + CIRCLE_SIZE > getHeight())

{

y = getHeight() - CIRCLE_SIZE ;

down.setEnabled(false);

}

else

{

up.setEnabled(true);

down.setEnabled(true);

}

repaint();

}

}

}

// ***************************************************************

// 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);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

Students also viewed these Databases questions

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago

Question

LO6 Describe how individual pay rates are set.

Answered: 1 week ago