Problem med rendered JButtons
Jeg har et problem med JButtons som jeg tegner i min renderloop. Hvis jeg bruger en undecorated JFrame som underflade virker det fint, men sætter jeg den til false bliver knapperne rendered efter graphics koordinat systemet, men musen registrere dem som værende placeret anderledes. Bedst forklaret med et eksempel:import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestJavaForum {
public static void main(String[] args) {
new TestJavaForum().init();
}
private TestJavaForumScreen screen;
private JButton testButton;
public void init() {
screen = new TestJavaForumScreen();
screen.initiateScreen();
testButton = new JButton("Quit");
JFrame frame = screen.getScreen();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(testButton);
renderLoop();
}
public void draw(Graphics2D g) {
g.setColor(Color.GREEN);
g.fillRect(0, 0, screen.getScreen().getWidth(), screen.getScreen()
.getHeight());
screen.getScreen().getLayeredPane().paintComponents(g);
}
public void renderLoop() {
while (true) {
Graphics2D g = screen.getGraphics();
draw(g);
g.dispose();
screen.update();
try {
Thread.sleep(15);
} catch (InterruptedException ex) {
}
}
}
}
class TestJavaForumScreen {
private GraphicsDevice device;
private JFrame screen;
public TestJavaForumScreen() {
GraphicsEnvironment environment = GraphicsEnvironment
.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
screen = new JFrame(device.getDefaultConfiguration());
}
public void initiateScreen() {
screen.setUndecorated(false); // < - Problem
screen.setResizable(false);
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setSize(320, 200);
screen.setVisible(true);
screen.createBufferStrategy(2);
}
public Graphics2D getGraphics() {
BufferStrategy strategy = screen.getBufferStrategy();
return (Graphics2D) strategy.getDrawGraphics();
}
public void update() {
BufferStrategy strategy = screen.getBufferStrategy();
if (!strategy.contentsLost()) {
strategy.show();
}
Toolkit.getDefaultToolkit().sync();
}
public JFrame getScreen() {
return screen;
}
}