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: Help! Image leaving marks

  1. #1

    Lightbulb Help! Image leaving marks

    This doesnt look normal to me:


    The Image leaves marks on the screen. Here is all my code:

    CatchTheJaffa.java

    package net.nivangerow.yogscast.catchthejaffa;
    import javax.swing.*;
    import java.awt.*;
    public class CatchTheJaffa extends JFrame{
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("Catch the Jaffa");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(640, 480);
    		frame.setVisible(true);
    		frame.setResizable(false);
    		frame.setBackground(Color.BLACK);
    		frame.add(new Game());
    	}
    }

    Game.java

    package net.nivangerow.yogscast.catchthejaffa;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Game extends JPanel implements ActionListener{
    	Timer time;
    	Honeydew hd;
    	public Game()
    	{
    		setVisible(true);
    		setSize(640, 480);
    		setFocusable(true);
    		hd=new Honeydew();
    		time = new Timer(5, this);
    		time.start();
    		addKeyListener(new AL());
    	}
    	public void actionPerformed(ActionEvent ae) {
    		hd.move();
    		repaint();
    	}
    	public void paint(Graphics g)
    	{
    		Graphics2D g2d = (Graphics2D)g;
    		System.out.println(g2d.drawImage(hd.img, hd.x, hd.y, null)+" "+hd.x);
    		g2d.drawImage(hd.img, hd.x, hd.y, null);
    	}
    	public class AL extends KeyAdapter
    	{
    		public void keyPressed(KeyEvent e)
    		{
    			int key = e.getKeyCode();
    			if(key==KeyEvent.VK_A)
    				hd.px=-1;
    			if(key==KeyEvent.VK_D)
    				hd.px=+1;
    		}
    		public void keyReleased(KeyEvent e)
    		{
    			int key = e.getKeyCode();
    			if(key==KeyEvent.VK_A)
    				hd.px=0;
    			if(key==KeyEvent.VK_D)
    				hd.px=0;
    		}
    	}
    }

    Honeydew.java

    package net.nivangerow.yogscast.catchthejaffa;
    import javax.swing.*;
    import java.awt.*;
     
    public class Honeydew {
    	Image img;
    	ImageIcon i = new ImageIcon(getClass().getResource("honeydew.png"));
    	int x, y, px;
    	public Honeydew()
    	{
    		img = i.getImage();
    		y=340;
    	}
    	public void move()
    	{
    		x = x + px;
    	}
    }

    And the image itself:


    Thanks in advance.
    Last edited by nivangerow; September 10th, 2011 at 12:59 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,384
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Help! Image leaving marks

    Override paintComponent(), not paint(). The paint() method handles fancy things like double buffering, so you don't want to mess with it.

    Then call super.paintComponent(g) before doing anything else. That draws the background color, which clears anything you previously drew.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3

    Default Re: Help! Image leaving marks

    wait i forgot to put super.paint(g); i usually do that though. let me try.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,384
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Help! Image leaving marks

    Quote Originally Posted by nivangerow View Post
    wait i forgot to put super.paint(g); i usually do that though. let me try.
    That'll probably do it. But my comment about using paintComponent still stands.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5

    Default Re: Help! Image leaving marks

    yap, but i use the special stuff later in my program. Thanks .

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,384
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Help! Image leaving marks

    Quote Originally Posted by nivangerow View Post
    yap, but i use the special stuff later in my program. Thanks .
    Hmm, I think you're probably misunderstanding me, but okay.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 5
    Last Post: August 28th, 2011, 04:48 PM
  2. Replies: 1
    Last Post: August 18th, 2011, 06:48 AM
  3. Replies: 2
    Last Post: February 14th, 2011, 04:36 PM
  4. Forum list improperly marks whats unread
    By Lord.Quackstar in forum The Cafe
    Replies: 14
    Last Post: June 18th, 2010, 01:14 AM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM

Tags for this Thread