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 5 of 5

Thread: How to draw a rectangle on a JPanel, from a different class?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to draw a rectangle on a JPanel, from a different class?

    Hello everyone, now to this forum

    I'm fairly new to 2D graphics in Java.
    I learned how to draw simple shapes on a JPanel, using the paintComponent method.

    Now I'm trying to do the same thing, except the drawing should be done from an exterior class, to the class that extends JPanel.

    I tried something that I believe should work, but it doesn't. Could you explain to me the standard way to do such a thing?

    Here is my attempt:

    Class Main:

    package m;
     
    import java.util.*;
    import java.awt.*;
    import java.awt.Event.*;
    import javax.swing.*;
     
    public class Main extends JFrame {
     
    	public static void main(String[] args) {
     
    		Main m = new Main();
     
    	}
     
    	public Main(){
     
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setTitle("Bla");
    		setSize(500,500);
    		Surface s = new Surface();
    		add(s);
    		setVisible(true);
     
    	}
     
    }

    Class Surface:

    package m;
     
    import java.util.*;
    import java.awt.*;
    import java.awt.Event.*;
    import javax.swing.*;
     
    public class Surface extends JPanel {
     
    	public void paintComponent(Graphics g){
     
    		super.paintComponent(g);
     
    		DrawRect d = new DrawRect(this);
    		d.draw();
     
    	}
     
    }

    Class DrawRect:

    package m;
     
    import java.util.*;
    import java.awt.*;
    import java.awt.Event.*;
    import javax.swing.*;
     
    public class DrawRect {
     
    	Surface surface;
    	Graphics g;
     
    	public DrawRect(Surface surface){
    		this.surface = surface;
    		g = surface.getGraphics();
    	}
     
        public void draw(){
        g.fillRect(20,20,100,50); // (this won't work).
        }
     
    }

    Thanks a lot


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to draw a rectangle on a JPanel, from a different class?

    Good first post, and you've created an excellent study to explore how painting in Java works and as the basis from which to ask a decent question. Welcome!

    You're close, but the key to remember when painting on a specific component is to remember that whatever is doing the painting must have a reference to that component's graphics object. There are a number of ways to accomplish that, and you can explore those with the example you've created, but one of the simplest ways is to pass the Graphics object to the painting method, like this (some of the code you had is now irrelevant, but I left it as is):
    class Surface extends JPanel {
     
    	public void paintComponent(Graphics g){
     
    		super.paintComponent(g);
     
    		DrawRect d = new DrawRect(this);
    		d.draw( g );
     
    	}
     
    }
     
    class DrawRect {
     
    	Surface surface;
    	Graphics g;
     
    	public DrawRect(Surface surface)
        {
    		g = surface.getGraphics();
    	}
     
        public void draw( Graphics g )
        {
            g.fillRect(20,20,100,50); // (now this will work).
        }
     
    }
    If you haven't already, I recommend you read Performing Custom Painting in the Java tutorials.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to draw a rectangle on a JPanel, from a different class?

    One issue might be the creating of a new DrawRect object every time the paintComponent() method is called.
    Usually you don't want a new object every time. It's better to create the object one time outside of paintComponent() and then use it in the method.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to draw a rectangle on a JPanel, from a different class?

    It works, thanks But I don't understand why what I did before didn't work.

    You said I needed a reference to the Graphics object of Surface. But I did obtain one inside DrawRect class, in the previous version, and used this reference inside the draw() method. The only difference is that with the version you suggested (that works), the method gets the Graphics object by directly passing it into the method, and in my previous version (that didn't work), I used
    g = surface.getGraphics();
    in the constructor of DrawRect, and the method draw() simply used g. Why does it matter?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to draw a rectangle on a JPanel, from a different class?

    It actually is "working," but not persistently. Notice what happens when you run your version and then resize the frame by grabbing the corner and move it around. You can see the rectangle flash in and out like an early moving picture. I think the answer to why your version does not draw persistently is because of the lifespan of the surface's Graphics object. I'm not sure that's addressed specifically in the link I posted earlier, but you may be able to find it through further searching and study into Swing's painting mechanism.

Similar Threads

  1. draw line to edge of rectangle
    By Bc1151 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 11th, 2013, 02:33 PM
  2. Cannot draw graphics on JPanel
    By ICEPower in forum AWT / Java Swing
    Replies: 2
    Last Post: March 19th, 2013, 05:42 AM
  3. Writing a method to draw a rotatable rectangle using Graphics2D
    By johnpooley3 in forum AWT / Java Swing
    Replies: 3
    Last Post: May 18th, 2012, 09:28 AM
  4. Choosing the jpanel to draw on?
    By jm24 in forum AWT / Java Swing
    Replies: 17
    Last Post: February 16th, 2012, 08:44 PM
  5. Unable to draw a rectangle
    By n00b in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 07:32 AM

Tags for this Thread