1

When I see my application, it draws my image not on (0,0) but somewhere middle in my program, So I debug my program,and I saw that the clipRegion.lox is 456 and loy is 130 and this is the location that program draw (0,0). So I think that Clip Region position is wrong, but I don't know how to fix it. Any help please?

code:

public void changeState(int bef,int cur){
    if(bef==1){
        if(cur==2){
            intro.setVisible(false);
            this.setContentPane(play);
            play.init();
            play.setVisible(true);
        }
    }
}

This is when first my Panel is started. In play.init() I set my socket to connect with server program, and start sound file. That is all.

 public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(new Color(255,255,255));
    g.drawImage(backgroundImg, 0, 0, this);
    g.drawImage(player, 368, 280, this);
    for(int i=0;i<60;i++){
        for(int j=0;j<100;j++){
            if(map[i][j]==1){
                g.drawImage(stone, (j-10)*32-dx, (i-10)*32, this);
            }
            else if(map[i][j]==2){
                if(i>0&&map[i-1][j]==0) g.drawImage(grass, (j-10)*32-dx, (i-10)*32, this);
                else g.drawImage(dirt, (j-10)*32-dx,(i-10)*32,this);
            }
        }
    }
    g.drawString(servermessage, 320, 200);
}
  • I erased my Println code and Thread Sleep Code. It was just for debugging and when I deleted however, nothing changed.

Plus, I repainted it, and it draws in (0,0) but it doesn't draw full screen.

I think that drawn image is the same size as (456,130) to (800,600) so I think the picture is cut out.

I can't post my picture because of low reputation... Any help too?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Be, very, very careful when playing with clip, the `Graphics` context has already been clipped to the area which the `RepaintManager` wants painted. Also, `Thread.sleep(500)` within the `paintComponent` method IS a really, really bad idea, you're blocking the EDT which will make your app appear as it's hung – MadProgrammer Jun 03 '15 at 06:40
  • 2
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jun 03 '15 at 06:41
  • 1
    One way to get image(s) for the runnable example is to *hot link* to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jun 03 '15 at 09:25

1 Answers1

0
this.setBackground(new Color(255,255,255));

Don't set the background in the painting method. Set the background in the constructor of your class.

it draws my image not on (0,0) but somewhere middle in my program, g.drawImage(backgroundImg, 0, 0, this);

No, the image is drawn at (0, 0) which is relative to the location of the panel. This would mean that the panel is not being painted at (0, 0) relative to the frame. This could be because of the layout manager you are using is positioning the panel in the center of the frame. Check your layout code.

I saw that the clipRegion.lox is 456 and loy is 130 and

If you think the clipping area is wrong it may be because your panel does not have a preferred size. Whenever you do custom painting you should override the getPreferredSize() method to return the size of the panel so the layout manager can do their job properly. Maybe the preferred size should be the size of your image?

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • as above, I use this.setContentPane(play) in Frame Class, and before I made this project, it worked well. Is this is wrong, if this is wrong, how can I should change my code? and I overrided getPrefferedSize() but nothing improved. – JaeHyunChoi Jun 04 '15 at 00:32
  • Solved! I change my getX() and getY() method to getCoorX() and getCoorY() method, and I see my program worked well!! Maybe java think that this is overrided. Thanks for help. – JaeHyunChoi Jun 04 '15 at 00:58
  • `I change my getX() and getY() method` - that is why you should always post a "runnable" example as requested in the comments. There is no way would could guess you overrode those methods without seeing the demo code. – camickr Jun 04 '15 at 02:02