Thread won't start - java

I am attempting to move an star image diagonally across. I am using a Thread to try and achieve this. The program compiles and the image is displayed, however the star won't move at all. I dont think the thread started properly.
Help would be greatly appreciated
drawing class (Board):
//define host package
package star;
//import awt and swing drawing packages
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
//jpanel and other javax classes
import javax.swing.JPanel;
import javax.swing.ImageIcon;
//main board class
public class Board extends JPanel implements Runnable
{
//constructor
Image star; //star image to hold image returned from directory
int x, y; //co ordinates for translation of star image
//delay constant
private final int DELAY = 50;
private Thread animator;
public Board()
{
//set the background colour to black
setBackground(Color.black);
//image directory
ImageIcon ii = new ImageIcon(this.getClass().getResource("star.png"));
//retrieve image from directory
star = ii.getImage();
//paint in memory then screen to improve
setDoubleBuffered(true);
//set star co ords variables
x = y = 10;
}
//initialize thread
void AddNotify()
{
super.addNotify();
//run method in this class
animator = new Thread(this);
animator.start();
}
//jpanel paintComponent() with abstract graphics object
#Override public void paintComponent(Graphics comp)
{
//repaint screen due to animation
super.paintComponent(comp);
Graphics2D comp2d = (Graphics2D) comp;
//draw the star
//class should be notified of drawing
comp2d.drawImage(star, x, y, this);
//sync for linux systems
Toolkit.getDefaultToolkit().sync();
comp.dispose();
}
//set the coordinates for the star image
public void cycle()
{
//move star
x += 1;
y += 1;
//if top corner goes out of range
if (y > 240)
{
x = -45;
y = -45;
}
System.out.println("x: " + x + "y: " + y);
}
//action performed method. Event parameter from the timer
public void run()
{
//beforeTime, timeDiff and sleep variables
//long = 2 x integer
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
//infinite loop
while (true)
{
//cycle and add notify methods
cycle();
//call the paintComponent method
repaint();
//compute system time
timeDiff = System.currentTimeMillis() - beforeTime;
/*subtracting from delay keeps lag from cycle() & AddNotify()
methods unoticable.
timeDiff will change with each loop cycle
*/
sleep = DELAY - timeDiff;
//compensate for a timeDiff > 50
if (sleep < 0)
{
sleep = 2;
}
//sleep thread in exception
try
{
Thread.sleep(sleep);
}
catch (InterruptedException ie)
{
System.out.println("Thread could not sleep: " + ie.getMessage());
}
//reset beforeTime time
beforeTime = System.currentTimeMillis();
}
}
}
main java frame class:
//import jframe
import javax.swing.JFrame;
//main class
public class Star extends JFrame
{
//constructor
public Star()
{
//title, resize, size, location etc.
add(new Board2());
setTitle("Star animation");
setSize(240, 280);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
//class instance
public static void main(String[] arguements)
{
new Star();
}
}
Happy to clarify on request.

You're never calling AddNotify method, which is where you start your thread. I'm guessing you have a typo, and you meant addNotify instead (notice the lower case).
When overriding methods, it's useful to add #Override tag, as compiler will complain if the method you're overriding does not exists.
#Override
void addNotify() {
super.addNotify();
//run method in this class
animator = new Thread(this);
animator.start();
}

It seems your Thread start call is not invoked anywhere in your code execution. You are starting the thread in AddNotify method but that method is not called.
Also your code has a compilation problem on this line:
add(new Board2());
there is no class Board2, rather your class name is Board. I expect it is just an error while pasting the code here.

Related

Ball doesn't move; Thread?

This is an UI that makes a ball go down in a diagonal way, but the ball stays static; it seems something is not working adecuatedly with the threads. Could you please, tell me how to make the ball move?
Please download a ball and change the directory so the program can find where your ball is allocated. It's not necessary to download the soccer pitch but if you want, it's OK. Finally, I have to thank you for spending time in search of this malfunctioning.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.io.File;
class Animation extends JFrame implements ActionListener { //Frame and listener
Rectangle2D dimensions = new Rectangle2D.Double(0,0,850,595); //Not implemented limits
JButton animate, stop;
Runnable runnable;
Thread move;
public Animation() {
setLayout(new BorderLayout()); //BorderLayout disposition
setTitle("Pelota en acción");
animate = new JButton("Animate it!"); //Button to create balls
animate.setBounds(0,0,120,30);
animate.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
Image ball = null;
new Layout().createEllipse(ball);
runnable = new Layout();
move = new Thread(runnable);
move.start();
}
});
stop = new JButton("Freeze"); //Button to interrupt thread (not implemented)
stop.setBounds(0,0,120,30);
stop.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
move.interrupt();
Layout.running = false;
}
});
JPanel subPanel = new JPanel(); //Layout with its buttons situated to the south
subPanel.add(animate);
subPanel.add(stop);
add(subPanel,BorderLayout.SOUTH);
add(new Layout());
}
public static void main(String[] args) {
Animation ventana = new Animation();
ventana.setSize(850,625);
ventana.setLocationRelativeTo(null);
ventana.setVisible(true);
ventana.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e) {} //Tag
} //Class close
class Layout extends JPanel implements Runnable { //Layout and thread
int X,Y; //Coordenadas
static boolean running = true; //"To interrupt the thread" momentaneously.
static ArrayList<Image> balls = new ArrayList<>(); //Balls collection
#Override
public void run () { //Just moves ball towards Narnia xd
while(running) {
X++; Y++;
System.out.println(X+" "+Y);
repaint();
updateUI();
try {
Thread.sleep(4);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
repaint();
updateUI();
try {
URL url = new URL("https://www.freejpg.com.ar/image-900/9c/9ca2/F100004898-textura_pasto_verde_linea_de_cal.jpg");
Image picture = ImageIO.read(url);
g.drawImage(picture,0,0,null);
} catch(IOException e){
System.out.println("URL image was not found");
}
finally {
try {
//----------------------------------------------------------------------------
Image picture = ImageIO.read(new File("C:\\Users\\Home\\Desktop\\Cancha.jpg")); //Pitch
//----------------------------------------------------------------------------
g.drawImage(picture, 0, 0, null);
} catch (IOException ex) {
System.out.println("Pitch image was not found");
}
}
for (Image ball : balls) { //I add balls to the Layout
g2.drawImage(ball,X,Y,100,100,null);
}
}
public void createEllipse (Image ball) { //Method that adds balls to the collection
try {
//-------------------------------------------------------------------- Ball
ball = ImageIO.read(new File("C:\\Users\\Home\\Desktop\\Pelota.png")); //Change this
//-------------------------------------------------------------------- Ball
} catch(IOException ex) {
System.out.println("Any balls were found");
}
balls.add(ball);
}
}
So to break your code down:
When the button is pressed, you execute the following code:
Image ball = null;
new Layout().createEllipse(ball);
runnable = new Layout();
move = new Thread(runnable);
move.start();
This will create a new layout. The run() method of this will increase the X and Y variables. They are declared here:
int X,Y; //Coordenadas
Those are instance variables, this means they belong to your newly created Layout.
Then you call repaint() on the new Layout, which will do nothing, because this new Layout has not been added to some window.
So, how do you fix this?
First, you have to keep the original Layout around:
class Animation extends JFrame { // no need to implement ActionListener
Rectangle2D dimensions = new Rectangle2D.Double(0,0,850,595); //Not implemented limits
JButton animate, stop;
Thread move;
Layout layout;
Then remember the Layout when you create it:
// before: add(new Layout());
layout = new Layout();
add(layout);
Then use the layout in your ActionListener:
layout.createEllipse(ball);
move = new Thread(layout);
move.start();
This might have some problems with concurrency (Swing is not thread-safe), so for good measure, you should call repaint() in the AWTEventThread:
// in run(), was repaint():
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
repaint();
}
});
Now, there are some cleanup tasks left:
Delete this code:
#Override
public void actionPerformed(ActionEvent e) {} //Tag
It's no longer needed, because you don't implement ActionListener.
Drop the static modifiers from some fields, and add volatile:
volatile int X,Y; //Coordenadas
volatile boolean running = true; //"To interrupt the thread" momentaneously.
ArrayList<Image> balls = new ArrayList<>(); //Balls collection
volatile is needed for variables that are accessed from more than one thread.
Also remove repaint() and resetUI() from the paint method. You don't need them.
For the pictures in paint: you should cache them. Store them in a field, so you don't have to load the picture every time.
When all this is done, your code is much cleaner, but there are still some warts that should be addressed. But at least you have something working.
Johannes has already spoken about many of the things which are wrong with your original example, so I won't go over many of them again.
This example makes use of a Swing Timer instead of a Thread as the main "animation" loop. It also focuses on demonstrating encapsulation and responsibility.
For example, the AnimtionPane is responsible for managing the balls, managing the animation loop and paint. It isn't, however, responsible for determining "how" the balls are updated or paint, it only provides the timing and functionality to make those things happen.
A couple of the glaring issues I can see are:
Trying to load resources from within the paintComponent method. This is a bad ideas, as it could slow you paint pass down, causing your UI to lag
Calling repaint and updateUI from within the paintComponent method. You should avoid causing any new updates to the UI from occurring during a paint process. This could cause your program to run wide and consume all the CPU cycles, not only making your app non-responsive, but also the whole system.
Some very quick points
Swing is not thread safe. You should never update the UI (or anything the UI relies on) from outside the context of the Event Dispatching Thread. This example uses a Swing Timer as it allows the delay to occur of the EDT (and not block the UI), but it's updates are triggered within the EDT, allowing us to safely update the UI from within
You create multiple instances of Layout, meaning that the one on the screen isn't the one which is been updated
Your "freeze" logic is broken. It will never "freeze" anything
Runnable example
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private AnimationPane animationPane;
public TestPane() {
setLayout(new BorderLayout());
animationPane = new AnimationPane();
JButton actionButton = new JButton("Start");
actionButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
if (animationPane.isAnimating()) {
animationPane.stop();
actionButton.setText("Start");
} else {
animationPane.start();
actionButton.setText("Stop");
}
}
});
add(animationPane);
add(actionButton, BorderLayout.SOUTH);
}
}
// This is just makes it seem more random ;)
private static Random RANDOM = new Random();
public class Ball {
private int x;
private int y;
private int xDelta;
private int yDelta;
private Color color;
private Shape shape;
public Ball(Color color) {
shape = new Ellipse2D.Double(0, 0, 10, 10);
this.color = color;
// Get some random motion
do {
xDelta = RANDOM.nextInt(6) + 2;
yDelta = RANDOM.nextInt(6) + 2;
} while (xDelta == yDelta);
}
public void update(Rectangle bounds) {
x += xDelta;
y += yDelta;
if (x + 10 > bounds.x + bounds.width) {
x = bounds.x + bounds.width - 10;
xDelta *= -1;
} else if (x < bounds.x) {
x = bounds.x;
xDelta *= -1;
}
if (y + 10 > bounds.y + bounds.height) {
y = bounds.y + bounds.height - 10;
yDelta *= -1;
} else if (y < bounds.y) {
y = bounds.y;
yDelta *= -1;
}
}
public void paint(Graphics2D g2d) {
// This makes it easier to restore the graphics context
// back to it's original state
Graphics2D copy = (Graphics2D) g2d.create();
copy.setColor(color);
copy.translate(x, y);
copy.fill(shape);
// Don't need the copy any more, get rid of it
copy.dispose();
}
}
public class AnimationPane extends JPanel {
// This does not need to be static
private List<Ball> balls = new ArrayList<>(); //Balls collection
private Timer timer;
private List<Color> colors;
public AnimationPane() {
colors = new ArrayList<>(8);
colors.add(Color.RED);
colors.add(Color.GREEN);
colors.add(Color.BLUE);
colors.add(Color.CYAN);
colors.add(Color.MAGENTA);
colors.add(Color.ORANGE);
colors.add(Color.PINK);
colors.add(Color.YELLOW);
timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
if (RANDOM.nextBoolean()) {
makeBall();
}
Rectangle bounds = new Rectangle(new Point(0, 0), getSize());
for (Ball ball : balls) {
ball.update(bounds);
}
repaint();
}
});
makeBall();
}
protected void makeBall() {
Collections.shuffle(colors);
balls.add(new Ball(colors.get(0)));
}
public boolean isAnimating() {
return timer.isRunning();
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
// Bad ideas. Repaint will cause a new paint event to be posted, causing your
// UI to run away - consuming all your CPU cycles in a singulator forms
// and destorys the known universe
//repaint();
// This doesn't do what you think it does and there shouldn't be
// reason for you to call it
//updateUI();
// This is a bad idea as it could cause the paint cycles to slow down
// destorying the responsiveness of your app
// Besids, you should be passing this as the ImageObserver
// try {
// URL url = new URL("https://www.freejpg.com.ar/image-900/9c/9ca2/F100004898-textura_pasto_verde_linea_de_cal.jpg");
// Image picture = ImageIO.read(url);
// g.drawImage(picture, 0, 0, null);
// } catch (IOException e) {
// System.out.println("URL image was not found");
// } finally {
// try {
// //----------------------------------------------------------------------------
// Image picture = ImageIO.read(new File("C:\\Users\\Home\\Desktop\\Cancha.jpg")); //Pitch
// //----------------------------------------------------------------------------
// g.drawImage(picture, 0, 0, null);
// } catch (IOException ex) {
// System.out.println("Pitch image was not found");
// }
// }
// This is "bad" per say, but each ball should have it's own
// concept of location
// for (Image ball : balls) { //I add balls to the Layout
// g2.drawImage(ball, X, Y, 100, 100, null);
// }
for (Ball ball : balls) {
ball.paint(g2);
}
// I made a copy of the graphics context, as this is shared
// with all the other components been painted, changing the
// render hints could cause issues
g2.dispose();
}
}
}

Having trouble getting my MouseListener to work for a Pokemon game for high school

I have a class for the game where most of the rendering and framework is done. I have a class for the mouse listener. I also have a class called Menu that draws a menu on the canvas. I want it to actually start the game when I click on the "Start" button but it seems as though the MouseListener is not receiving the mouse click.
I have tried putting the line addMouseListener(new MouseInput()) in many places throughout the Game class but it will not work.
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MouseInput implements MouseListener
{
public void mousePressed(MouseEvent e)
{
int mx = e.getX();
int my = e.getY();
if(Game.STATE == 0)
{
if(mx >= 415 && mx <= 615)
{
if(my >= 350 && my <= 425)
{
Game.STATE = Game.STATE + 1;
}
}
if(mx >= 415 && mx <=615)
{
if(my >= 500 && my <= 575)
{
System.exit(1);
}
}
}
}
}
//Game Class
public class Game extends JFrame implements Runnable
{
private Canvas c = new Canvas();
public static int STATE = 0;
public static final int WIDTH = 1000;
public static final int HEIGHT = 800;
private Menu menu;
private FightState fight;
public Game()
{
//Forces program to close when panel is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//sets position and size of Frame
setBounds(0, 0, WIDTH, HEIGHT);
//puts Frame in center of the screen
setLocationRelativeTo(null);
//adds canvas to game
add(c);
//Makes frame visible
setVisible(true);
//creates our object for buffer strategy
c.createBufferStrategy(2);
// adds the mouse listner;
addMouseListener(new MouseInput());
}
public void update()
{
}
//renders the graphics onto the screen
public void render()
{
BufferStrategy bufferStrategy = c.getBufferStrategy();
Graphics g = bufferStrategy.getDrawGraphics();
super.paint(g);
//instantiates the menu object
menu = new Menu();
//instantiates the FightState object
fight = new FightState();
//renders the menu
if(STATE == 0)
{
menu.render(g);
}
//renders the fight stage
if(STATE == 1)
{
fight.render(g);
}
g.setFont(new Font("Monospaced", Font.PLAIN, 35));
g.drawString("STATE: " + STATE, 10, 400);
repaint();
//checks if mouseListener is working
System.out.print(STATE);
g.dispose();
bufferStrategy.show();
}
//game loop
public void run()
{
BufferStrategy bufferStrategy = c.getBufferStrategy();
long lastTime = System.nanoTime(); //long is an int that stores more space
double nanoSecondConvert = 1000000000.0 / 60; //frames/sec
double deltaSeconds = 0;
while(true)
{
long now = System.nanoTime();
deltaSeconds += (now-lastTime)/nanoSecondConvert;
while(deltaSeconds >=1)
{
update();
deltaSeconds = 0;
}
render();
lastTime = now;
System.out.println("STATE: " + STATE);
}
}
//main method
public static void main(String[] args)
{
Game game = new Game();
Thread gameThread = new Thread(game);
gameThread.start();
}
}
Don't call super.paint(g); on the JFrame if you're using a BufferStrategy. Just paint to the buffer directly. You should also be adding your MouseListener to the Canvas, not the frame.
The Canvas is laid out WITHIN the frame boundaries of the window, meaning it will be offset and smaller than the actual frame itself.
Mouse events are automatically converted to the sources coordinate context, this means, you are currently trying to compare values coming from the frame's coordinate context with values been used by the Canvas which are different
One question: How would i paint directly to the buffer if buffer doesnt have the "Graphics methods" such as fillRect()?
Graphics g = bufferStrategy.getDrawGraphics() gives you the Graphics context, you then paint directly to it.
You don’t want to call paint directly (ever) as it can be called by the system and you could up with race conditions and other issues
Swing uses a different painting algorithm, which you've opted out of by using a BufferStrategy, this means you can no longer make use of the "normal" Swing painting process and instead, must write your own

Why does my ball keep flashing?

I am still a student. I am trying to learn how to draw a ball and move by myself.
Here is the code :
import javax.swing.*;
import java.awt.*;
public class Ball extends JFrame
{
int x = 50;
int y = 50;
int rad = 30;
Ball(){
setSize(500,500);
setTitle("Ball");
setVisible(true);
}
void move()
{
if (x < getWidth() - rad){
x = x + 1 ;
}
try
{
Thread.sleep(100);
}
catch( Exception e)
{
}
}
public void paint( Graphics g)
{
super.paint(g);
g.fillOval(x,y,rad,rad);
}
public static void main(String args[])
{
Ball b = new Ball();
while(true){
b.move();
b.repaint();
}
}
}
I would say this code work 60% of it because
when i run the program the ball is moving to the right, but it keep flashing for some reason and i dont know why.
it is my computer problem , or the code or some kind of bug?
i am using eclipse luna
This is a very classic problem you see when the screen updates with only parts of the data you want it to show.
In this case, the JFrame's update(Graphics) clears the screen with a fillRect, then calls your paint(Graphics) which draws the ball with a fillOval.
If the screen updates between the fillRect and the fillOval, the ball will briefly disappear, causing the flashing (aka flickering).
The solution is double buffering, where all the graphics operations are drawn to an offscreen image, and then drawn to the window in one operation.
This is something you get for free with JPanel, so just modify your code to inherit from that instead of JFrame (this is good practice in any case). Here it is with minimal code changes:
import javax.swing.*;
import java.awt.*;
public class Ball extends JPanel
{
int x = 50;
int y = 50;
int rad = 30;
void move()
{
if (x < getWidth() - rad){
x = x + 1 ;
}
try
{
Thread.sleep(100);
}
catch( Exception e)
{
}
}
public void paint( Graphics g)
{
super.paint(g);
g.fillOval(x,y,rad,rad);
}
public static void main(String args[])
{
Ball b = new Ball();
JFrame frame = new JFrame();
frame.add(b);
frame.setSize(500,500);
frame.setVisible(true);
while(true){
b.move();
b.repaint();
}
}
}
This should be flicker free, but may still be jerky.
For smoother animation, you'd typically account for inter-frame timing and framedrops instead of just updating every 100ms and hoping it makes it into a timely repaint.

Updating JFrame

I have two classes which I use to paint a JFrame (see below).
I am trying to refresh the content so it gives the impression of the points randomly "moving". (Ie: Repainting fast enough)
Ideally, I would then like to pass in some parameters to specify at which coordinates the points should appear. However, all I get is a static image.
Any advice?
package uk.me.dariosdesk.dirtydemo;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
class DrawPanel extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
for (int i = 0; i <= 1000; i++) {
Dimension size = getSize();
Insets insets = getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
Random r = new Random();
int x = Math.abs(r.nextInt()) % w;
int y = Math.abs(r.nextInt()) % h;
g2d.drawLine(x, y, x, y);
}
g2d.fillRect(200, 250, 200, 250);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
And
package uk.me.dariosdesk.dirtydemo;
import javax.swing.*;
public class PointsExample extends JFrame {
public PointsExample() {
initUI();
}
public final void initUI() {
DrawPanel dpnl = new DrawPanel();
add(dpnl);
setSize(500, 500);
setTitle("Points");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
PointsExample ex = new PointsExample();
ex.setVisible(true);
for(int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ex.repaint();
}
}
});
}
}
"all I get is a static image" is very light on details. But I think LuxxMiner is right, Thread.Sleep on your Event Dispatch Thread is a bad idea. What's more, the Runnable never exits for 1000 seconds. So you are blocking the EDT for 1000 seconds.
What repaint Component.repaint does (emphasis mine):
If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.
This already signals that this method posts a message to the dispatch thread, which you are blocking with Thread.Sleep. What you can do instead is use a Swing Timer to ask for a repaint every second:
In general, we recommend using Swing timers rather than general-purpose timers for GUI-related tasks because Swing timers all share the same, pre-existing timer thread and the GUI-related task automatically executes on the event-dispatch thread.

Java Paint() issues

first post here ever so forgive me if I am totally ignorant of all the rules.
I have some issues, I am relatively new to Java and have read and got some help from this community before.
I am having issues at the moment paint multiple balls on a JFrame, I have some solutions from other students but to no success. One student has got it working now but by painting everything within the Frame class which I don't feel is correct and putting repaint() within paint() which also feels wrong. if anyone could point me in the right direction I would be extremely appreciative.
Daniel
Code:
Gamejava
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myanimie;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* #author Dan
*/
public class Game extends JFrame implements Runnable {
private Ball myBall = new Ball();
private Paddle myPad = new Paddle();
final JPanel jp = new JPanel();
final JPanel jp1 = new JPanel();
final JPanel jp2 = new JPanel();
public Game()
{
setVisible(true);
setResizable(false);
setTitle("First Test Animation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setSize(640,480);
}
public void run()
{
move();
}
public void paint(Graphics g)
{
super.paint(g);
myBall.paint(g);
}
public void move()
{
myBall.start();
repaint();
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
System.exit(0);
}
}
}
Ball.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myanimie;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
/**
*
* #author Dan
*/
public class Ball extends Thread {
Point pos;
Color ballColor = Color.red;
Color[] ts = {Color.CYAN,Color.green,Color.black};
private int yChange = 2;
private int xChange = 1;
public Ball()
{
pos = new Point();
pos.x = (int)(Math.random() * (500 - 100)) + 10 ;
pos.y = (int)(Math.random() * (500/2 - 100)) + 10;
}
#Override
public void run()
{
while(true)
{
move();
}
}
public void paint(Graphics g)
{
g.setColor(ballColor);
g.fillOval(pos.x - 10, pos.y - 10, 60,60);
}
public void move()
{
// System.out.println("y " + pos.y);
// System.out.println("x " + pos.x);
if(pos.y < 20)
{
yChange = -yChange;
System.out.println("T");
ballColor = Color.BLUE;
}
if(pos.x < 20)
{
xChange = -xChange;
System.out.println("L");
ballColor = Color.MAGENTA;
}
if(pos.x > 620 - 20)
{
xChange = -xChange;
System.out.println("R");
ballColor = Color.GREEN;
}
if(pos.y > 430 - 20)
{
yChange = -yChange;
System.out.println("B");
ballColor = Color.PINK;
}
if(pos.y < 640 - 20)
{
pos.translate(xChange, yChange);
}
if(pos.x < 480 - 20 || pos.x > 460)
{
pos.translate(xChange, yChange);
}
}
public Point getPosition()
{
return pos;
}
public Ellipse2D area()
{
return new Ellipse2D.Double(pos.x, pos.y,60,60);
}
}
This is my terrible code, I have gotten around the errors but no animation atm.
Thanks guys!! your insight is invaluable
"and putting repaint() within paint()" sounds dangerous!
repaint() method causes a call to this component's paint method as soon as possible.
You may elaborate how you are trying to draw "multiple" balls on the frame. If nothing special, your answer may be here
OK some general observations based on the code you posted.
You need to throttle your move() method so that it's not updating millions of times per second. I see you have a Thread.sleep() in your Game.move method. I think what you were trying to do was something like this:
public void run() {
while(true) {
//change the game state
move();
//draw the changes to the state that I just made
repaint();
//wait before moving to the next "frame"
try {
Thread.sleep(50);
} catch ( InterruptedException ie ) {
}
}
}
private void move() {
myBall.move();
}
Since Ball.move() won't loop anymore, don't have Ball extend Thread at all. Get rid of the run() method. Your main game loop will be managed centrally by Game.
From your main method or whoever starts the game running, call Game.run() either in that thread or in a new thread like this:
public void main(String[] args) {
Game game /* = new Game(...)*/;
Thread gameThread = new Thread(game);
gameThread.start();
}
Is there a good reason for getPosition() to exist? This is probably better off as encapsulated state.
Yes it is indeed very bad practice. Calling repaint() in paint() will cause a StackOverflow Error.
Take a look at the game engine Bonsai from Ivo Wetzel. I like it a lot.
And indeed: You should create a JComponent which overrides the paintComponent(Graphics g) method. Add that JComponent to the JFrame. But you don't have to care about this. This will do the Bonsai game engine automatically.

Categories

Resources