Using awt Paint in Multi threading - java

I have a school project and i want to dedicate this for New Year, the project that i came up is a Text-Firework, i am using characters and symbols as the Explosion particles, and just constantly changing their X and Y position inside Paint().
I am confused on how to use Paint and Thread together. The problem is it's not painting on the screen or maybe the thread is not starting. (i cant really tell, im sorry). the problem is i dont get any error, it just doesn't work :(
the code is a little bit long i think, thank you for reading it.
How it should Works: When a user click, a Firework Thread will be started on the mouse position,
this Firework class has a paint loop for recreating the incremental explosion. so basically, i want the user to create multiple explosions thats why i made it a Thread.
here is the main applet:
public class TBFireworks extends Applet implements MouseListener
{
public void init()
{
setBackground( Color.black );
addMouseListener( this );
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) { }
public void mouseReleased( MouseEvent e ) { }
public void mouseClicked( MouseEvent e )
{
new Firework( e.getX(),e.getY(), this);
}
}
and the Firework Thread class:
class Firework extends Thread
{
Point center = new Point(0,0);
int blastRadius = 10;
Point posIncrement = new Point(0,0);
Applet applet;
public Firework(int positionX, int positionY, Applet apple)
{
center.x = positionX;
center.y = positionY;
applet = apple;
new Thread(this).start();
}
public void run()
{
while(blastRadius > 0)
{
applet.paint(applet.getGraphics());
try {
this.sleep(1000/20);
} catch (InterruptedException e) { ; }
}
}
public void paint(Graphics g)
{
if(blastRadius > 0)
{
Point[] fakeFire = {new Point(20,20),new Point(20,30),new Point(30,20)};
ApplyNextPos(fakeFire,posIncrement);
g.setColor(Color.red);
for(int xaa=1; xaa<5; xaa++) // draw the formation
{
for(int zaa=0;zaa<fakeFire.length;zaa++)
{
fakeFire[zaa] = GetQuadrant(xaa,center,fakeFire[zaa]);
}
for(int yaa=0;yaa<fakeFire.length;yaa++)
{
g.drawString("*",fakeFire[yaa].x,fakeFire[yaa].y);
}
}
posIncrement.incrementPos(5);
blastRadius--;
}
}
}

First, you seem not to be using your paint-method in the FireWork thread, you call the applet's paint method instead.
I'm a bit rosty in the applet and AWT stuff, but if it were Swing (I guess it not that different), I would suggest another approach. Painting should (can?) only be done in the EDT (Event Dispatch Thread). When the user clicks, you create a similar object to FireWork, and add that to a list. Then you start ONE thread (if not already started, that continously calls repaint on some panel. Then in the paint-method of your panel you loop the list of all fireworks and draw them.
This will also be more memory efficient, since you only use one thread.

The paint method should (normaly) only be called by the GUI when the corresponding component or part of it needs to be (re-)painted. It should not be called by the application (if not from inside another paint method).
The paint method is called to draw the actual state of the component. The state should be changed by another method/thread. The repainting of the component is forced by calling repaint.
An incomplete, untested example:
public class Animation extends Canvas {
private final List<Firework> fireworks = new ArrayList<Firework>();
public void start() {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
while (true) {
step();
Thread.sleep(STEP_TIME); // InterruptedException ?
}
}
});
t.start();
}
#Override
public void paint(Graphics g) {
super.paint(g);
for (Firework fw : fireworks)
fw.draw(g); // draw the Firework
}
private void step() {
for (Firework fw : fireworks)
fw.step(); // update/move the Firework
repaint();
}
// methods for adding/deleting Fireworks, synchronization?
}

Related

Leave Event Dispatch Thread entry ONLY on key press (Java)

I understand that it is important to use the Event Dispatch Thread for any changes to the interface in Java. However, I have no idea how I can manipulate these events to stop/continue/start. I want to refrain from moving on to the next line of main() (after the ones which put the Runnable in the EventQueue) until a certain key is pressed.
I put together an example for clarity. What I'd like to do here is spawn the JFrame, allow the user to move the box around with the arrow keys and then press Enter to cease the box-shifting operations, and ONLY then make the calculation at the end of main() and cause the answer to appear. I should be able to get 400, 500, 600, etc. As it is, the calculation is made immediately after the JFrame appears, so the answer is always 300.
I carved out a spot for whatever action should be bound to Enter; it's underneath the declarations for the actions bound to the arrow keys.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EndTheShifter extends JFrame
{
private Color ourRectColor = new Color(28,222,144);
private int ourRectWidth = 50;
private int ourRectHeight = 50;
protected static Point ourRecLocation = new Point(100,100);
// Rectangle object can paint itself
public class Rectangle
{
protected void paint(Graphics2D g2d)
{
g2d.setColor(ourRectColor);
g2d.fillRect(ourRecLocation.x, ourRecLocation.y, ourRectWidth, ourRectHeight);
}
} // Rectangle class
// OurRectangle can create a Rectangle and call paint() on it
public class OurRectangle extends JPanel
{
private Rectangle capableRectangle;
public OurRectangle()
{
capableRectangle = new Rectangle();
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
capableRectangle.paint(g2d);
g2d.dispose();
}
} // OurRectangle class
KeyStroke pressRight = KeyStroke.getKeyStroke("RIGHT");
KeyStroke pressLeft = KeyStroke.getKeyStroke("LEFT");
KeyStroke pressUp = KeyStroke.getKeyStroke("UP");
KeyStroke pressDown = KeyStroke.getKeyStroke("DOWN");
KeyStroke pressEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0);
OurRectangle recToWorkWith = new OurRectangle();
// Create InputMap and ActionMap
InputMap inputMap = recToWorkWith.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = recToWorkWith.getActionMap();
// Mapping Shortcut
protected void setTheAction(KeyStroke a, String b, Action c)
{
inputMap.put(a,b);
actionMap.put(b,c);
}
// Constructor!!!
public EndTheShifter()
{
add(recToWorkWith);
Action rightAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if(ourRecLocation.x != 600)
ourRecLocation.x += 50;
else
ourRecLocation.x = 100;
recToWorkWith.repaint();
}
};
Action leftAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if(ourRecLocation.x != 100)
ourRecLocation.x -= 50;
else
ourRecLocation.x = 600;
recToWorkWith.repaint();
}
};
Action downAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if(ourRecLocation.y != 600)
ourRecLocation.y += 50;
else
ourRecLocation.y = 100;
recToWorkWith.repaint();
}
};
Action upAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if(ourRecLocation.y != 100)
ourRecLocation.y -= 50;
else
ourRecLocation.y = 600;
recToWorkWith.repaint();
}
};
/*
Action enterAction = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
}
}
setTheAction(pressEnter,"enterAction",enterAction);
*/
setTheAction(pressRight,"rightAction",rightAction);
setTheAction(pressLeft,"leftAction",leftAction);
setTheAction(pressDown,"downAction",downAction);
setTheAction(pressUp,"upAction",upAction);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,800);
setVisible(true);
}
// Main kicks things off by putting all of the above
// in the Event Dispatch thread
// On an enter press, I want the last line of main() to run
public static void main(String[] argv)
{
EventQueue.invokeLater(
new Runnable()
{
#Override
public void run()
{
new EndTheShifter();
}
});
// What I want to trigger only on Enter
System.out.println(ourRecLocation.x + 2*ourRecLocation.y);
}
} // EndTheShifter, our outermost class
and ONLY then make the calculation at the end of main()
That is not the way a GUI works.
The main() method is only used to display the frame.
Once the frame is visible the EDT is started and the frame sits there waiting for user events to be generated.
Your application code then responds to these user events.
I understand that it is important to use the Event Dispatch Thread for any changes to the interface in Java.
All code invoked in a listener does execute on the EDT. So the code in your Action does execute on the EDT. You don't need to do anything special.
What I want to trigger only on Enter
Then that logic should be contained in the Enter Action.
I would like to support what camickr said; there is likely a better way to achieve what you are trying to do. That said, if you really want to make your main method wait until the enter key is pressed, here's how:
First, at the top of your file, define an object to use as a synchronization lock like so:
public static final Object LOCK = new Object();
Then, in your main method, before your println statement, put the following code:
synchronized (LOCK) {
LOCK.wait();
}
What this does is it waits until the LOCK object's monitor lock is not being used by any thread (very simplified explanation, read more here), and then it makes the current thread (in this case, the thread that started your main method) wait indefinitely.
Next, add a throws declaration to the method header on your main method:
public static void main(String[] argv) throws InterruptedException
This tells the compiler that your code could throw an InterruptedException, which would happen if your thread was interrupted while it was waiting.
Finally, anywhere in your EndTheShifter constructor, put the following code:
synchronized (LOCK) {
LOCK.notify();
}
This again waits until the LOCK object's monitor lock becomes available, and it then "notifies" all threads waiting on the LOCK object that they may continue. In this case, it will make our main thread continue and execute the println.

Why is my while loop not working in paintComponent?

When I run this code, I see nothing but a blank(white) Panel and I would like to know why.
Here is my code:
Graph.java
public class Graph extends JPanel {
private static final long serialVersionUID = -397959590385297067L;
int screen=-1;
int x=10;
int y=10;
int dx=1;
int dy=1;
boolean shouldrun=true;
imageStream imget=new imageStream();
protected void Loader(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0,x,y);
x=x+1;
y=y+2;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
while(shouldrun){
Loader(g);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Do not ever call Thread.sleep() on the Event Dispatch Thread!!!
This causes the thread that actually redraws the screen and makes controls responsive to stop doing anything.
For animations, use a Timer. Don't worry about writing the while loop yourself, just tell the Timer to fire every so often, and change the values of x and y inside that timer. Something like:
// this is an **inner** class of Graph
public class TimerActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
}
}
// snip
private final Timer yourTimer;
public Graph() {
yourTimer = new Timer(2000, new TimerActionListener());
timer.start();
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0,0,x,y);
}
You never change the state of shouldrun within the loop -- so it will never end.
Also, never call Thread.sleep(...) within a painting method. This method is for painting and can never be put to sleep, else the GUI will be put to sleep, will be frozen.
First of all, your paintComponent method should only handle all painting and nothing else (if possible). You should not implement your program loop within paintComponent.
The blank screen can be caused by a number of reasons. You can easily debug it manually by commenting off certain section of your codes and run it. See whether it is still blank.
At least from what I see here, your paintComponent will give your problems.
If you want an animation, you can:
Use a swing timer
Create a loop in a new thread (not Event Dispatch Thread). Your loop will look something like this:
As below:
while(running){
update();
render();
try(
Thread.sleep(1000/fps);
)catch(InterruptedException ie){
ie.printStackTrace();
}
}
Note: To make a proper loop for animation, you will need more than that.

repaint method for moving an object randomly

I am trying to move an object randomly. I have my GUI class which uses another class (lets say Obj) to create a new image and then start the thread to make the object move randomly. But my repaint() does not work in this context. The code below can give you an idea about how I am using the repaint method.
thanks,
Gui class
public class GUI extends JFrame implements ActionListener {
public void addNewObj(){
Obj f = new Obj();
x = panel.getGraphics();
f.paint(x);
Thread thr=new Thread(f);
thr.start();
}
}
Create object class
public class Obj extends JPanel implements Runnable
{
public Obj()
{
try {
myImage = ImageIO.read(new File("b:\\imgs\\bottle.jpg"));
}
catch (IOException e) {}
}
public void run()
{
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true)
{
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
try
{
moveRandom();
repaint();
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
The problem is very basic: Never paint an object in the way you do. You should add it to the frame or an container. This is also the reason, why repaint() doesn't work. Your object never makes it into the componenthierachy, and therefore repaint will only repaint this single object, but nothing else (including the frame, which should be repainted). Simply add the object directly to the frame, validate and repaint the frame.
the new addNewObj:
public void addNewObj(){
Obj f = new Obj();
Thread t = new Thread(f);
t.start();
panel.add(f);//add it to the panel
panel.validate();//validate the hierachy
panel.repaint();//repaint the whole thing to make the new obj visible
}
And override your Obj class to paint the objects:
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(myImage , 0 , 0 , Color.white , null);
}

Java 2D Game: repaint(); makes window grey

I'm trying to make a 2D game in Java, but when I call the repaint() method in a thread there's an odd grey-only window.
Here's the source code I have so far:
Spaceshooter.java
package spaceshooter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Spaceshooter extends JFrame implements KeyListener, Runnable {
private Player player = new Player(5, 186, this);
private boolean up, down;
public Spaceshooter(String title) {
super(title);
this.setFocusable(true);
this.addKeyListener(this);
}
#Override
public void paint(Graphics gr) {
super.paint(gr);
gr.setColor(Color.BLACK);
gr.fillRect(0, 0, 800, 500);
player.paintPlayer(gr);
}
public static void main(String[] args) {
Spaceshooter shooter = new Spaceshooter("Spaceshooter");
new Thread(shooter).start();
shooter.setSize(800,500);
shooter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shooter.setVisible(true);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 38) {
up = true;
down = false;
} else if (e.getKeyCode() == 40) {
down = true;
up = false;
}
}
#Override
public void keyReleased(KeyEvent e) {
down = false;
up = false;
}
#Override
public void run() {
while(true) {
if (up) {
player.moveUp();
} else if (down) {
player.moveDown();
}
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
Logger.getLogger(Spaceshooter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Player.java
package spaceshooter;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Toolkit;
public class Player {
private int x, y;
private Component comp;
public Player(int x, int y, Component comp) {
this.x = x;
this.y = y;
this.comp = comp;
}
public void moveUp() {
y -= 5;
}
public void moveDown() {
y += 5;
}
public void paintPlayer(Graphics gr) {
gr.drawImage(Toolkit.getDefaultToolkit().getImage("images/player.png"), x, y, comp);
}
}
Thanks for your answers in advance!
What is EDT ?
Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. This is necessary because most Swing object methods are not "thread safe". All GUI related task, any update should be made to GUI while painting process must happen on the EDT, which involves wrapping the request in an event and processing it onto the EventQueue. Then the event are dispatched from the same queue in the one by one in order they en-queued, FIRST IN FIRST OUT. That is, if That is, if Event A is enqueued to the EventQueue before Event B then event B will not be dispatched before event A.
Any task you perform which may take a while, likely to block the EDT, no dispatching will happen, no update will be made and hence your application FREEZES. You will have to kill it to get rid of this freezing state.
In your program, aside from creating your JFrame and making it to visible from the main thread (which we also should not do):
while(true) {
if (up) {
player.moveUp();
} else if (down) {
player.moveDown();
}
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
Logger.getLogger(Spaceshooter.class.getName()).log(Level.SEVERE, null, ex);
}
}
you are posting repaint() request from the thread which you sent to sleep right immediately after that.
SwingUtilities.invokeLater():
Swing provides a nice function SwingUtilities.invokeLater(new Runnable(){}) for posting repaint request to the EDT. All you have to do is to write:
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
repaint();
}
});
Now some more thing to mention:
We should not implements a GUI component with Runnable. Make another class implementing Runnable, make your computation inside that then use SwingUtilities to post the component update request.
we should not made custom painting on JFrame directly. JFrame is a top level component. It is more like a container which contains your whole app. If you want custom painting use a custom component MyCanvas extends JComponent.
we should not override paint() function. Instead paintComponent(g) will serve our custom painting purposes nicely.
Learn to use Swing Timer class for timely repeated GUI rendering task.
Tutorial Resource and References:
The Event Dispatch Thread
EventQueue
How to Use Swing Timers
Lesson: Performing Custom Painting

Clicking out of a Canvas

I am making a Pong program, and I have a start button that begins to draw everything, and quite literally to get the ball rolling (you're welcome for the pun). Anyways, when I hit the start button, a Key Listener to move the paddles won't work unless I click the mouse somewhere on the canvas to give it priority. Is there some sort of code to automatically "click" on the canvas without the user being hassled to do so? Thanks in advance.
This Is running awt by the way. I realize I should learn swing, but never got around to it.
public class Pong extends Applet implements ActionListener, KeyListener
{
Canvas c1;
Graphics myG;
Button start;
ball ball;
paddle LPaddle;
paddle RPaddle;
public void init()
{
this.setSize(1300,700);
c1 = new Canvas();
add(c1);
c1.addKeyListener(this);
c1.setBackground(Color.pink);
start = new Button("Start");
add(start);
start.addActionListener(this);
ball = new ball();
LPaddle = new paddle();
RPaddle = new paddle();
myG = c1.getGraphics();
}
public void paint(Graphics g)
{
c1.setLocation(0,0);
c1.setSize(1251,700);
start.setLocation(1255,350);
start.setSize(40,20);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)//up
{
RPaddle.erasePaddle(myG);
RPaddle.movePaddleUp();
RPaddle.drawPaddle(myG);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)//down
{
RPaddle.erasePaddle(myG);
RPaddle.movePaddleDown();
RPaddle.drawPaddle(myG);
}
if(e.getKeyCode()==KeyEvent.VK_W)
{
LPaddle.erasePaddle(myG);
LPaddle.movePaddleUp();
LPaddle.drawPaddle(myG);
}
if(e.getKeyCode()==KeyEvent.VK_S)
{
LPaddle.erasePaddle(myG);
LPaddle.movePaddleDown();
LPaddle.drawPaddle(myG);
}
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
myG.drawLine(625,0,625,700);
LPaddle.setInitial(150,0,350);
RPaddle.setInitial(150,1250,350);
LPaddle.drawPaddle(myG);
RPaddle.drawPaddle(myG);
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==start)
{
myG.drawLine(625,0,625,700);
LPaddle.setInitial(150,0,350);
RPaddle.setInitial(150,1250,350);
LPaddle.drawPaddle(myG);
RPaddle.drawPaddle(myG);
}
}
KeyListeners are low level interfaces which have a major, significant draw back: The componet they are registered to must be focusable and be focused.
By clicking the start button, you are giving the button focus.
You could call requestFocusInWindow on the instance of the canvas, but this assumes that the the canvas is focusable in the first place.
If you can, you'd be better of using a JComponent/JPanel as the base for your game canvas and use the key bindings API.

Categories