Ben
Ben

Reputation: 1142

Implementing Graphics within a JPanel

I've been trying to implement graphics in a GUI application I've made in Netbeans. I've done the layout (buttons, textboxes, etc.) using the GUI editor and it all works fine so far. Basically what I want to do is generate some graphics (lines and such) in the bottom half of my JFrame and leave the checkboxes, buttons, and such in the top half of my JFrame.

From the examples I've seen, the general approach is to create a class which extends JPanel and contains graphics code to create lines and other various shapes. I've been able to implement this independent of the GUI editor, but the application consists solely of the JPanel which occupies the entire JFrame. Different approaches I've used for doing this in the GUI editor have either generated the graphics but blocked out everything else in my JFrame (even for a tiny line) or not done anything at all.

Ideally I'd want to be able to place a JPanel of arbitrary size in an arbitrary location on my JFrame and create graphics that are contained within the panel. I'd also want the coordinates of the graphics I create to be referenced to the JPanel rather than the JFrame (so coordinate 0,0 is the top left of my JPanel regardless of where I decide to place it.)

Is there a straightforward means of doing this?

(example code would be greatly appreciated)

Upvotes: 2

Views: 659

Answers (2)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

First, you will have to provide a class that extends the JPanel, then override the paintComponent method to provide your custom painting of graphics.

Then on the JFrame you need to use an appropriate Layout Manager to place multiple components on the JFrame. Say for instance, you can use a GridLayout to divide the Jframe into two equal halves. The one on top can contain a normal JPanel with your buttons and controls and the one underneath will be your custom JPanel with custom graphics.

The coordinates that you use in paintComponent will refer to only the JPanel at the bottom.

Of course you can use more advanced Layout Managers than GridLayout if you need more specific layout options.

  • this can also be done in the netbeans ide by right-clicking the component and selecting "vertical" and/or "horizontal" in the auto resizing option.

Upvotes: 3

Mike
Mike

Reputation: 1899

Well you can go old school, Simple and efficient:

  1. Set the layout manager as null

    contentPane.setLayout ( null ) ;

  2. Implement the public void doLayout() method in your class

for example

   public void doLayout () {
      Dimension size      = getSize() ;
      int       x         = wizardImage.getImage().getWidth(this)+10 ;
      int       y         = 5 ;
      int       rowHeight = 18;
      super.doLayout() ;
      st_IMAGE.setBounds ( 5,((size.height-40)/2)-(wizardImage.getImage().getHeight(this)/2), wizardImage.getImage().getWidth(this), wizardImage.getImage().getHeight(this)) ;
      st_EXPORTTYPE.setBounds ( x, y, size.width-(x)-10, rowHeight ) ;
      y+=rowHeight ;
      rb_TAB_DELIM.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight ;
      rb_COMMA_SEPARATED.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight ;
      rb_FLAT_TEXT.setBounds ( x, y, size.width-(x)-10-100, rowHeight ) ;
      y+=rowHeight+10 ;
      pb_EXPORT.setLocation ( x, size.height-75 ) ;
      pb_CANCEL.setLocation ( x+pb_EXPORT.getMinimumSize().width+5, size.height-75 ) ;

   }

Upvotes: -1

Related Questions