Button to increase or decrease the number of the balls

Assignment Help JAVA Programming
Reference no: EM13167861

Simulates a bouncing ball. Extend to allow mulitple balls. You can use the +I or -I button to increase or decrease the number of the balls and use teh Suspend and Resume buttoms to freeze the balls or resume bouncing. For each ball, assign a random color.

I am having trouble getting my program to run. Here is what I came up with:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class BouncingBalls extends JApplet {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame();
JApplet applet = new BouncingBalls();

frame.add(applet);

frame.setTitle("BouncingBalls");

frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(3);

frame.setSize(400,200);
frame.setVisible(true);
}
public BouncingBalls()
{

add(new BallControl());
}
class BallControl extends JPanel implements ActionListener, AdjustmentListener
{
private Ball ball;
private JButton jbtSuspend;
private JButton jbtResume;
private JButton jbtAdd;
private JButton jbtSubtract;
private JScrollBar jsbDelay;

public BallControl()
{
ball = new Ball();

jbtSuspend = new JButton("Suspend");

jbtResume = new JButton("Resume");

jbtAdd = new JButton("+1");


jbtSubtract = new JButton("-1");
jsbDelay = new JScrollBar();
JPanel panel = new JPanel();

panel.add(jbtSuspend);

panel.add(jbtResume);

panel.add(jbtAdd);

panel.add(jbtSubtract);

ball.setBorder(new LineBorder(Color.red));
jsbDelay.setOrientation(0);
ball.setDelay(jsbDelay.getMaximum());
setLayout(new BorderLayout());
add(jsbDelay, "North");
add(ball, "Center");
add(panel, "South");
jbtSuspend.addActionListener(this);
jbtResume.addActionListener(this);
jbtAdd.addActionListener(this);
jbtSubtract.addActionListener(this);
jsbDelay.addAdjustmentListener(this);
}


class Ball extends JPanel implements Runnable
{

class BallState
{
int x;

int y;
int dx;
int dy;
Color color;

public BallState()
{
x = 0;
y = 0;
dx = 2;
dy = 2;

int r = (int)(Math.random() * 1000) % 256;
int g = (int)(Math.random() * 1000) % 256;
int b = (int)(Math.random() * 1000) % 256;

color = new Color(r,g,b);
}
}
public void add()
{
list.add(new BallState());
}
public void subtract()
{
if(list.size() > 0)
list.remove(list.size() - 1);
}
protected void paintComponent (Graphics g)
{
super.paintComponent(g);

for(int i = 0; i < list.size(); i++);
{
BallState ball;
ball = (BallState)list.get(i);
if(ball.x < radius) ball.dx = Math.abs(ball.dx);
if(ball.x > getWidth() - radius) ball.dx = -Math.abs(ball.dx);
if(ball.y < radius) ball.dy = Math.abs(ball.dy);
if(ball.y > getHeight() - radius) ball.dy = -Math.abs(ball.dy);

ball.x += ball.dx;
ball.y += ball.dy;
g.setColor(ball.color);
g.fillOval(ball.x - radius, ball.y - radius, radius*2, radius*2);
}
}
public void run()
{
try
{
while(true)
{
while(!isSuspended)
{
repaint();
Thread.sleep(speed);
}
lock.lock();
suspended.await();
lock.unlock();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}

public void resume()
{
lock.lock();
isSuspended = false;
suspended.signalAll();
lock.unlock();
}
public void suspend()
{
lock.lock();
isSuspended = true;
lock.unlock();
}

public void setDelay(int ms)
{
speed = ms;
}
private int speed;
protected Thread timer;
private ArrayList list;
private int x;
private int y;
private int radius ;
private int dx;
private int dy;
boolean isSuspended;
Lock lock;
Condition suspended;

public Ball()
{
speed = 10;
timer = new Thread(this);
list = new ArrayList();
x = 0;
y = 0;
radius = 5;
dx = 2;
dy = 2;
isSuspended = false;
lock = new ReentrantLock();
suspended = lock.newCondition();
timer.start();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbtSuspend) ball.suspend();
else
if(e.getSource() == jbtResume) ball.resume();
else
if(e.getSource() == jbtAdd) ball.add();
else

if(e.getSource() == jbtSubtract) ball.subtract();
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
ball.setDelay(jsbDelay.getMaximum() - e.getValue());
}
}

}

 

 

Reference no: EM13167861

Questions Cloud

Logic circuit to figure out how many of the bits : Design a logic circuit to figure out how many of the bits in a 6 bit unsigned number equal 1. Design the simplest possible circuit to accomplish this.
How much of that total is interest : How much do you need to invest today to have $500,000 30 years in the future and how much of that total is interest?
Prove that p(y|x=x)~bin(n-x,b/1-a) : Prove that p(y|X=x)~Bin(n-x,b/1-a) where x,y,z have multinomial distribution with parameters a,b,c and n.
Determine the patient body temperature in fahrenheit : A robust first grader goes for swimming lessons three times a week, and he enjoys it thoroughly. During the first week of December, he develops an upper respiratory infection, starts coughing continuously, and has a high fever of 39°C.
Button to increase or decrease the number of the balls : Simulates a bouncing ball. Extend to allow mulitple balls. You can use the +I or -I button to increase or decrease the number of the balls and use teh Suspend and Resume buttoms to freeze the balls or resume bouncing. For each ball, assign a random c..
Hierarchy chart and design the logic : Draw the hierarchy chart and design the logic for a program that calculates the projected cost of an automobile trip. Assume that the user's car travels 20 miles per gallon of gas. Design a program that prompts the user for a number of miles drive..
What is the density of the oil : One cup is equivalent to 237. mL. If 1 cup of oil has a mass of 210. g, what is the density of the oil (in grams per cubic centimeter)?
User that will be asked to input the time : Write a code for the following C++ problem. We will have a user that will be asked to input the time of a train traveling in terms of minutes and this minutes is a non-negative integer.
Integer programming formulation for problem : The objective is to minimize the sum of the completion times of all the jobs. Provide an integer programming formulation for this problem.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Develop parent lock method asks for four digit password

Develop the parent lock method (turns lock on/off) which asks for 4 digit password. Navigating to any channel which is in range 99-110 must ask for password when parent lock is on.

  Method that accepts a string object

Word Counter Write a method that accepts a String object as an argument and returns the number of words it contains. For instance, if the argument is "Four score and seven years ago" the method should return the number

  A method with the signature public static void

A method with the signature public static void printDetails(City[] cities) that will iterate through the cities and printthe details of the city using the displayDetails(...) method.

  The class constructor should accept an array

Write a class named TestScores. The class constructor should accept an array of test scores as its arguments. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 10..

  Write java code to read integers from an input file

write java code to read integers from an input file and write only the odd numbers to an output file. the two file names will be provided on the command line as the input file followed by the output file.

  Design an object-oriented java application

You are to design an object-oriented Java application to let the user play a dice game that uses two dices. The player bets on a value and the dices roll

  Implement/update specific methods for the dfs of a graph

implement/update specific methods for the DFS of a graph; for at least 2 graphs (1 being the provided one), show the DFS order of vertices in the graph, and for each node,

  Create to determine how much either joe or jim

What type of equation would you create to determine how much either Joe or Jim makes separately? What equation is needed in Java (ignoring the $ symbol)? What data type is needed need for this equation?

  Write method called median that accepts an array of integer

Write a method called median that accepts an array of integers as its parameter and returns the median of the numbers in the array.

  Create a class safestack that implements a stack of strings

Create a class SafeStack that implements a stack of strings

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  Allows the user to enter students names followed

Write a program that allows the user to enter students names followed by their test scores and outputs the following information(assume that the maximum nmber of students in the class is 50

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd