The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » Swing & AWT

Thread: How to Use BufferStrategy with awt Frame and Canvas with 2+ threads

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 3 - Last Post: Nov 30, 2008 6:58 PM by: travis_j_t Threads: [ Previous | Next ]
travis_j_t

Posts: 3
How to Use BufferStrategy with awt Frame and Canvas with 2+ threads
Posted: Nov 29, 2008 2:16 AM
  Click to reply to this thread Reply

Hello,

Here is my issue, i am pretty new to graphics with java so bare with me if you can. I pretty quickly managed to use the standard paint( ) repaint( ) for graphics in java, but wasn't happy with the speed and look of it. i did some reading and found BufferStrategy( ) and was happy to find a simple solutions for double buffering. my issue is poor examples for somebody learning as i am. incomplete, using different components, or not using threads, are some examples of the missing help i am looking for. i see suggestions from people and with i take them i have to try to Frankenstein the examples together.

so this is what i would like to see.
-Multiple threads, 2+
-use java awt frame, and canvas.
-be full screen compatible ( with little editing)
-and be simple, i will be making this for specific computers so a bunch of checking for
best settings to me seem unnecessary.

so i will post my existing attempt at a framework, so hopefully it wont be to embarrassing, but i would really appreciate help, plus i hope this will answer some questions for people in the future.

i want to add that my code wont currently compile.

Thanks Travis


import java.awt.*;
import java.awt.Graphics.*;
import java.awt.GraphicsConfiguration.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;

public class MyInstrument extends Canvas{

final int FRAMEWIDTH = 800;
final int FRAMEHEIGHT = 480;

BufferStrategy buffStrat = null;
Graphics graphics = null;

public MyInstrument(){


Frame frame = new Frame();
Canvas canvas = new Canvas();
frame.add(canvas);
frame.setSize(FRAMEWIDTH, FRAMEHEIGHT);
frame.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});

frame.setIgnoreRepaint(true);
frame.createBufferStrategy(2);
frame.setVisible(true);

renderThread rendThrd = new renderThread();
calcThread calcThrd = new calcThread();

rendThrd.start();
calcThrd.start();
}

public void renderLoop() {

if (buffStrat == null) {
buffStrat = getBufferStrategy();
}

try{

graphics = buffStrat.getDrawGraphics();

graphics.fillOval(500, 200, 100, 100);

}finally{
graphics.dispose();
}

buffStrat.show();
Toolkit.getDefaultToolkit().sync();
}

public void calcLoop() {

}

private class renderThread extends Thread {

@Override
public void run() {
while (true) {

try {

renderLoop();

System.out.println("Render");
Thread.sleep(20);

} catch (Exception e) {
break;
}
}
}
}

private class calcThread extends Thread {

@Override
public void run() {
while (true) {

try {

calcLoop();

System.out.println("Calc");

Thread.sleep(20);

} catch (Exception e) {
break;
}
}
}
}

public static void main(String[] args) {
new MyInstrument();

}

}

linuxhippy

Posts: 646
Re: How to Use BufferStrategy with awt Frame and Canvas with 2+ threads
Posted: Nov 29, 2008 4:30 AM   in response to: travis_j_t
  Click to reply to this thread Reply

Its often not a really good idea to render with two threads simultaneously to a Surface, it will be synchronized anyway.

The only thing that could happen is:
- Lock contention
- Higher pipeline validation overhead

- Clemens

travis_j_t

Posts: 3
Re: How to Use BufferStrategy with awt Frame and Canvas with 2+ threads
Posted: Nov 29, 2008 12:26 PM   in response to: linuxhippy
  Click to reply to this thread Reply

sorry, if you mean both threads doing rendering, i don't plan on it. i was hoping to keep my calculation as close to real time as possible by having a specific thread just for it. i will soon need to add serial communications for receiving the data as well. the data will pass from the interrupt to the calc thread.

my biggest issue is trying to learn from the tutorials and examples about buffer strategy. is there any obvious things i am doing wrong.

thanks
Travis

travis_j_t

Posts: 3
Re: How to Use BufferStrategy with awt Frame and Canvas with 2+ threads
Posted: Nov 30, 2008 6:58 PM   in response to: travis_j_t
  Click to reply to this thread Reply

does anybody have a example of BufferStragety with awt Frame and maybe with canvas and at least 2 threads. that is pretty much what i am looking for.




 XML java.net RSS