Results 1 to 4 of 4
  1. #1
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default Does Each Shape requre a new Panel?

    Hi,

    I've been trying to search the net on this topic and haven't had much luck. I am trying to draw a variety shapes each having different stroke sizes etc.

    The way I am doing it now doesn't seem to be very efficient. I am creating a new class that extends jPanel and then draw my shape under the paint method. Am I going to have to do this for every little shape?

    Can't I have one class with many different paint methods or something? Like paintRect or paintLine, and then in the paintRect I call g2.draw(new Rectangle2D(...)). This doesn't work but I think I want something like that.

    Any help would be great. Thanks everyone.

  2. #2
    Humbly is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    All you need is one JPanel.
    You can then use the paintComponent method to draw as many shapes as you want in that method.

    Java Code:
    private static class ExamplePanel extends JPanel {
          public void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.drawString( "Hello World!", 20, 30 );
          }
       }
    Last edited by Humbly; 02-12-2011 at 11:47 PM. Reason: Forgot code tags

  3. #3
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    Thanks Humbly,

    So unless Im missing something huge here, I still don't see how this will work.

    If I draw multiple shapes with this they all have to have the same linewidth right? What if I want shapes with different linewidths? Or if I want to set different opacities on each shape.

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by dvreed77 View Post
    Thanks Humbly,

    So unless Im missing something huge here, I still don't see how this will work.

    If I draw multiple shapes with this they all have to have the same linewidth right? What if I want shapes with different linewidths? Or if I want to set different opacities on each shape.
    No. In paint(), set the opacity, line width, etc. on the Graphics object, paint the first Shape, set the Graphics properties for the next Shape, draw the next Shape, etc.

Similar Threads

  1. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:29 PM
  2. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 08:43 PM
  3. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  4. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:56 AM
  5. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 06:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •