14-4. Write a program that will define a runnable frame to have a ball move acro
ID: 3832603 • Letter: 1
Question
14-4. Write a program that will define a runnable frame to have a ball move across the frame left to right. While that is happening, have another frame that will let the user do something like click a button or input into a textfield, but keep the ball moving in the other frame. When the ball reaches the right edge of the frame or when the user takes action - enters in textfield or clicks the button, end the thread. The frame application will be running while the ball runnable thread is running. The one frame class is shown below and the second ball frame class is partially done. Finish the ball frame class, filling in the missing methods shown with comments below. First, here is the Frame class with the ball thread instantiated: package threads14; /* * This program will let the user click a button while a ball is moving */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ex144 extends JFrame implements ActionListener { ball144 b = new ball144(); // create ball frame runnable thread object JButton jb = new JButton("Click"); JLabel jl = new JLabel(" "); public ex144(String s) { super(s); setSize(100, 100); setLayout(new FlowLayout()); add(jb); add(jl); jb.addActionListener(this); setVisible(true); b = new ball144(); // run constructor b.setSize(500, 300); // set size of frame b.setVisible(true); new Thread(b).start(); /* start the runnable ball b by converting to subclass Thread and call Thread start() method */ setVisible(true); } public void actionPerformed(ActionEvent e) { jl.setText("You clicked"); b.end(); // end the runnable ball b } public static void main(String[] args) { ex144 jf = new ex144("Click"); } }Next, here is the ball runnable thread: package threads14; /* Bill Wohlleber * Exercise 14-4 * This class will define a moving ball */ import java.awt.*; import javax.swing.*; public class ball144 extends JFrame implements Runnable /* since extends JFrame, must implements Runnable since can only extends one class */ { boolean stopflag; int x, y; Container c; public ball144() { c = getContentPane(); c.setBackground(Color.cyan); stopflag = false; x = 0; y = 50; setTitle("Ball"); setForeground(Color.blue); } /* write run() method that will switch stopflag and loop and keep calling repaint() while the ball is not stopped and the ball is on the frame (the ball width less than the right side). When done, end the ball thread. */ // write update method to not clear frame and call paint() /* write paint() method to cover old ball, move x across the screen and draw new ball */ /* write end() method that will switch stopflag and draw final ball */
Explanation / Answer
Executable code:
BallGameTest.java
public class BallGameTest {
public static void main(String[] args) {
BallGame world = new BallGame("Ball game");
world.setVisible(true);
world.move();
}
}
BallGame.java:
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class BallGame extends MovingBall {
private Paddle myPaddle = new Paddle(FRAME_WIDTH, FRAME_HEIGHT);
public BallGame(String title) {
super(title);
addKeyListener(new KeyList());
}
public void paint(Graphics g) {
super.paint(g);
if (isContact()) {
myBall.bounce();
} else {
myPaddle.paint(g);
}
}
public boolean isContact() {
if (myPaddle.area().contains(myBall.getPosition())) {
return true;
} else {
return false;
}
}
private class KeyList extends KeyAdapter {
public void keyPressed(KeyEvent k) {
if (k.getKeyCode() == KeyEvent.VK_LEFT) {
myPaddle.moveLeft();
}
if (k.getKeyCode() == KeyEvent.VK_RIGHT) {
myPaddle.moveRight();
}
}
}
}
MovingBall.java:
import java.awt.Graphics;
import javax.swing.JFrame;
public class MovingBall extends JFrame {
protected final int FRAME_WIDTH = 240;
protected final int FRAME_HEIGHT = 320;
protected Ball myBall = new Ball(FRAME_WIDTH, FRAME_HEIGHT);
public MovingBall(String title) {
super(title);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
myBall.paint(g);
}
public void move() {
while (true) {
myBall.move();
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.exit(0);
}
}
}
}
Ball.java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
public class Ball {
private final int RADIUS = 10;
private Point pos;
private Color ballColour = Color.red;
private int yChange = 2;
private int xChange = 1;
private int height, width;
public Ball(int frameWidth, int frameHeight) {
width = frameWidth;
height = frameHeight;
pos = new Point();
pos.x = (int) (Math.random() * (width - RADIUS)) + RADIUS;
pos.y = (int) (Math.random() * (height / 2 - RADIUS)) + RADIUS;
}
public void paint(Graphics g) {
g.setColor(ballColour);
g.fillOval(pos.x - RADIUS,
pos.y - RADIUS, 2 * RADIUS, 2 * RADIUS);
}
public void move() {
if (pos.y < RADIUS) {
yChange = -yChange;
}
if (pos.x < RADIUS) {
xChange = -xChange;
}
if (pos.x > width - RADIUS) {
xChange = -xChange;
}
if (pos.y < height - RADIUS) {
pos.translate(xChange, yChange);
}
if (pos.x < width - RADIUS) {
pos.translate(xChange, yChange);
}
}
public void bounce() {
yChange = -yChange;
pos.translate(xChange, yChange);
}
public Point getPosition() {
return pos;
}
}
Paddle.java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Paddle {
private Color paddleColour = Color.blue;
private int x, y;
private int paddleWidth = 20;
private int paddleHeight = 10;
private int move = 5;
public Paddle(int frameWidth, int frameHeight) {
x = (int) (Math.random() * (frameWidth - paddleWidth));
y = frameHeight - paddleHeight;
}
public void moveRight() {
x = x + move;
}
public void moveLeft() {
x = x - move;
}
public Rectangle area() {
return new Rectangle(x, y, paddleWidth, paddleHeight);
}
public void paint(Graphics g) {
g.setColor(paddleColour);
g.fillRect(x, y, paddleWidth, paddleHeight);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.