1

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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I think you need to set the position of the `label` into the `Container pane` – HericDenis Nov 09 '12 at 17:39
  • otherwise I think the position will be the upper-left-most point of your frame – HericDenis Nov 09 '12 at 17:40
  • Anytime I set the position, it covers up the whoel screen and the panel becomes invisible @HericDenis – user1477707 Nov 09 '12 at 17:42
  • I see... I think this happening because your image is too big isn't? You might need to resize it first. – HericDenis Nov 09 '12 at 17:44
  • So I'll post it as an answer and than please accept it (: – HericDenis Nov 09 '12 at 17:56
  • The problem isn't the image, it's the how you size the frame. You're not taking into consideration that any frame has a boarder and and possibly a menu bar. You've asked the frame to try and squeeze your image into the that also needs to render all this additional content. Rather then setting the size of frame absolutly, simply call the `JFrame#pack` method and let the frame figure out how best to size itself based on the contents preferred size – MadProgrammer Nov 09 '12 at 21:17

2 Answers2

2

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)

enter image description hereenter image description here

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);
            }
        });
    }


}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-2

As I mentioned before, you should set the position of your JLabel using

aJLabel.setLocation(Point p)

OR

aJLabel.setLocation(int x, int y)

if your image is too large, you'll need to resize it too in order to get a good placement (:

Best Regards.

HericDenis
  • 1,364
  • 12
  • 28
  • No you shouldn't. So much easier to simply use a layout manager and call `pack` on the frame :P – MadProgrammer Nov 09 '12 at 21:05
  • You mentioned this before ... I am surprised the previous answer was not down voted so you wouldn't repeat that recommendation. Absolute positioning is (almost) never the answer. – Robin Nov 10 '12 at 12:44
  • Well @Robin, the guy that asks for it said it works, so I thought, why not post it as the answer? – HericDenis Nov 10 '12 at 14:09