5 Replies - 3549 Views - Last Post: 07 August 2012 - 03:49 PM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Why repaint() doesn't work?

Posted 07 August 2012 - 12:28 AM

I'm trying to create Easter game, where user has to catch eggs with basket. Everything works just fine, but when I want to paint eggs, repaint method doesn't work.

Here is code of that problemic class:
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;


public class Egg extends Background implements ActionListener{

	private Image egg;
	private int x;
	private int y;
	private int counter;
	private Random rand;
	
	public Egg(){
		rand = new Random();
		x = rand.nextInt(777)+7;
		egg = new ImageIcon("egg.png").getImage();
		counter = 1;
		y = 1;
		drawEggs();
	}
	
	private void drawEggs(){
		Timer t = new Timer(30,this);
		t.start();
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.drawImage(egg,x,y*counter,null);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		counter++;
		repaint();
	}
}


As you can see. It should increase counter variable, so, egg will be paint down down down...
But that repaint() method doesn't work, and I don't know why. I tried to add println statements after repaint() and in paintComponent() methods. Println statement, which was added after repaint() works. But println statement, which was added in paintComponent() doesn't work, so I suggest that method never was called at all.

Is This A Good Question/Topic? 0
  • +

Replies To: Why repaint() doesn't work?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Why repaint() doesn't work?

Posted 07 August 2012 - 03:19 AM

'Background' is a subclass of what exactly?
Was This Post Helpful? 0
  • +
  • -

#3 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Why repaint() doesn't work?

Posted 07 August 2012 - 04:07 AM

View Postg00se, on 07 August 2012 - 03:19 AM, said:

'Background' is a subclass of what exactly?

Background extends JPanel
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Why repaint() doesn't work?

Posted 07 August 2012 - 04:35 AM

Must be something in the way you're using it. Works fine for me
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why repaint() doesn't work?

Posted 07 August 2012 - 12:47 PM

You must do something tricky in Background because it should work.
Post your Background class.
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: Why repaint() doesn't work?

Posted 07 August 2012 - 03:49 PM

I'd like to see your Background class as well, because the only thing I can think of is that your application is ending before your 30ms timer interval occurs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1