Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Graphics mayhem

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Graphics mayhem

    How do you draw a rectangle around a whole bunch of components?

    Also, how do you draw a polygon?

    I tried creating a method called paint but it seems to be overriding another paint method and causing the graphics on my GUI to go haywire. What way do you add graphics without overriding the paint method?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Graphics mayhem

    To draw a rectangular outline:

    public void paint(Graphics g)
    {
        g.drawRect(x, y, width, height);
    }

    To draw a polygon outline:

    Polygon p = new Polygon();
    p.addPoint(x,y);
    // .. add as many points as you want
    // g is a Graphics object
    g.drawPolygon(p);

    To override the paint method safely:

    make sure you call the super class's paint method before you do any extra painting.

    public void paint(Graphics g)
    {
        super.paint(g);
        // your painting
    }

    In general, try to not override the paint method directly. See if you can override the paintComponent() method (make sure to call super.paintComponent() if the base class had some painting going on inside paintComponent()).

    For more information, see: Performing Custom Painting.

    Please pick the most appropriate category for your questions. It makes it easier for those answering your question to know what they should expect when reading the post, and also makes it easier for future users to easily find and use the knowledge provided.
    Last edited by helloworld922; August 9th, 2010 at 05:00 PM.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (August 9th, 2010)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Graphics mayhem

    How do you draw a rectangle around a whole bunch of components?
    If you wish to just add a border around components, just set the border of the component and use one of the many BorderFactory static methods to create the border.This is much cleaner in that the border is taken into account when the components are laid out, whereas custom painting is done inside the component and anything inside may be drawn over the top of the border.

Similar Threads

  1. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM
  2. keeping an drawing centered in Graphics
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2010, 11:26 PM
  3. How do i use graphics with Java?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 4
    Last Post: December 27th, 2009, 05:16 PM
  4. Increasing performance of my graphics routines
    By willberg in forum AWT / Java Swing
    Replies: 6
    Last Post: November 16th, 2009, 01:41 PM
  5. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM