Graphics rendering in title bar
The graphics keep rendering in the title bar. I use a buffered Image encapsulated in a jlabel and use the resultant graphic objects to draw rectangles in my code. This is the important part of the jframe class constructor:
super();
BufferedImage image=new BufferedImage(680,581,BufferedImage.TYPE_INT_ARGB);
m_graphicsObject =image.getGraphics();
JLabel label=new JLabel(new ImageIcon(image));
// buttons, mouse events and other controls use listeners to handle actions
// these listener are classes
btn1 = new JButton("Go!");
//btn1.setPreferredSize(new Dimension(100, 30));
btn1.addActionListener(new button_go_Click()); //listener 1
btn2 = new JButton("Clear!");
//btn2.setPreferredSize(new Dimension(100, 30));
btn2.addActionListener(new button_clear_Click()); //listener 2
//always add created buttons/controls to form
JPanel panel=new JPanel(new GridLayout(20,2));
panel.add(btn1);
panel.add(btn2);
Container pane = this.getContentPane();
pane.add(label);
pane.add(panel, BorderLayout.EAST);
this.setSize(680,581);
this.setVisible(true);
Solution 1:
The problem is you're not taking into consideration the frame's border (and possibly the menu bar as well) when setting the size of the frame...
Instead of using this.setSize(680,581)
which is will cause the image to rendered inside the frames borders (and beyond into non-visible space), you should simple call JFrame#pack
and let the frame decide how best to size it self (based on the preferred size of it's content)
Left, absolute sizing, right preferred sizing
public class SimpleImageLabel {
public static void main(String[] args) {
new SimpleImageLabel();
}
public SimpleImageLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JLabel imageLabel = new JLabel();
try {
imageLabel.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/image"))));
} catch (Exception e) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(imageLabel);
frame.pack(); // <-- The better way
// frame.setSize(imageLabel.getPreferredSize()); // <-- The not better way
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}