Hey, I have a problem with my rendered JButtons in my applications. It seems that when I render them on a decorated JFrame that they are drawn according to the graphics coordinate system, i.e. starting from 0,0 behind the top bars, but the mouse responds on them as they were placed according to the frame itself. Best shown with an example I guess:
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("I am an annoying BUTTON");
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;
}
}
The button appears under the topbar, but can be click as though it was a few pixels below. It works correctly with setUndecorated(true), but I want it to be decorated :’( How do I get around that problem?