5 Replies - 266 Views - Last Post: 02 March 2013 - 10:00 PM Rate Topic: -----

#1 poldz123  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 26-October 12

Somethings wrong with my drawImage

Posted 01 March 2013 - 11:22 PM

 public void paintComponent(Graphics g){
        	g = f.getGraphics();
        	Graphics2D g1 = (Graphics2D)g;
        	g.drawImage(bg,0,0,null);
        	while(isRunning){
    			try{
    				for(int i = 0; i < living.size(); i++){
    				Spawn s = living.get(i);	
    				int x = (int) s.getX();
    				int y = (int) s.getY();
    				g1.drawImage(s.getImage(), x,y,null);
    			
    				}
    				 
    			Thread.sleep(15);
    			}catch(Exception ex){
    				
    			}
    		}
         }


Posted Image
Click on the Image in uploaded here..
you can see that it always print and its not bouncing and moving..

This post has been edited by poldz123: 01 March 2013 - 11:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Somethings wrong with my drawImage

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: Somethings wrong with my drawImage

Posted 02 March 2013 - 04:23 AM

Quote

g = f.getGraphics();
Why!? Why are you not using the parameter passed to paintComponent?
Was This Post Helpful? 0
  • +
  • -

#3 poldz123  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 26-October 12

Re: Somethings wrong with my drawImage

Posted 02 March 2013 - 09:08 AM

It wont draw it if i dont use it.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: Somethings wrong with my drawImage

Posted 02 March 2013 - 10:13 AM

You are lucky that it draws correctly when you do use it. That code is just begging for a race condition to paint something goofy on screen.

Your things aren't moving because their x and y positions arent't changing.

This post has been edited by farrell2k: 02 March 2013 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Somethings wrong with my drawImage

Posted 02 March 2013 - 03:23 PM

It is complety useless to sleep() in a paint() or paintComponent() method

When these method are executed, all the painting you are doing is goind somewhere in memory in a region that has a int for each pixel of your screen
It is only when the method finishes that the physical screen is refreshed.

And use the Graphics object received as parameter
Was This Post Helpful? 0
  • +
  • -

#6 poldz123  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 26-October 12

Re: Somethings wrong with my drawImage

Posted 02 March 2013 - 10:00 PM

Well I already solve it took the sleep out and instead of while I change it to if with Timer at the constructor. BTW thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1