1 Reply Latest reply: Feb 15, 2011 12:52 PM by morgalr RSS

    Custom painting. Only one part of my componenent is painted..

    837480
      Hello everyone,

      I have a custom component "+DrawingBoard+" who is a JScrollPane. This "+DrawingBaord+" contains an inner subclass of JDesktopPane who override the paintComponent(Graphics) method to paint another custom component named "+Sheet+" :
      <h4>@Override
      protected void paintComponent(Graphics g) {
      final Dimension size = sheet.getSize();
      final Insets insets = SHEET_BORDER.getBorderInsets(sheet);
      final Point pos = new Point(
      ((getWidth()-size.width)/2)-insets.left,
      ((getHeight()-size.height)/2)-insets.top);                         
                     
      final Graphics sheetGraphics = g.create();
      System.out.println("Translating graphics for sheet to "+pos+".");     
      sheetGraphics.translate(pos.x, pos.y);
                     
      SHEET_BORDER.paintBorder(sheet, sheetGraphics, 0, 0,
      size.width+insets.right+insets.left,
      size.height+insets.bottom+insets.top);

      sheetGraphics.translate(insets.left, insets.top);
      sheet.paint(sheetGraphics);
      }</h4>

      The "+Sheet+" also override paint(Graphics) to draw a grid and then call the paint(Graphics) method all custom "+Shape+"s.

      But when I execute this app, only one part of the "+Shape+"s is painted. I have to resize the JFrame to paint all shapes compeletely.

      I have added some system out into "+DrawingBoard+", "+Sheet+" and "+Shape+" to view the graphics clip and componenets bounds and I can see that x and y are negatives for the Graphics clip after translation but I don't understand why :
      <h4>Translating graphics for sheet to java.awt.Point[x=398,y=21]. (from DrawingBoard)
      Painting sheet into java.awt.Rectangle[x=-399,y=-22,width=1399,height=845] (from Sheet)
      Painting shape with bounds java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=-399,y=-22,width=1399,height=845] (from Shape)</h4>

      EDIT 1 :
      Also, sometimes the graphics x;y or width;height are goods when starting painting "+Shape+"s but changes later
      <h4>
           Painting Sheet java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=0,y=0,width=600,height=800] (Original graphics clip :java.awt.Rectangle[x=0,y=0,width=600,height=800])
                Painting shape with bounds java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=0,y=0,width=600,height=800]
                Painting shape with bounds java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=0,y=0,width=600,height=800]
                Painting shape with bounds java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=0,y=-140,width=600,height=800]
                Painting shape with bounds java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=3,y=-302,width=600,height=800]
                Painting shape with bounds java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=-257,y=-302,width=600,height=800]
                Painting shape with bounds java.awt.Rectangle[x=0,y=0,width=600,height=800] into java.awt.Rectangle[x=-549,y=-299,width=600,height=800]</h4>

      EDIT 2 :
      I have tried to use the "create(int, int int, int)" method from Graphics insetead of "create()" and "translate(int, int)" but that don't change anything. But if the Sheet paint itself into a Graphics who isn't translated then all "+Shape+"s are fully painted..

      Can someone help me ?

      Thanks
        • 1. Re: Custom painting. Only one part of my componenent is painted..
          morgalr
          834477 wrote:
          I have a custom component "+DrawingBoard+" who is a JScrollPane. This "+DrawingBaord+" contains an inner subclass of JDesktopPane who override the paintComponent(Graphics) method to paint another custom component named "+Sheet+" :
          This is the normal way of doing it, but you have some strange code there like 'g.create();' g is passed in by the sytem as a reference to your graphic context--don't mess with it, other than using it as a reference to your graphic context. If you need it cleared do a "this.super();" call at the beginning of your paint component.
          The "+Sheet+" also override paint(Graphics) to draw a grid and then call the paint(Graphics) method all custom "+Shape+"s.
          This is not just a no no, but HELL NO! if you are going to overide something in your graphics for SWING, then you do paintComponent. I recomend that you do back into the SWING tutorial and study it more completely before rewriting this app.

          Often, as you have seen in your code, you run into conflicts when you try to do more than recomended--like overriding paintComponent and paint.