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

Thread: g.drawImage(); Not working?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default g.drawImage(); Not working?

    Hi, So I am making a game and I created a class called Board which is basically the engine of my game , and I implement it into a JFrame. Now, I have set a Boolean variable "ready_to_end" and if it is not true then the compiler executes the code to play the game. However if !ready_to_end, then I want to display a picture of Gman from Half Life, matter of a fact any other picture. But I can not seem to be able to?

    My concern is that maybe the picture is being drawn, but behind what has already been drawn when the user was playing the game.

     
    	public void actionPerformed(ActionEvent e) {
     
    		repaint(); 
     
    	}
     
    	public void paint(Graphics g){
    //........
     
    		 else  { //            ISUUE HERE!!!! THANKS GUYS!!!!!
     
    			super.paint(g);
    			PlaySound.sound = new File("Music/chest_open.wav");
     
    			new Thread(PlaySound.play).start();
     
    			Image end;
    			g.drawImage(m.getGman(), (p.getTileX() - 2) * 32,
    					(p.getTileY() + 3) * 32, null);
    			try {
    				Thread.sleep(3000);
    			} catch (Exception ee) {
    			}
     
    			PlaySound.sound = new File("Music/Gman_end.wav");
    			new Thread(PlaySound.play).start();
     
    			ImageIcon img = new ImageIcon("Pictures/Hfl3.jpg");
     
    			end = img.getImage();
     
    			g.drawImage(end, 0, 0, null);
     
    			try {
    				Thread.sleep(25000);
    			} catch (Exception eeeee) {
    			}
     
    			Main.baseLevel.dispose();
     
    		}
    	}
     
     
     
     
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: g.drawImage(); Not working?

    A few notes on your code:
    1) I'd recommend overriding paintComponent rather than paint.
    2) Never call Thread.sleep in a painting routine, this locks up the event dispatch thread and prevents any further painting (or user interaction) to be performed until the sleep completes. My guess (see 3) is that you have several successive sleep calls that are locking up things.
    3) I'd recommend stripping out unnecessary parts of the code and post an SSCCE, which only demonstrates the problem you are dealing with

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: g.drawImage(); Not working?

    Quote Originally Posted by copeg View Post
    A few notes on your code:
    1) I'd recommend overriding paintComponent rather than paint.
    2) Never call Thread.sleep in a painting routine, this locks up the event dispatch thread and prevents any further painting (or user interaction) to be performed until the sleep completes. My guess (see 3) is that you have several successive sleep calls that are locking up things.
    3) I'd recommend stripping out unnecessary parts of the code and post an SSCCE, which only demonstrates the problem you are dealing with
    How do I override painComponent? Can you please provide an example?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: g.drawImage(); Not working?

    Quote Originally Posted by sakonpure6 View Post
    How do I override painComponent? Can you please provide an example?
    Same as you do paint, but using the paintComponent method signature

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        //your custom painting here
    }

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: g.drawImage(); Not working?

    @sakonpure6, in paint/paintComponent:
    a) Don't do any sleep or other thread-blocking actions.
    b) Don't play sounds
    c) Don't start threads
    d) Don't load resources indiscriminately (at most, and only if there are really no other ways, with a "caching" strategy like: is img null? Load image and assign to img. So that this happens just 1 time and no more)
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: g.drawImage(); Not working?

    Thank you all, I fixed the issue.

Similar Threads

  1. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  2. drawImage help! :(
    By JuLiAnc in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2012, 10:59 PM
  3. drawImage() causes endless calls to paintComponent()
    By repaint_forever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 10th, 2011, 12:15 PM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM