ok I’m pretty much frustrated now… I’ve been trying to get it working all day now. I’ve also made a simple example and swing components draw fine but they don’t get any action listeners. JTextFields are not focusable, JButtons aren’t clickable and so on… they are just drawn. So please someone take a look at the code, it’s very similar to sample Keith gave, and tell me what’s wrong.
2 classes, each in it’s file, undecorated JFrame as fullscreen app, turn of with alt+f4
Viktorije.java:
/*
* Created on 2006.10.21 by Kova
*/
package viktorije;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Viktorije extends JFrame {
public final static int DEFAULT_FPS = 60;
public static int res_height;
public static int res_width;
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
// MAIN
public static void main(String[] args) {
int fps = DEFAULT_FPS;
long period = (long) 1000.0/fps;
System.out.println("fps: " + fps + "; period: " + period + " ms");
new Viktorije("Viktorije", period*1000000L); // ms --> nanosecs
} // end: main()
// CONSTRUCTOR
public Viktorije(String title, long period) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
EventQueue.invokeAndWait(new Runnable(){
public void run() {
NullRepaintManager.install(); // this installs the NullRepaintManager on the AWT's Event Dispatch Thread
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (java.lang.reflect.InvocationTargetException e) {
e.printStackTrace();
}
SynchronizedEventQueue.use(); // this stops the AWT's Event Dispatch Thread from modifying the Swing menus at the same time we paint them.
setIgnoreRepaint(true);
setUndecorated(true);
Dimension screen_dim = Toolkit.getDefaultToolkit().getScreenSize();
res_width = (int)screen_dim.getWidth();
res_height = (int)screen_dim.getHeight();
setBounds(0,0,res_width,res_height);
setResizable(false);
setVisible(true); // must be called before createBufferStrategy() or exception is thrown, don't know why
synchronized(SynchronizedEventQueue.MUTEX){
createBufferStrategy(2);
}
ViktorijePanel viktorije_panel = new ViktorijePanel(this, period);
setContentPane(viktorije_panel);
viktorije_panel.startAnim(); // Kova: starts game loop thread
}
}
ViktorijePanel.java:
package viktorije;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ViktorijePanel extends JPanel implements Runnable {
private Thread animator;
private volatile boolean running = false;
private boolean game_over = false;
private GraphicsDevice device;
private DisplayMode display_mode;
private BufferStrategy buffer_strategy;
private Viktorije viktorije;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
public ViktorijePanel(Viktorije viktorije, long period) {
this.viktorije = viktorije;
this.buffer_strategy = viktorije.getBufferStrategy();
setIgnoreRepaint(true);
setOpaque(true);
setLayout(null);
JTextField tf = new JTextField("TEST ... use ALT+F4 to shutdown");
JButton btn = new JButton("press this to exit");
btn.setBounds(200,300,150,25);
// btn.addActionListener(new ActionListener() { // dosen't work, but even if it works this shouldn't be neccessarry
// public void actionPerformed(ActionEvent e) {
// System.exit(0);
// }
// });
tf.setBounds(200,200,200,20);
add(tf);
add(btn);
// viktorije.requestFocus(); // dosen't work
// this.requestFocus(); // also dosen'w work
} // end: constructor
public void paint(Graphics g) {
System.out.println("ViktorijePanel.paint(): this should rarely (best never) be called");
}
// Kova: start main game thread ("Viktorije Animator")
public void startAnim() {
if (animator == null || !running) {
animator = new Thread(this, "Viktorije Animator");
animator.start();
}
} // kraj: startAnim()
// Kova: start main game thread
public void run() {
running = true;
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
display_mode = new DisplayMode(1024,768,32,DisplayMode.REFRESH_RATE_UNKNOWN);
System.out.println("device.isFullScreenSupported() == " + device.isFullScreenSupported());
while(running) {
gameUpdate(); // Kova: update game state
gameRender(); // Kova: render the game to a buffer
paintScreen(); // Kova: draw the buffer on-screen
sleep();
}
} // end: run()
private void sleep() {
try {
Thread.sleep(20); // Kova: 50 fps
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Kova: update game state for 1 tick
private void gameUpdate() {}
private void gameRender() {
Graphics2D g2d = null;
synchronized (SynchronizedEventQueue.MUTEX){
g2d = (Graphics2D)buffer_strategy.getDrawGraphics(); // CommanderKeith: must sync this or else there can be Exceptions thrown when resizing window (especially in OGL mode).
}
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, Viktorije.res_width, Viktorije.res_height); // Kova: clear the background
g2d.setColor(Color.white);
g2d.fillRect(0, 45, Viktorije.res_width, 6);
g2d.setColor(Color.BLACK);
g2d.drawString("say: ", 25, 725);
synchronized (SynchronizedEventQueue.MUTEX){
this.paintComponents(g2d);
viktorije.getGlassPane().paint(g2d); // paint glass pane to paint tool tips
}
} // end of gameRender()
private void paintScreen() {
try {
buffer_strategy.show();
Toolkit.getDefaultToolkit().sync(); // Sync the display on some systems (on Linux, this fixes event queue problems)
}
catch (Exception e) { // quite commonly seen at applet destruction
System.out.println("Graphics error: " + e);
e.printStackTrace();
}
} // end of paintScreen()
public void setFullScreen(boolean fullScreen) throws IllegalArgumentException {
if (isFullScreen() == fullScreen) {
return; // CommanderKeith: we're already in fullscreen, so return.
}
if (fullScreen) {
viktorije.setResizable(false);
device.setFullScreenWindow(viktorije);
if (display_mode != null && device.isDisplayChangeSupported()) {
try {
device.setDisplayMode(display_mode);
System.out.println("display mode set");
} catch (IllegalArgumentException e) {
System.out.println("display mode not supported");
device.setFullScreenWindow(null);
viktorije.setResizable(true);
throw e;
}
} else {
device.setFullScreenWindow(null);
viktorije.setResizable(true);
throw new IllegalArgumentException("device.isDisplayChangeSupported() == false");
}
} else {
device.setFullScreenWindow(null);
viktorije.setResizable(true);
}
viktorije.createBufferStrategy(2);
this.buffer_strategy = viktorije.getBufferStrategy();
}
public boolean isFullScreen() {
if (device.getFullScreenWindow() == null){
return false;
}
return true;
}
} // end: ViktorijePanel