why my code "Bouncing ball" doesn't work? - java

I'm trying to write a code a bout a bouncing ball, but i'm stuck in how to make the ball bounced. The code seems correct, no wrong messages from eclipse and yet the ball doesn't move. Any help/hint is appreciate.
Here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BouncingBallTest extends JFrame {
private JButton jbtStart = new JButton("Start");
private JButton jbtStop = new JButton("Stop");
private Ball canvas = new Ball();
public BouncingBallTest() {
JPanel panel = new JPanel(); // Use the panel to group buttons
panel.add(jbtStart);
panel.add(jbtStop);
add(canvas, BorderLayout.CENTER); // Add canvas to centre
add(panel, BorderLayout.SOUTH); // Add panel to south
// register listener
jbtStart.addActionListener(new StartBouncingBallTest());
jbtStop.addActionListener(new StopBouncingBallTest());
}
// the main method
public static void main(String[] args) {
JFrame frame = new BouncingBallTest();
frame.setTitle("Bouncing Ball Test");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 300);
frame.setVisible(true);
}
class StartBouncingBallTest implements ActionListener { // inner class
#Override
public void actionPerformed(ActionEvent e) {
canvas.StartBouncingBallTest();
}
}
class StopBouncingBallTest implements ActionListener { // inner class
#Override
public void actionPerformed(ActionEvent e) {
canvas.StopBouncingBallTest();
}
}
class Ball extends JPanel {
private int radius = 10;
private int x;
private int y;
private int dx = 3;
private int dy = 3;
private Timer timer = new Timer(20, new TimerListener());
public void StartBouncingBallTest() {
if (x > 0 && y > 0) {
x += dx;
y += dy;
}
else if (x == 0) {
x -= dx;
y += dy;
}
else if (y + (2 * radius) > getHeight()) {
x += dx;
y -= dy;
}
else if (y == 0) {
x += dx;
y -= dy;
}
else if (x + (2 * radius) > getWidth()) {
x -= dx;
y += dy;
}
repaint();
timer.start();
}
public void StopBouncingBallTest() {
timer.stop();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}

Basically, nothing is moving the ball.
Each time the Swing Timer ticks, all you do is repaint.
You need to move the movement logic to the actionPerformed method of the ActionListener registered to the Timer
More like...
public void StartBouncingBallTest() {
timer.start();
}
//...
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (x > 0 && y > 0) {
x += dx;
y += dy;
}
else if (x == 0) {
x -= dx;
y += dy;
}
else if (y + (2 * radius) > getHeight()) {
x += dx;
y -= dy;
}
else if (y == 0) {
x += dx;
y -= dy;
}
else if (x + (2 * radius) > getWidth()) {
x -= dx;
y += dy;
}
repaint();
}
}
This way, each time the Timer ticks, you are making update the position of the ball accordingly...
Updated with working example
I made two changes. I made your TimerListener an inner class of Ball, which allows it to access the variable and methods of Ball and modified your movement logic so it works
class Ball extends JPanel {
private int radius = 10;
private int x;
private int y;
private int dx = 3;
private int dy = 3;
private Timer timer = new Timer(20, new TimerListener());
public void StartBouncingBallTest() {
timer.start();
}
public void StopBouncingBallTest() {
timer.stop();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (x < 0 || x + (2 * radius) > getWidth()) {
dx *= -1;
}
if (y < 0 || y + (2 * radius) > getHeight()) {
dy *= -1;
}
x += dx;
y += dy;
repaint();
}
}
}

private Timer timer = new Timer(20, new TimerListener());
doesn't that new TimerListener() need to do something useful? shouldn't most of the code inside StartBouncingBallTest be happening every time the timer ticks, intsead of just once when the timer starts?

you never declared that the g.fillOval was the ball here do this...
g.drawOval((int)applex, (int)appley, (int)appleradius, appleradius);
public double applex = ArandomX; //replace the random with values..
public double appley = ArandomY; //same here
public int appleradius = 10;
this makes it move...
a.px += a.vx;
// check for boundaries
if (a.px >= this.getWidth() || a.px <= 0) {
// reverse vel and apply
a.vx = -a.vx;
a.px += -a.vx; // to prevent sticking
}
//a.vx += -gravity; // add this constant to accelerate things down
a.py += a.vy;
// check for boundaries
if (a.py >= this.getHeight() || a.py <= 0) {
// reverse vel and apply
a.vy = -a.vy;
a.py += a.vy; // to prevent sticking
}`
replace a.vx with your x or dx idk because your variables are unclear and yeah just replace a.vy with either y or dy and do the same for my px... it will work

Related

My ball is not moving and staying in the center although it should be moving

I am coding a primitive Pong game and I have coded it so for when the ball goes off the left hand side of the screen, it goes to the center of the screen and moves in a random directton. However, upon start the ball always stays in the center and never moves(despite the fact the ball's X coordinate is not less than 0 at start.)
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Pong extends JPanel
{
private static final int FRAME_WIDTH = 400;
private static final Color BACKGROUND = new Color(204, 204, 204);
private static final Color BALL_COLOR = Color.BLACK;
private static final Color PRIZE_COLOR = Color.BLACK;
private static final int BALL_DIAM = 50;
private static final int PRIZE_DIAM = 25;
private static final int PRIZE_SHIFT = 10;
private BufferedImage myImage;
private Graphics myBuffer;
private Ball ball;
private int score1;
private int score2;
private Polkadot prize;
private Bumper bumper;
private Bumper bumper2;
private Timer t;
private int hits;
public Pong()
{
myImage = new BufferedImage(400, 400, 1);
myBuffer = this.myImage.getGraphics();
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, 400, 400);
ball = new Ball(0, 0, 35, BALL_COLOR);
ball.jump(400, 400);
prize = new Polkadot(0, 0, 25, PRIZE_COLOR);
prize.jump(400, 400);
bumper = new Bumper(380,70,50,100,Color.RED);
bumper2 = new Bumper(30,70,50,100,Color.BLUE);
hits = 0;
t = new Timer(5, new Listener());
t.start();
addMouseListener(new Mouse());
addKeyListener(new Key());
setFocusable(true);
}
public void paintComponent(Graphics g)
{
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
private class Mouse extends MouseAdapter
{
private Mouse() {}
public void mousePressed(MouseEvent e)
{
if (e.isMetaDown())
{
ball.setX(e.getX());
ball.setY(e.getY());
}
else if (e.isShiftDown())
{
ball.setdx((int)(Math.random() * 11.0D - 6.0D));
ball.setdy((int)(Math.random() * 11.0D - 6.0D));
}
else
{
prize.setX(e.getX());
prize.setY(e.getY());
}
}
}
private class Key extends KeyAdapter
{
private Key() {}
public void keyPressed(KeyEvent e)
{
if ((e.getKeyCode() == 38) && (bumper.getY() > 0)) {
bumper.setY(bumper.getY() - 10);
} else if ((e.getKeyCode() == 40) && (bumper.getY() < 438)) {
bumper.setY(bumper.getY() + 10);
} else if ((e.getKeyCode() == 87) && (bumper2.getY() > 0)) {
bumper2.setY(bumper2.getY() - 10);
} else if ((e.getKeyCode() == 83) && (bumper2.getY() < 438)) {
bumper2.setY(bumper2.getY() + 10);
}
}
}
private class Listener implements ActionListener
{
private Listener() {}
public void actionPerformed(ActionEvent e)
{
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, 400, 400);
ball.move(400, 400);
collide(ball, prize);
BumperCollision.collide(bumper,ball);
BumperCollision.collide(bumper2,ball);
ball.draw(myBuffer);
prize.draw(myBuffer);
bumper.draw(myBuffer);
bumper2.draw(myBuffer);
myBuffer.setColor(Color.red);
myBuffer.setFont(new Font("Comic Sans MS", 1, 24));
myBuffer.drawString("Count: " + score1, 100, 25);
if(score1 >= 10)
myBuffer.drawString("This man is the Legend27", 50, 102);
if(score2 >= 10)
myBuffer.drawString("The other man might be the legend27", 160, 102);
if(ball.getX() < -100)
score1++;
ball.setX(208);
ball.setY(50);
ball.setdx(20);
repaint();
}
}
public void collide(Ball b, Polkadot p)
{
double dist = distance(b.getX(), b.getY(), p.getX(), p.getY());
if (dist < 37.0D)
{
hits += 1;
p.jump(400, 400);
}
}
private double distance(double x1, double y1, double x2, double y2)
{
return Math.sqrt(Math.pow(x1 - x2, 2.0D) + Math.pow(y1 - y2, 2.0D));
}
}
Ball:
//Name: Date:
import java.awt.*;
public class Ball extends Polkadot
{
private double dx; // pixels to move each time step() is called.
private double dy;
// constructors
public Ball() //default constructor
{
super(200, 200, 50, Color.BLACK);
dx = Math.random() * 12 - 6; // to move vertically
dy = Math.random() * 12 - 6; // to move sideways
}
public Ball(double x, double y, double dia, Color c)
{
super(x, y, dia, c);
dx = Math.random()* 12 - 6;
dy = Math.random() * 12 - 6;
}
//modifier methods
public void setdx(double x)
{
dx = x;
}
public void setdy(double y)
{
dy = y;
}
//accessor methods
public double getdx()
{
return dx;
}
public double getdy()
{
return dy;
}
//instance methods
public void move(double rightEdge, double bottomEdge)
{
setX(getX()+ dx); // move vert.
setY(getY()+ dy);
if(getX() >= rightEdge - getRadius())
{
setX(rightEdge - getRadius());
dx = dx * -1;
}
else if(getX() <= getRadius())
{
setX(getRadius());
dx = dx * -1;
}
if(getY() >= bottomEdge - getRadius())
{
setY(bottomEdge - getRadius());
dy = dy * -1;
}
else if (getY() <= getRadius())
{
setY(getRadius());
dy = dy * -1;
}
}
}
You seem to have two different values for the position of the ball, first you have x, y which are from your parent class, the name of which I do not know. Then you have the position delta member variables of the Ball class called dx and dy. Since your constructor you call the parent constructor
super(x, y, dia, c);
Where x, y are 200 respectively and then they are never touched after. My guess is that those hold the true position of your ball and therefore those values need to be set. Use the setters after you create the random delta positions in your Ball constructor like so
public Ball(double x, double y, double dia, Color c)
{
super(x, y, dia, c);
dx = Math.random()* 12 - 6;
dy = Math.random() * 12 - 6;
setX(getX() + dx); //add your x difference
setY(getY() + dy); //add your y difference
}
This should now add your deltas to the Ball object on creation and when it gets painted for the first time it will have a random position on your game board every time a Ball gets created.
How about this line inside the actionPerformed()
if(ball.getX() < -100)
score1++;
ball.setX(208);
ball.setY(50);
ball.setdx(20);
If the setters should be called only when ball.getX() < -100 then you need to use brackets with your if. No brackets after an if only executes the next line, and nothing more.
if(ball.getX() < -100)
{
score1++;
ball.setX(208);
ball.setY(50);
ball.setdx(20);
}

Java: drawing multiple sprites in different threads at a time

I need to create a JPanel, where upon a mouse click a new Sprite must appear in a new thread. This is what I have:
public class BouncingSprites {
private JFrame frame;
private SpritePanel panel = new SpritePanel();
private Sprite ball;
public BouncingSprites() {
frame = new JFrame("Bouncing Sprite");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
}
public void start(){
panel.animate(); // never returns due to infinite loop in animate method
}
public static void main(String[] args) {
new BouncingSprites().start();
}
}
Class SpritePanel:
public class SpritePanel extends JPanel
{
Sprite sprite;
public SpritePanel()
{
addMouseListener(new Mouse());
}
//method for creating a new ball
private void newSprite (MouseEvent event)
{
sprite = new Sprite(this);
}
public void animate()
{
}
private class Mouse extends MouseAdapter
{
#Override
public void mousePressed( final MouseEvent event )
{
newSprite(event);
}
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (sprite != null)
{
sprite.draw(g);
}
}
}
Class Sprite :
public class Sprite implements Runnable
{
public final static Random random = new Random();
final static int SIZE = 10;
final static int MAX_SPEED = 5;
SpritePanel panel;
private int x;
private int y;
private int dx;
private int dy;
private Color color = Color.BLUE;
private Thread animation;
public Sprite (SpritePanel panel)
{
this.panel = panel;
x = random.nextInt(panel.getWidth());
y = random.nextInt(panel.getHeight());
dx = random.nextInt(2*MAX_SPEED) - MAX_SPEED;
dy = random.nextInt(2*MAX_SPEED) - MAX_SPEED;
animation = new Thread(this);
animation.start();
}
public void draw(Graphics g)
{
g.setColor(color);
g.fillOval(x, y, SIZE, SIZE);
}
public void move()
{
// check for bounce and make the ball bounce if necessary
//
if (x < 0 && dx < 0){
//bounce off the left wall
x = 0;
dx = -dx;
}
if (y < 0 && dy < 0){
//bounce off the top wall
y = 0;
dy = -dy;
}
if (x > panel.getWidth() - SIZE && dx > 0){
//bounce off the right wall
x = panel.getWidth() - SIZE;
dx = - dx;
}
if (y > panel.getHeight() - SIZE && dy > 0){
//bounce off the bottom wall
y = panel.getHeight() - SIZE;
dy = -dy;
}
//make the ball move
x += dx;
y += dy;
}
#Override
public void run()
{
while (Thread.currentThread() == animation)
{
move();
panel.repaint();
try
{
Thread.sleep(40);
}
catch ( InterruptedException exception )
{
exception.printStackTrace();
}
}
}
}
This code creates a new moving sprite. However, the previous sprite gets removed from the panel. What I need to do is to add a new sprite with a mouse click, so that the previous sprite is not removed. If I click three times, three sprites should be painted.
How do I change my code to implement that?
Many thanks!
You could replace Sprite sprite; in the SpritePanel class with List<Sprite> sprites = new ArrayList<>();. Then, when you make a new Sprite, add it to the list. Then in your paintComponent method, you could iterate through the list, drawing all of the sprites.

jframe won't repaint at constant rate

I am trying to learn Java, coming from a C/assembly embedded systems background. After a few weeks of learning, I thought it would be fun to try and make a game, but I am having some problems with a JPanel being repainted at an inconsistent rate.
My "game" GUI consists of a single JFrame which contains a JPanel. As you can see, the main thread for the JFrame sleeps for 30 milliseconds and then updates "game" logic and redraws the JFrame and JPanel. Each time the JPanel is redrawn, I check that it took about 30 milliseconds. As you would expect, it never takes more than about 32 milliseconds between frame redraws.
In spite of the fact that the JPanel is definitely repainted every 30 milliseconds or so, The animation in it can be very jerky. On Ubuntu 14.10, it is extremely obvious, even though the time between calls to my JPanel's repaint() are still never more than 32ms apart.
The most vexing thing is that if I uncomment the lines
//this.resize(300 + a, 300);
//a = (a == 1)?(0):1;
in SimFrame.java, everything is perfectly smooth, which implies that my computer has the capability to update the JPanel at the rate I want.
Is there some way I can force the JFrame to update at the rate I want it to without doing the absurd .resize call?
Main.java
package jdemo;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main
{
public static void main(String[] args)
{
SimFrame s = new SimFrame();
Thread t = new Thread(s);
t.start();
}
}
Sim.java
package jdemo;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class Sim extends JPanel implements KeyListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private int keys[] = new int[1024];
double x = 100;
double y = 100;
double dx = 0;
double dy = 0;
double theta = 0.0;
private int size = 0;
private int dsize = 1;
public Sim()
{
this.addKeyListener(this);
this.setFocusable(true);
}
long prevmillis;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D drawing = (Graphics2D)g;
//clear the background.
drawing.setColor(Color.WHITE);
drawing.fillRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
//System.out.printf("dt = %d\n", System.currentTimeMillis() - prevmillis);
prevmillis = System.currentTimeMillis();
drawing.setColor(Color.BLACK);
drawing.drawRect(0, 0, size, size);
drawing.setColor(Color.BLACK);
int[] xpoints = {(int)(x + 10 * Math.cos(Math.toRadians(theta))),
(int)(x + 5 * Math.cos(Math.toRadians(theta - 150))),
(int)(x + 5 * Math.cos(Math.toRadians(theta + 150)))};
int[] ypoints = {(int)(y + 10 * Math.sin(Math.toRadians(theta))),
(int)(y + 5 * Math.sin(Math.toRadians(theta - 150))),
(int)(y + 5 * Math.sin(Math.toRadians(theta + 150)))};
drawing.drawPolygon(xpoints, ypoints, 3);
}
public void updateLogic()
{
if(keys[KeyEvent.VK_UP] == 1)
{
size++;
}
else if(keys[KeyEvent.VK_DOWN] == 1)
{
size--;
}
//update theta.
if(keys[KeyEvent.VK_LEFT] == 1)
{
theta += 5;
}
if(keys[KeyEvent.VK_RIGHT] == 1)
{
theta -= 5;
}
if(theta > 360.1)
{
theta -= 360;
}
if(theta < -0.1)
{
theta += 360;
}
//update acceleration
if(keys[KeyEvent.VK_SPACE] == 1)
{
dx += 0.08* Math.cos(Math.toRadians(theta));
dy += 0.08 * Math.sin(Math.toRadians(theta));
}
dx *= 0.99;
dy *= 0.99;
//update position
x = x + dx;
y = y + dy;
System.out.printf("%f, %f\n", dx, dy);
//update size
if(size > 150)
{
dsize = -1;
}
if(size < 10)
{
dsize = 1;
}
size += dsize;
}
#Override
public void keyPressed(KeyEvent arg0)
{
// TODO Auto-generated method stub
keys[arg0.getKeyCode()] = 1;
System.out.printf("%d\n", arg0.getKeyCode());
}
#Override
public void keyReleased(KeyEvent arg0)
{
// TODO Auto-generated method stub
keys[arg0.getKeyCode()] = 0;
}
#Override
public void keyTyped(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
}
SimFrame.java
package jdemo;
import jdemo.Sim;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SimFrame extends JFrame implements Runnable
{
private Sim s;
public SimFrame()
{
this.s = new Sim();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(this.s);
this.pack();
this.setLocationRelativeTo(null);
this.setSize(200, 200);
}
#Override
public void run()
{
int a = 0;
this.setVisible(true);
while(true)
{
//repaint
s.updateLogic();
this.getContentPane().revalidate();
this.repaint();
//this.resize(300 + a, 300);
//a = (a == 1)?(0):1;
try
{
Thread.sleep(30);
}
catch(InterruptedException e)
{
System.out.printf("failed");
break;
}
}
}
}
Thank you very much.
Try this and if it works I'll transform it into a true answer (I just can't know if it will work on your system better than your current code):
public class SimFrame extends JFrame {
public SimFrame() {
setContentPane(new Sim());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private class Sim extends JPanel {
double x = 100;
double y = 100;
double dx = 0;
double dy = 0;
double theta = 0.0;
private int size = 0;
private int dsize = 1;
public Sim() {
addKeyListener(new Controller());
setFocusable(true);
setBackground(Color.WHITE);
new Timer(30, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dx *= 0.99;
dy *= 0.99;
// update position
x = x + dx;
y = y + dy;
// update size
if (size > 150)
dsize = -1;
if (size < 10)
dsize = 1;
size += dsize;
// update theta.
if (theta > 360.1) {
theta -= 360;
}
if (theta < -0.1) {
theta += 360;
}
repaint();
}
}).start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
};
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D drawing = (Graphics2D) g;
drawing.setColor(Color.BLACK);
drawing.drawRect(0, 0, size, size);
drawing.setColor(Color.BLACK);
int[] xpoints = {(int) (x + 10 * Math.cos(Math.toRadians(theta))), (int) (x + 5 * Math.cos(Math.toRadians(theta - 150))),
(int) (x + 5 * Math.cos(Math.toRadians(theta + 150)))};
int[] ypoints = {(int) (y + 10 * Math.sin(Math.toRadians(theta))), (int) (y + 5 * Math.sin(Math.toRadians(theta - 150))),
(int) (y + 5 * Math.sin(Math.toRadians(theta + 150)))};
drawing.drawPolygon(xpoints, ypoints, 3);
}
private class Controller extends KeyAdapter {
#Override
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_UP:
size++;
break;
case KeyEvent.VK_DOWN:
size--;
break;
case KeyEvent.VK_LEFT:
theta += 5;
break;
case KeyEvent.VK_RIGHT:
theta -= 5;
break;
case KeyEvent.VK_SPACE:
dx += 0.08 * Math.cos(Math.toRadians(theta));
dy += 0.08 * Math.sin(Math.toRadians(theta));
break;
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new SimFrame();
}
});
}
}
And yes, I know there's the OS's delay on holding a key, we'll get to it if it works.
Edit:
Simpler animation:
public class CopyOfSimFrame extends JFrame {
public CopyOfSimFrame() {
setContentPane(new Sim());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private class Sim extends JPanel {
private int size = 0;
private int dsize = 1;
public Sim() {
setBackground(Color.WHITE);
new Timer(30, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// update size
if (size >= 150)
dsize = -1;
if (size <= 0)
dsize = 1;
size += dsize;
repaint();
}
}).start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(150, 150);
};
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(0, 0, size, size);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CopyOfSimFrame();
}
});
}
}

how to space out random circles across a JPanel when using an array

I am trying to draw 7 random circles across a JPanel using an array. I managed to get the array to work but now I am having trouble spacing out the circles. When i run the program i see multiple circles being drawn but they are all on the same spot. All the circles are of different size and color. The other problem i have is making the circles move towards the bottom of the screen.
public class keyExample extends JPanel implements ActionListener, KeyListener{
private Circle[] circles = new Circle[7];
Timer t = new Timer(5,this);
//current x and y
double x = 150, y = 200;
double changeX = 0, changeY = 0;
private int circlex = 0,circley = 0; // makes initial starting point of circles 0
private javax.swing.Timer timer2;
public keyExample(){
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
NewCircle();
timer2 = new javax.swing.Timer(33,new MoveListener());
timer2.start();
}
public void NewCircle(){
Random colors = new Random();
Color color = new Color(colors.nextInt(256),colors.nextInt(256),colors.nextInt(256));
Random num= new Random();
int radius = num.nextInt(45);
for (int i = 0; i < circles.length; i++)
circles[i] = new Circle(circlex,circley,radius,color);
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fill(new Rectangle2D.Double(x,y,40,40));
for (int i = 0; i < circles.length; i++)
circles[i].fill(g);
}
public void actionPerformed(ActionEvent e){
repaint();
x += changeX;
y += changeY;
changeX = 0;
changeY = 0;
}
public void up() {
if (y != 0){
changeY = -3.5;
changeX = 0;
}
}
public void down() {
if (y <= 350){
changeY = 3.5;
changeX = 0;
}
}
public void left() {
if (x >= 0) {
changeX = -3.5;
changeY = 0;
}
}
public void right() {
if (x <= 550) {
changeX = 3.5;
changeY = 0;
}
}
private class MoveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
Random speed = new Random();
int s = speed.nextInt(8);
}
}
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP){
up();
}
if (code == KeyEvent.VK_DOWN){
down();
}
if (code == KeyEvent.VK_RIGHT){
right();
}
if (code == KeyEvent.VK_LEFT){
left();
}
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}
Circle class
import java.awt.*;
public class Circle{
private int centerX, centerY, radius, coord;
private Color color;
public Circle(int x, int y, int r, Color c){
centerX = x;
centerY = y;
radius = r;
color = c;
}
public void draw(Graphics g){
Color oldColor = g.getColor();
g.setColor(color);
g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
g.setColor(oldColor);
}
public void fill(Graphics g){
Color oldColor = g.getColor();
g.setColor(color);
g.fillOval(centerX - radius, centerY - radius, radius *2, radius * 2);
g.setColor(oldColor);
}
public boolean containsPoint(int x, int y){
int xSquared = (x - centerX) * (x - centerX);
int ySquared = (y - centerY) * (y - centerY);
int RadiusSquared = radius * radius;
return xSquared + ySquared - RadiusSquared <=0;
}
public void move(int xAmount, int yAmount){
centerX = centerX + xAmount;
centerY = centerY + yAmount;
}
}
This is one of the problems with relying on borrowed code that you don't understand...
Basically, all you need to do is change the creation of the circles, for example...
for (int i = 0; i < circles.length; i++) {
circles[i] = new Circle(circlex, circley, radius, color);
circlex += radius;
}
You may wish to re-consider the use of KeyListener, in favour of Key Bindings before you discover that KeyListener doesn't work the way you expect it to...
For some strange reason, you're calling NewCirlces from within the MoveListener's actionPerfomed method, meaning that the circles are simply being re-created on each trigger of the Timer...instead, call it first in the constructor
You're also calling within your paintComponent method...this should mean that the circles never move and instead, random change size...
Updated with runnable example...
I modified your paint code NewCircle and the MoveListener a little...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class CircleExample extends JPanel implements ActionListener, KeyListener {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CircleExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private Circle[] circles = new Circle[7];
Timer t = new Timer(5, this);
//current x and y
double x = 150, y = 200;
double changeX = 0, changeY = 0;
private int circlex = 0, circley = 0; // makes initial starting point of circles 0
private javax.swing.Timer timer2;
public CircleExample() {
NewCircle();
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer2 = new javax.swing.Timer(33, new MoveListener());
timer2.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
public void NewCircle() {
for (int i = 0; i < circles.length; i++) {
Random colors = new Random();
Color color = new Color(colors.nextInt(256), colors.nextInt(256), colors.nextInt(256));
Random num = new Random();
int radius = num.nextInt(90);
circles[i] = new Circle(circlex, circley, radius, color);
circlex += radius;
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fill(new Rectangle2D.Double(x, y, 40, 40));
for (int i = 0; i < circles.length; i++) {
circles[i].fill(g);
}
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
x += changeX;
y += changeY;
changeX = 0;
changeY = 0;
}
public void up() {
if (y != 0) {
changeY = -3.5;
changeX = 0;
}
}
public void down() {
if (y <= 350) {
changeY = 3.5;
changeX = 0;
}
}
public void left() {
if (x >= 0) {
changeX = -3.5;
changeY = 0;
}
}
public void right() {
if (x <= 550) {
changeX = 3.5;
changeY = 0;
}
}
private class MoveListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
Random speed = new Random();
for (Circle circle : circles) {
int s = speed.nextInt(8);
circle.move(0, s);
}
repaint();
}
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
up();
}
if (code == KeyEvent.VK_DOWN) {
down();
}
if (code == KeyEvent.VK_RIGHT) {
right();
}
if (code == KeyEvent.VK_LEFT) {
left();
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public class Circle {
private int centerX, centerY, radius, coord;
private Color color;
public Circle(int x, int y, int r, Color c) {
centerX = x;
centerY = y;
radius = r;
color = c;
}
public void draw(Graphics g) {
Color oldColor = g.getColor();
g.setColor(color);
g.drawOval(centerX, centerY, radius, radius);
g.setColor(oldColor);
}
public void fill(Graphics g) {
Color oldColor = g.getColor();
g.setColor(color);
g.fillOval(centerX, centerY, radius, radius);
g.setColor(oldColor);
}
public boolean containsPoint(int x, int y) {
int xSquared = (x - centerX) * (x - centerX);
int ySquared = (y - centerY) * (y - centerY);
int RadiusSquared = radius * radius;
return xSquared + ySquared - RadiusSquared <= 0;
}
public void move(int xAmount, int yAmount) {
centerX = centerX + xAmount;
centerY = centerY + yAmount;
}
}
}

Need help on breakout game ball speed

This is a grade 12 object oriented programming project.
I have a class called Ball to construct my ball object.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Ball{
private double xPos;
private double yPos;
private int direction;
public Ball(int ixPos, int iyPos, int idirection){
xPos = ixPos;
yPos = iyPos;
direction = idirection;
}
public int returnX(){
return (int)xPos;
}
public int returnY(){
return (int)yPos;
}
public int returnDirection(){
return direction;
}
public void move(){
xPos += 1*Math.cos(Math.toRadians(direction));
yPos -= 1*Math.sin(Math.toRadians(direction));
}
public void collide(int collideWith){
if(collideWith==1){//collide with left wall
if(90<direction && direction<180){
direction = 180-direction;
}
if(180<direction && direction<270){
direction = 540-direction;
}
}
if(collideWith==2){//collide with right wall
if(0<direction && direction<90){
direction = 180-direction;
}
if(270<direction && direction<360){
direction = 540-direction;
}
}
if(collideWith==3){//collide with up wall
if(0<direction && direction<90){
direction = 360-direction;
}
if(90<direction && direction<180){
direction = 360-direction;
}
}
if(collideWith==4){//collide with down wall
direction = 360-direction;
}
}
public void collidePaddle(int collidePos){
if(collidePos!=50 && collidePos!=0){
direction = (50-collidePos)*180/50;
}
}
}
As you can see in the "move" function, right now the ball is going at a very low speed. But i need the ball to go faster. If I change the 1 into something like, 5, there would be a problem. In my main class where it checks if the ball is hitting the wall or blocks to change direction, the ball would go into the wall or the blocks if the amount of pixels the ball can move each time is greater than 1.
To me it seems like there's no way to solve this problem, no idea where I would start thinking, is there a better way of checking collide or something?
Thank you.
Instead of using absolute checks in your collidePaddle check, you should allow for ranges.
For example...
public void collidePaddle(int collidePos){
if (collidePos >= 50) {
direction = (50-collidePos)*180/50;
// Correct the position of the ball to meet the minimum requirements
// of the collision...
} else if (collidePos <= 0) {
direction = (50-collidePos)*180/50;
// Correct the position of the ball to meet the minimum requirements
// of the collision...
}
}
(Sorry, I'm having fun working out your code ;))
This will allow the ball the "pass" beyond the these points within a virtual context, but if you correct the position to componsate, it should make no difference...when it's rendered...
Updated
Here's a REALLY SIMPLE example of what I'm talking about...
public class SimpleBouncyBall {
public static void main(String[] args) {
new SimpleBouncyBall();
}
public SimpleBouncyBall() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CourtPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CourtPane extends JPanel {
private Ball ball;
private int speed = 5;
public CourtPane() {
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Rectangle bounds = new Rectangle(new Point(0, 0), getSize());
if (ball == null) {
ball = new Ball(bounds);
}
speed = ball.move(speed, bounds);
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (ball != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Point p = ball.getPoint();
g2d.translate(p.x, p.y);
ball.paint(g2d);
g2d.dispose();
}
}
}
public class Ball {
private Point p;
private int radius = 12;
public Ball(Rectangle bounds) {
p = new Point();
p.x = 0;
p.y = bounds.y + (bounds.height - radius) / 2;
}
public Point getPoint() {
return p;
}
public int move(int speed, Rectangle bounds) {
p.x += speed;
if (p.x + radius >= (bounds.x + bounds.width)) {
speed *= -1;
p.x = ((bounds.x + bounds.width) - radius) + speed;
} else if (p.x <= bounds.x) {
speed *= -1;
p.x = bounds.x + speed;
}
p.y = bounds.y + (bounds.height - radius) / 2;
return speed;
}
public void paint(Graphics2D g) {
g.setColor(Color.RED);
g.fillOval(0, 0, radius, radius);
}
}
}
My move method doesn't care if you've past the boundaries as it will reposition the ball back to sit within side those boundaries
public int move(int speed, Rectangle bounds) {
// Apply the delta
p.x += speed;
// Have we passed beyond the right side??
if (p.x + radius >= (bounds.x + bounds.width)) {
speed *= -1;
p.x = ((bounds.x + bounds.width) - radius) + speed;
// Have we past beyond the left side??
} else if (p.x <= bounds.x) {
speed *= -1;
p.x = bounds.x + speed;
}
p.y = bounds.y + (bounds.height - radius) / 2;
return speed;
}
Play around with the speed and see what you get ;)

Categories

Resources