|
Replies:
3
-
Last Post:
Nov 30, 2008 6:58 PM
by: travis_j_t
|
Threads:
[
Previous
|
Next
]
|
|
|
|
|
|
How to Use BufferStrategy with awt Frame and Canvas with 2+ threads
Posted:
Nov 29, 2008 2:16 AM
|
|
|
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(); }
}
|
|
|
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
|
Re: How to Use BufferStrategy with awt Frame and Canvas with 2+ threads
Posted:
Nov 29, 2008 12:26 PM
in response to: linuxhippy
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
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.
|
|
|
|
|