Moving a GOval object in circular motion using movePolar method in java - java

I am currently creating a small project, using ACM graphics library, where you will have to move the ball (or an object) of GOval using method movePolar to move in circular motion. So far I know that there are two arguments in movePolar() where the first r argument is the distance you want to move and the second argument is the angle that you want to move in. But I couldn't figure out how to make the ball move in circular motion using movePolar() method. I have tried using multiple movePolar(1,90), movePolar(1,45), etc. and still doesn't got the goal I want. Here is my code:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import acm.graphics.*;
import acm.program.*;
public class ColorCircleDecomp extends GraphicsProgram implements ActionListener {
public static final int PROGRAM_WIDTH = 800;
public static final int PROGRAM_HEIGHT = 600;
public static final int BALL_SIZE = 50;
public static final int DELAY_MS = 25;
private GOval ball;
//TODO create a private GOval here
public void run() {
//TODO add your ball here
Timer t = new Timer(DELAY_MS, this);
ball = new GOval(300,300,BALL_SIZE,BALL_SIZE);
add(ball);
t.start();
}
#Override
public void actionPerformed(ActionEvent e) {
ball.movePolar(1,90);
ball.movePolar(1,0);
ball.movePolar(1, 270);
ball.movePolar(1, 180);
}
public void init() {
setSize(PROGRAM_WIDTH, PROGRAM_HEIGHT);
}
}

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import acm.graphics.*;
import acm.program.*;
public class ColorCircleDecomp extends GraphicsProgram implements ActionListener {
public static final int PROGRAM_WIDTH = 800;
public static final int PROGRAM_HEIGHT = 600;
public static final int BALL_SIZE = 50;
public static final int DELAY_MS = 25;
int i = 0;
private GOval ball;
public void run() {
ball = new GOval (370, 540, BALL_SIZE, BALL_SIZE);
Timer t = new Timer(DELAY_MS, this);
t.start();
}
#Override
public void actionPerformed(ActionEvent e) {
Color color = new Color(0, 0, 255);
add(ball);
i+=30;
ball.setColor(color);
ball.movePolar(130, i);
}
public void init() {
setSize(PROGRAM_WIDTH, PROGRAM_HEIGHT);
}
}

Related

java threads + bouncing balls

Thanks in advance for help
I created a program that makes multiple bouncing balls When user clicks on the screen a new ball should appear and move around screen. But when i click on the screen a ball appears and doesn't moving at all. When another click happens, the ball created previously jumped to another position instantly.
this is the ball class: used to create balls
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import javax.swing.JComponent;
public class Ball extends JComponent implements Runnable{
private final int DIAMETER = 25;
private final int v1 = 5;
private final int v2 = -5;
private final Random rnd = new Random();
private int posX;
private int posY;
private Color color;
private int xVelocity;
private int yVelocity;
public Ball(int posX, int posY) {
this.posX = posX;
this.posY = posY;
this.color = randomColor();
this.xVelocity = rnd.nextBoolean()?v1:v2;
this.yVelocity = rnd.nextBoolean()?v1:v2;
}
public void move() {
if (posX < 15) {
xVelocity = -xVelocity;
} else if(posX > 475) {
xVelocity = -xVelocity;
}
if (posY < 0) {
yVelocity = -yVelocity;
} else if(posY > 475) {
yVelocity = -yVelocity;
}
posX +=xVelocity;
posY +=yVelocity;
}
public void draw(Graphics2D g2) {
g2.setColor(color);
g2.fill(new Ellipse2D.Double(posX,posY,DIAMETER,DIAMETER));
}
private static Color randomColor() {
int r = (int)(Math.random()*255);
int g = (int)(Math.random()*255);
int b = (int)(Math.random()*255);
Color rColor = new Color(r,g,b);
return rColor;
}
#Override
public void run() {
while(!Thread.interrupted()) {
move();
repaint();
try {
Thread.sleep(60);
} catch (InterruptedException ex) {}
}
}
}
this is the ballcomponent class: used to create the panel & display the balls
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BallComponent extends JPanel{
private ArrayList<Ball> bList;
public BallComponent() {
bList = new ArrayList<Ball>();
this.setPreferredSize(new Dimension(500,500));
this.setBackground(Color.BLACK);
this.addMouseListener(new ClickListener());
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
for (Ball a : bList) {
a.draw(g2);
}
}
private class ClickListener extends MouseAdapter{
public void mouseClicked(MouseEvent e) {
Ball a = new Ball(e.getX(),e.getY());
bList.add(a);
repaint();
Thread gameThread = new Thread(a);
gameThread.start();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Bouncing Balls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new BallComponent());
frame.pack();
frame.setVisible(true);
}
}
Since you have a bunch of threads doing random stuff. You might as well have your JPanel update on it's own.
At the end of your main method.
new Thread( ()->{
while(true){
frame.repaint();
try{
Thread.sleep(60);
} catch(Exception e){
break;
}
}
}).start();
Usually you would do this with a timer task, but since you said you couldn't use a Timer, I just wrote a thread version.
I think repaint() is safe to call from off of the EDT since it just schedules a repaint. The paintComponent method and click method will only be called on the EDT. So you shouldn't get CCME. There are a bunch of race conditions with the multiple threads, but it seems like the only problem would be balls drawn out of position.

JAVA - Graphics2D dispose() method not disposing filled objects

I'm trying to learn Graphics2D library while building small games such as 2D car games, platformer games and so on... Today I decided to make a space-based game and obvoiusly in space you need stars. So I decided to use fillOval() to draw small circles which would give the effect of night-sky. BUT, whenever I try to move the stars or any other object, the older frames just stay there and a bunch of layers are drawn one after another. I thought that dipose() method would be enough... but it seems like it aint.What options do I have to fix this problem?
Here's a preview of what it looks like when i run the code.
Main.java (irrelevant code filtered out)
package asteroids;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
public class Main extends Canvas implements KeyListener {
private boolean gameOver;
private BufferStrategy backBuffer;
private Dimension dimension = new Dimension(Config.WINDOW_WH[0], Config.WINDOW_WH[1]);
private List<Star> stars = new ArrayList<Star>();
private HashMap<Integer,Boolean> keyDownMap = new HashMap<Integer, Boolean>();
private Ship ship;
public Main() {
// Initialize Window
initWindow();
addKeyListener(this);
this.createBufferStrategy(2);
backBuffer = this.getBufferStrategy();
// init variables
gameOver = false;
// Generating stars
generateStars();
// Init spaceship
ship = new Ship(25,36,"ship.png");
// Init loop
gameLoop();
}
public void initWindow(){
JFrame window = new JFrame("Asteroids");
setPreferredSize(dimension);
window.add(this);
window.pack();
window.setResizable(false);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(new Color(54, 71, 99));
window.requestFocus();
}
public void update() {
if(keyDownMap.containsKey(KeyEvent.VK_ESCAPE)){
gameOver = false;
System.exit(0);
}
/*for(Star s: stars) {
s.update(keyDownMap);
}*/
this.ship.update(keyDownMap);
}
public void render(){
Graphics2D g = (Graphics2D) backBuffer.getDrawGraphics();
// Stars
for(Star s: stars) {
g.fillOval(s.posx - (s.width/2), s.posy - (s.height/2), s.width, s.height);
}
// Spaceship
g.drawImage(
this.ship.getImage(),
this.ship.posx, this.ship.posy,
this.ship.width, this.ship.height, null);
g.dispose();
backBuffer.show();
}
public void generateStars() {
for(int i = 0;i < 20;i++) {
int starX = new Random().nextInt(Config.WINDOW_WH[0]-10)+5;
int starY = new Random().nextInt(Config.WINDOW_WH[1]-10)+5;
stars.add(new Star(starX, starY));
}
}
public void gameLoop(){
while(!gameOver){
update();
render();
try{ Thread.sleep(20);}catch(Exception e){};
}
}
public static void main(String[] args) {
new Main();
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
keyDownMap.put(e.getKeyCode(), true);
}
#Override
public void keyReleased(KeyEvent e) {
keyDownMap.remove(e.getKeyCode());
}
}
Star.java
package asteroids;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.util.HashMap;
import java.util.Random;
public class Star {
int width, height;
int posx, posy;
/** CONSTRUCTOR **/
public Star(int x, int y) {
int rand = new Random().nextInt(Config.STAR_SIZES.length);
width = Config.STAR_SIZES[rand];
height = Config.STAR_SIZES[rand];
posx = x;
posy = y;
}
public void update(HashMap keyDownMap) {
if(keyDownMap.containsKey(KeyEvent.VK_LEFT))
this.posx += 1;
if(keyDownMap.containsKey(KeyEvent.VK_RIGHT))
this.posx -= 1;
if(keyDownMap.containsKey(KeyEvent.VK_UP))
this.posy += 1;
if(keyDownMap.containsKey(KeyEvent.VK_DOWN))
this.posy -= 1;
}
}
Ship.java
package asteroids;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import javax.swing.ImageIcon;
public class Ship {
public int posx, posy;
public int width, height;
private Image image;
public Ship(int w, int h, String img) {
this.posx = Config.WINDOW_WH[0]/2;
this.posy = Config.WINDOW_WH[1]/2;
this.width = w;
this.height = h;
this.image = new ImageIcon(getClass().getResource("/"+img)).getImage();
}
public Image getImage() {
return this.image;
}
public void setPosx(int x) {posx = x;}
public void setPosy(int y) {posy = y;}
public void setImg(Image img) {
this.image = img;
}
public void update(HashMap keyDownMap) {
if(keyDownMap.containsKey(KeyEvent.VK_LEFT))
this.posx += 1;
if(keyDownMap.containsKey(KeyEvent.VK_RIGHT))
this.posx -= 1;
if(keyDownMap.containsKey(KeyEvent.VK_UP))
this.posy += 1;
if(keyDownMap.containsKey(KeyEvent.VK_DOWN))
this.posy -= 1;
}
}
Config.java
import java.awt.Color;
public class Config {
// MAIN CANVAS SETTINGS
public static int[] WINDOW_WH = {500, 500};
public static String WINDOW_BG_IMG = "";
public static Color WINDOW_BG_CLR = new Color(40, 42, 45);
// OBJECT SETTINGS
public static int[] STAR_SIZES = {10,5,8,3,7};
}
EDIT:
After I tried to add a background image g.drawImage(this.bg,0,0,Config.WINDOW_WH[0], Config.WINDOW_WH[1], null); before drawing stars and the ship, it doesn't happen anymore. But im wondering if this is a fix or just a bad practice for avoiding the un-cleared frames.
BUT, whenever I try to move the stars or any other object, the older frames just stay there and a bunch of layers are drawn one after another.
Yes, that's how painting works. Think of the Graphics context like a physical canvas. Each time you paint on it, you are adding more more content to it. The API won't clear it for you.
There advantages to this, if you're performing an additive painting process, you may not wish to refresh it on each pass, but in your case, you're going to have to clear it each time.
I thought that dipose() method would be enough... but it seems like it aint
No, dispose releases any internal resources the context might be holding, freeing up memory. Having said that, you shouldn't dispose of a context you did create, as it can prevent future operations from been applied.
If you take a look at the example from the tutorials it clear shows the Graphics context been clear/prepared for each frame...
Graphics g = bufferStrategy.getDrawGraphics();
if (!bufferStrategy.contentsLost()) {
g.setColor(COLORS[i]);
g.fillRect(0,0,bounds.width, bounds.height);
bufferStrategy.show();
g.dispose();
}
I would also recommend having a look at the example in the JavaDocs which presents a basic loop operation

How to create a Graphics object to draw shapes on JFrame

I want to make a program to animate a projectile (a ball flying in a projectile in 2D). In main I call a Shoot() function whose argument is a Graphics object, but I don't know how to create the object so that it draws on my JFrame object. Please help me.
import java.lang.Math;
import java.applet.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Projectile extends JPanel{
public static int ScreenH=1500;
public static int ScreenW=3000;
public static int Xcor=0;
public static int Ycor=0;
public static int ballRadius=20;
public static int prevXcor = 0;
public static int prevYcor = 0;
public static int newXcor = 0;
public static int newYcor = 0;
public static int Time = 0;
public static int Angle = 45;
public static int Velocity = 10;
public static double Acceleration = 9.8;
public static void InitGraphics(){
JFrame jframe = new JFrame();
jframe.setTitle("Projectile");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(ScreenW, ScreenH);
jframe.setResizable(false);
jframe.setVisible(true);
jframe.add(new Projectile());
}
public static void drawCenteredCircle(Graphics g, int x, int y) {
int a = x;
int b = 1000 - y;
g.fillOval(a,b, ballRadius, ballRadius);
}
public static void move() throws InterruptedException {
Thread.sleep(20);
Time += 20;
double XVelocity = Math.sin(Velocity);
double YVelocity = Math.cos(Velocity);
int X = (int) (XVelocity*Time);
int Y = (int) ((YVelocity*Time) + (0.5*Acceleration*Time*Time));
newXcor = X;
newYcor = Y;
}
public static void repaint(Graphics g) {
g.setColor(Color.white);
drawCenteredCircle(g, prevXcor, prevYcor);
g.setColor(Color.red);
drawCenteredCircle(g, newXcor, newYcor);
prevXcor = newXcor;
prevYcor = newYcor;
}
public static void Shoot(Graphics g) throws InterruptedException {
while ( (newXcor < (ScreenW - (4*ballRadius))) && (newYcor < (ScreenH - (4*ballRadius)))) {
move();
repaint(g);
}
}
public static void main(String[]args) {
InitGraphics();
Shoot();
}
}
The JFrame is just the window, you need to add a JComponent to it. JComponents contain a protected void paintComponent(Graphics g) method which you need to override like this:
JFrame frame = new JFrame();
JComponent canvas = new JComponent() {
protected void paintComponent(Graphics g) {
//call repaint(g) here instead of this
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());
};
};
frame.add(canvas);
You will probably have to clear the background in repaint

Java Animation with ActionListener

So I'm trying to draw 2 circles on top of each other (kinda like a snowman) and move the snowman to the right when the user clicks on the "Start" button and stop moving the snowman when the user clicks on the "Stop" button. However, the only thing that I am able to come up with is 2 snowmen drawn next to each other that don't react to the buttons.
Here is what I've come up with:
import java.awt.Graphics2D;
public interface MoveableShape {
void draw(Graphics2D g);
void translate(int dx, int dy);
}
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class SnowmanShape implements MoveableShape {
private int x;
private int y;
private int width;
public SnowmanShape(int x, int y, int width){
this.x = x;
this.y = y;
this.width = width;
}
#Override
public void draw(Graphics2D g2) {
// TODO Auto-generated method stub
Ellipse2D.Double head = new Ellipse2D.Double(0, 0, 10, 10);
Ellipse2D.Double body = new Ellipse2D.Double(0, 11, 10, 10);
g2.draw(head);
g2.draw(body);
}
#Override
public void translate(int dx, int dy) {
// TODO Auto-generated method stub
x += dx;
y += dy;
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
MoveableShape s;
public MyPanel (MoveableShape m){
s = m;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
s.draw((Graphics2D)g);
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class AnimationTester {
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 200;
private static final int SNOWMAN_WIDTH = 50;
final static MoveableShape shape = new SnowmanShape(0, 0, SNOWMAN_WIDTH);
final static JPanel panel = new MyPanel(shape);
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
frame.setLayout(new BorderLayout());
frame.add(panel);
frame.add(startButton, BorderLayout.NORTH);
frame.add(stopButton, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
final int DELAY = 100;
// Milliseconds between timer ticks
Timer t = new Timer(DELAY, translateSnowman());
startButton.addActionListener(startTimer(t));
stopButton.addActionListener(stopTimer(t));
}
public static ActionListener translateSnowman(){
return new ActionListener(){
public void actionPerformed(ActionEvent event){
shape.translate(1, 0);
panel.repaint();
}
};
}
public static ActionListener startTimer(final Timer t){
return new ActionListener(){
public void actionPerformed(ActionEvent event){
t.start();
}
};
}
public static ActionListener stopTimer(final Timer t){
return new ActionListener(){
public void actionPerformed(ActionEvent event){
t.stop();
}
};
}
}
Could someone please let me know where I went wrong or point me in the right direction?
EDIT: I fixed up the AnimationListener so now it doesn't draw 2 snowmans. The snowman still won't move however. I updated the code in the post as well.
Add g2.translate(x, y); to SnowmanShape#draw, before you are painting:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
class SnowmanShape implements MoveableShape {
static final Color headColor = new Color(0xFFE9C9);
static final Color bodyColor = new Color(0xEAF6FF);
static final Color outlineColor = new Color(0x252525);
int x;
int y;
int size;
Ellipse2D.Double head;
Ellipse2D.Double body;
SnowmanShape(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
initModel();
}
void initModel() {
head = new Ellipse2D.Double(0, 0, size, size);
body = new Ellipse2D.Double(0, head.height, size * 1.3d, size * 1.5d);
body.x -= (body.width - head.width) * (1 / 2d);
}
#Override
public void draw(Graphics2D g2) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.translate(x, y);
g2.setColor(headColor);
g2.fill(head);
g2.setColor(outlineColor);
g2.draw(head);
g2.setColor(bodyColor);
g2.fill(body);
g2.setColor(outlineColor);
g2.draw(body);
}
#Override
public void translate(int dx, int dy) {
x += dx;
y += dy;
}
}
I not an expert on doing things like this, but if I were you I would use the timer to move the objects and then call the paint to repaint the objects in a new position. Therefore, your ShapeIcon class would just keep track of the position of your objects. That probably wasn't all that helpful, so to point you in right direction, you can check out some code in this tutorial here.

Painting to a panel using threads in Java

do not know why there is not printing going on here in my run method. I call repaint and override it in the horsethread class. here is my code. When I run the program and hit Run Race it doesnt draw anything to the panel, any ideas?
import javax.swing.JFrame;
public class HorseTester {
public static void main(String[] args)
{
JFrame frame = new HorsePanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.util.ArrayList;
public class HorsePanel extends JFrame
{
private JPanel panel;
private JButton reset;
private JButton quit;
private JButton run;
private ActionListener listener;
private static final int NUM_OF_HORSES = 5;
public static final int FRAME_WIDTH = 400;
public static final int FRAME_HEIGHT = 400;
private ArrayList<Thread> allThreads = new ArrayList<Thread>();
private ArrayList<HorseThread> horses = new ArrayList<HorseThread>();
public HorsePanel()
{
//Allocating the memory for horses
for(int i=0;i<NUM_OF_HORSES;i++)
horses.add(new HorseThread(i+1));
//Adding them to thread pool
for(int i=0;i<NUM_OF_HORSES;i++)
allThreads.add( new Thread(horses.get(i)));
createPanel();
createRunRace();
createQuit();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createRunRace()
{
class RunRace implements ActionListener
{
public void actionPerformed(ActionEvent rightEvent)
{
run.setEnabled(false);
for(int i=0;i<NUM_OF_HORSES;i++)
allThreads.get(i).start();
}
}
ActionListener a = new RunRace();
this.run.addActionListener(a);
}
public void createQuit()
{
class QuitRace implements ActionListener
{
public void actionPerformed(ActionEvent rightEvent)
{
System.exit(0);
}
}
ActionListener b = new QuitRace();
this.quit.addActionListener(b);
}
public void createPanel()
{
panel = new JPanel(new BorderLayout());
JPanel drawingPanel = new JPanel();
this.run = new JButton("Run Race");
this.quit = new JButton("Quit");
this.reset = new JButton("Reset");
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, 3));
topPanel.add(run);
topPanel.add(reset);
topPanel.add(quit);
panel.add(topPanel, BorderLayout.NORTH);
panel.add(drawingPanel, BorderLayout.SOUTH);
add(panel);
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
public class HorseThread extends JComponent implements Runnable
{
public static final int X_START = 10;
public static final int Y_START = 20;
private Horse horse;
private int xpos, ypos;
public HorseThread(int offset)
{
xpos = X_START;
ypos = Y_START * offset;
horse = new Horse(xpos, ypos);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
horse.draw(g2);
}
/**
* Run method that thread executes and makes horses go across
* the screen racing.
*/
public void run()
{
repaint();
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class Horse
{
private int xTop;
private int yTop;
public static final int RING_WIDTH = 20;
public Horse(int x, int y)
{
xTop = x;
yTop = y;
}
public void setXY(int dx, int dy)
{
xTop = dx;
yTop = dy;
}
public void draw(Graphics2D g2)
{
Ellipse2D.Double horse = new Ellipse2D.Double(xTop, yTop, RING_WIDTH, RING_WIDTH);
g2.setColor(Color.BLUE);
g2.fill(horse);
g2.setColor(Color.WHITE);
g2.fill(horse);
}
}
The main problem is, HorseThread extends from JComponent, but you never actually add it to anything, therefore it never gets painted.
If, instead, you created a TrackPane and painted each of the horses within the paintComponent method (there's no need for HorseThread to extend from JComponent), you might have better luck.
Also note, that once a Thread has completed, it can not be restarted...
For example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HorseTester {
public static void main(String[] args) {
new HorseTester();
}
public HorseTester() {
JFrame frame = new HorsePanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class HorsePanel extends JFrame {
private JPanel panel;
private JButton reset;
private JButton quit;
private JButton run;
private ActionListener listener;
public static final int FRAME_WIDTH = 400;
public static final int FRAME_HEIGHT = 400;
private TrackPane trackPane;
public HorsePanel() {
createPanel();
createRunRace();
createQuit();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createRunRace() {
class RunRace implements ActionListener {
public void actionPerformed(ActionEvent rightEvent) {
run.setEnabled(false);
trackPane.start();
}
}
ActionListener a = new RunRace();
this.run.addActionListener(a);
}
public void createQuit() {
class QuitRace implements ActionListener {
public void actionPerformed(ActionEvent rightEvent) {
System.exit(0);
}
}
ActionListener b = new QuitRace();
this.quit.addActionListener(b);
}
public void createPanel() {
panel = new JPanel(new BorderLayout());
trackPane = new TrackPane();
this.run = new JButton("Run Race");
this.quit = new JButton("Quit");
this.reset = new JButton("Reset");
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1, 3));
topPanel.add(run);
topPanel.add(reset);
topPanel.add(quit);
panel.add(topPanel, BorderLayout.NORTH);
panel.add(trackPane, BorderLayout.CENTER);
add(panel);
}
}
public class TrackPane extends JPanel {
private static final int NUM_OF_HORSES = 5;
private ArrayList<HorseThread> horses = new ArrayList<HorseThread>();
private ArrayList<Thread> threads = new ArrayList<Thread>(25);
public TrackPane() {
setBackground(Color.GREEN);
reset();
}
public void reset() {
// Should dispose of any running threads...
horses.clear();
//Allocating the memory for horses
for (int i = 0; i < NUM_OF_HORSES; i++) {
horses.add(new HorseThread(this, i + 1));
}
}
public void start() {
// Should dispose of any running threads...
threads.clear();
for (int i = 0; i < horses.size(); i++) {
Thread thread = new Thread(horses.get(i));
thread.start();
threads.add(thread);
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (HorseThread horse : horses) {
horse.paint(g);
}
}
}
public class HorseThread implements Runnable {
public static final int X_START = 10;
public static final int Y_START = 20;
private Horse horse;
private int xpos, ypos;
private TrackPane track;
public HorseThread(TrackPane track, int offset) {
xpos = X_START;
ypos = Y_START * offset;
horse = new Horse(xpos, ypos);
this.track = track;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
horse.draw(g2);
}
/**
* Run method that thread executes and makes horses go across the screen
* racing.
*/
public void run() {
track.repaint();
}
}
public class Horse {
private int xTop;
private int yTop;
public static final int RING_WIDTH = 20;
public Horse(int x, int y) {
xTop = x;
yTop = y;
}
public void setXY(int dx, int dy) {
xTop = dx;
yTop = dy;
}
public void draw(Graphics2D g2) {
Ellipse2D.Double horse = new Ellipse2D.Double(xTop, yTop, RING_WIDTH, RING_WIDTH);
g2.setColor(Color.BLUE);
g2.fill(horse);
g2.setColor(Color.WHITE);
g2.draw(horse);
}
}
}
Also note that Swing is not thread safe, be careful using Threads in this environment ;)

Categories