Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By camickr

Thread: Repainting on JPanel

  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Repainting on JPanel

    Say I have a custom JPanel which overrides paintComponent to call the following method:

    Java Code:
    private void drawSierpinski(Graphics2D g2d, Point2D.Float first, Point2D.Float second, Point2D.Float third){
    		g2d.setBackground(Color.GRAY);
    		if(first == null || second == null || third == null) return;
    		if(getLength(first, second)<size)	return;
    		Point2D.Float midFirstSecond = getMidpoint(first, second);
    		Point2D.Float midSecondThird = getMidpoint(second, third);
    		Point2D.Float midFirstThird  = getMidpoint(first, third);
    		drawPointLine(g2d, first, second);
    		drawPointLine(g2d, second, third);
    		drawPointLine(g2d, first, third);
    		drawSierpinski(g2d, first, midFirstSecond, midFirstThird);
    		drawSierpinski(g2d, midFirstThird, midSecondThird, third);
    		drawSierpinski(g2d, second, midSecondThird, midFirstSecond);
    	}
    As expected, this draws a sierpinski triangle based on the point instance variables.

    I am having some trouble re drawing the triangle based on some input parameters. The button which parses the input and redraws the triangle is in a class different from that of the panel the Sierpinski triangle is drawn on. Can anyone suggest the best approach to clearing the previously drawn triangle?

    Java Code:
    @Override public void paintComponent(Graphics g){
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.setBackground(Color.GRAY);
                    repaint();
    		drawSierpinski(g2d, bottomLeft, topPoint, bottomRight);
    	}
    Is my paintComponent method, but it doesn't work as I'd like it to.

    I'm sorry if this question isn't the best, I've always had a hard time asking questions(which is odd since I see many good and bad threads helping out here).

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Repainting on JPanel

    I'm not sure what your problem is, but seeing repaint() in a paintComponent method gives me the willies.

    But knowing what a Sierpinski triangle is suggests to me that you don't want to do this drawing directly in a paintComponent method but rather in a BufferedImage and then draw the BI in paintComponent when it's done, because the triangle is often drawn recursively (as you're trying to do) and as generations get higher, this takes longer and longer to do. Let's see an SSCCE perhaps and we'll be better able to help you. :)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Repainting on JPanel

    Additionally, g2D.setBackground(...) is useless without a subsequent clearRect(...) as that's the only method that uses the background color. See the API.

    db

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Repainting on JPanel

    I'd also abstract out the logic from the GUI creating a SierpinskiTriangle class, something on rough first approximation like,

    Java Code:
    class SierpinskiTriangle {
       private Point2D[] points = new Point2D[3];
       private int generation;
       private List<SierpinskiTriangle> children = null;
    
       public SierpinskiTriangle(Point2D[] points, int generation) {
          super();
          this.points = points;
          this.generation = generation;
       }
    
       public Point2D[] getPoints() {
          return points;
       }
    
       public List<SierpinskiTriangle> getChildren() {
          return children;
       }
    
       public void makeNextGeneration(int maxGeneration) {
          if (generation < maxGeneration) {
             if (children == null) {
                int nextGeneration = generation + 1;
                Point2D[] midPoints = new Point2D[3];
                midPoints[0] = new Point2D.Double(
                      (points[0].getX() + points[1].getX()) / 2,
                      (points[0].getY() + points[1].getY()) / 2);
                midPoints[1] = new Point2D.Double(
                      (points[1].getX() + points[2].getX()) / 2,
                      (points[1].getY() + points[2].getY()) / 2);
                midPoints[2] = new Point2D.Double(
                      (points[2].getX() + points[0].getX()) / 2,
                      (points[2].getY() + points[0].getY()) / 2);
    
                children = new ArrayList<SierpinskiTriangle>();
                children.add(new SierpinskiTriangle(new Point2D[] { points[0],
                      midPoints[0], midPoints[2] }, nextGeneration));
                children.add(new SierpinskiTriangle(new Point2D[] { points[1],
                      midPoints[0], midPoints[1] }, nextGeneration));
                children.add(new SierpinskiTriangle(new Point2D[] { points[2],
                      midPoints[1], midPoints[2] }, nextGeneration));
                children.add(new SierpinskiTriangle(new Point2D[] { midPoints[0],
                      midPoints[1], midPoints[2] }, nextGeneration));
             }
          }
          for (SierpinskiTriangle child : children) {
             child.makeNextGeneration(maxGeneration);
          }
       }
    
    }
    Last edited by Fubarable; 09-20-2011 at 05:05 AM.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Repainting on JPanel

    So if I understand you correctly fubar, I would then allow the Sierpinski Triangle to know how to draw itself, and simply draw it onto the JPanel(or BufferedImage)? Thanks for the suggestions, I'm gonna have to re think how I am handling and see what I can come up with.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Repainting on JPanel

    Do Mandelbrot and Julia sets. They're far prettier.
    DarrylBurke likes this.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Repainting on JPanel

    Alright, I looked into mandelbrot's set but never got around to it. I really want to try and figure out some ray tracing, but I'd imagine I should get better at basics first.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Repainting on JPanel

    Can anyone suggest the best approach to clearing the previously drawn triangle?
    You need a super.paintComponent(g) at the start of the paintComponent() method.
    Fubarable likes this.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Repainting on JPanel

    Quote Originally Posted by camickr View Post
    You need a super.paintComponent(g) at the start of the paintComponent() method.
    Ah, alright that got it working as expected. I guess I need to hit the tutorials some more. Thanks guys!

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2011, 11:59 PM
  2. Applet not repainting?
    By mjgood91 in forum Java Applets
    Replies: 4
    Last Post: 01-18-2011, 02:27 AM
  3. Question about repainting a JPanel
    By Nosrettap in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2011, 08:29 PM
  4. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  5. repainting more efficiently
    By imorio in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2010, 04:24 AM

Posting Permissions

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