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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: repaint() event does not work.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default repaint() event does not work.

    Hi!
    I have a java application with multiple buttons.When pressed,every button shows a different painting.One of the button is supposed to display a moving ball.The problem is,the repinat() event does not work and the ball moves but it is only possible to see it's movement when the button is pressed again. I'm working with a Frame.Why doesn't the repaint() event work?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    Does the method that calls repaint() return control to the JVM so that it can call the paint method?
    If your code is using the JVM's GUI/EDT thread like in a listener, the JVM will queue the calls to the repaint method and wait until your method exits before it calls the paint method. If your code never gives up control of the GUI's thread, the paint method will never be called.
    Your listener should compute the new locations etc, call repaint and exit.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    I'm not that advanced with java so could you please explain some of your used terms?what is JVM,what is the GUI's control?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    Basically you need to return/exit from your listener method code after you call repaint.

    Here are links with some of the terms defined:
    http://docs.oracle.com/javase/tutori.../dispatch.html

    http://java.sun.com/products/jfc/tsc.../threads1.html
    Last edited by Norm; December 25th, 2011 at 07:18 AM.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    What listener are you referring to?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    What listener are you referring to?
    buttons.When pressed,
    I am assuming that you have listeners for the buttons.

    If your code does not use listeners, then I have no idea how it is working.
    Can you post your code so we can see how it works?

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    I do have a listener for my buttons which is built like a class.
    My code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Menu extends Frame{
    private static Picture pic;
     
    private static Button b1;
    private Triangle triangle;
     
    private static Button b2;
    private Serpinsky s;
     
    private static Button b3;
    private Smiley sm;
     
    private static Button b4;
    private Rectangles r;
     
    private static Button b5;
    private MovePingPong mpp;
    public Menu(String st,Triangle tri,Serpinsky s,Smiley sm,Rectangles r,MovePingPong mpp)
    {
    	super(st);
    	this.setSize(800, 600);
     
    	this.addWindowListener(new ExitListener());
    	ButtonListener al=new ButtonListener();
     
    	b1=new Button("Traingle");
    	b1.setBackground(Color.gray);
    	b1.addActionListener(al);
     
     
    	b2=new Button("Serpinsky");
    	b2.setBackground(Color.gray);
    	b2.addActionListener(al);
     
    	b3=new Button("Smiley");
    	b3.setBackground(Color.gray);
    	b3.addActionListener(al);
     
    	b4=new Button("Rectangles");
    	b4.setBackground(Color.gray);
    	b4.addActionListener(al);
     
    	b5=new Button("Ping Pong ball");
    	b5.setBackground(Color.gray);
    	b5.addActionListener(al);
     
    	this.mpp=mpp;
    	this.sm=sm;
    	this.s=s;
        this.triangle=tri;
        this.r=r;
     
        pic=new Picture(this.triangle,this.s,this.sm,this.r,this.mpp);
        pic.setBackground(Color.LIGHT_GRAY);
        pic.add(b1);
        pic.add(b2);
       pic.add(b3);
       pic.add(b4);
        pic.add(b5);
        this.add(pic);
        this.setFocusable(true);
        this.setVisible(true);
     
    }
     
    public class ButtonListener implements ActionListener,KeyListener{
    	public ButtonListener(){
    		addKeyListener(this);
    	}
    	public void actionPerformed(ActionEvent e){
    		Button b=(Button)e.getSource();
    		Graphics g=pic.getGraphics();
     
    		if(b.equals(Menu.b1))
    			pic.paintFrameTraing(Menu.this.triangle, g);
    		if(b.equals(Menu.b2))
    			pic.paintFrameSerpinsky(Menu.this.s, g);
    		if(b.equals(Menu.b3))
    			pic.paintFrameSmiley(sm, g);
    		if(b.equals(Menu.b4))
    			pic.paintFrameRectangles(r, g);
    		if(b.equals(Menu.b5))
    				pic.paintFramePingPong(mpp, g);
    	}
     
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode()==KeyEvent.VK_RIGHT){
    			triangle.Move(10, 0);
    			repaint();
    		  }
    	}
     
    	public void keyReleased(KeyEvent e) {
    	}
    	public void keyTyped(KeyEvent e) {	
    	}
    }
     
    public class ExitListener extends WindowAdapter{
    	@Override
    	public void windowClosing(WindowEvent e){
    		System.exit(0);
    	}
    }
     
    public static void main(String[] args)
    {
    	Triangle triangle=new Triangle(Color.black);
    	Serpinsky s=new Serpinsky();
    	Smiley sm=new Smiley();
    	Rectangles r=new Rectangles();
    	MovePingPong mpp=new MovePingPong(5);
    	new Menu("This is an Applicaion Biiaaaaacch!!",triangle,s,sm,r,mpp);
    }
     
    }
    the repaint event is used in a class named "MovePingPong.java"
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.*;
    public class MovePingPong extends Frame{
    	int x_speed=5,y_speed=5;
    	Ball b=new Ball(200,300,Color.black);
    	Timer timer;
     public MovePingPong(int seconds){
    	 timer=new Timer();
    	 timer.schedule(new RemindTask(), 5*100,10*5);
     }
     class RemindTask extends TimerTask {
         public void run() {
         	if(b.GetX()!=1335 && b.GetX()!=10 && b.GetY()!=10 && b.GetY()!=730)
         	{
         		 b.Move(x_speed, y_speed);
         		 repaint();
         	}
         	if(b.GetX()==1335 || b.GetX()==10){
         		x_speed=-x_speed;
         		b.Move(x_speed,y_speed);
         		repaint();
         	}
         	if(b.GetY()==730 || b.GetY()==10)
         	{
         		y_speed=-y_speed;
         		b.Move(x_speed,y_speed);
         		repaint();
         	}
     }
    }
     public void drawPingPong(Graphics g)
     {
     
    	 b.DrawCircle(g);
     }
    }
    Last edited by SagiIs; December 25th, 2011 at 10:36 AM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    The first thing I see is two classes that extend Frame.
    What are the two frames for?

    The code does not compile. Several classes are missing. Impossible to test unless the code compiles and executes.
    Last edited by Norm; December 25th, 2011 at 10:29 AM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    Please make a small simple program (SSCCE) that compiles, executes and shows your problem.

    Please Edit your post and wrap your code with
    [code=java]<YOUR CODE HERE>[/code]
    to get highlighting and preserve formatting.

  10. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    Is it really relevant?I thought the Frames are a way to define the window.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    Please make a small simple program (SSCCE) that compiles, executes and shows your problem.

    Please Edit your post and wrap your code with
    <YOUR CODE HERE>
    to get highlighting and preserve formatting.

  12. #12
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    The code for the class with the buttons:
     
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    public class Menu extends Frame{
    private static Picture pic;
     
    private static Button b5;
    private MovePingPong mpp;
    public Menu(String st,MovePingPong mpp)
    {
    	super(st);
    	this.setSize(800, 600);
     
    	this.addWindowListener(new ExitListener());
    	ButtonListener al=new ButtonListener();
     
    	b5=new Button("Ping Pong ball");
    	b5.setBackground(Color.gray);
    	b5.addActionListener(al);
     
    	this.mpp=mpp;
     
        pic=new Picture(this.mpp);
        pic.setBackground(Color.LIGHT_GRAY);
        pic.add(b5);
        this.add(pic);
        this.setFocusable(true);
        this.setVisible(true);
     
    }
     
    public class ButtonListener implements ActionListener{
     
    	public void actionPerformed(ActionEvent e){
    		Button b=(Button)e.getSource();
    		Graphics g=pic.getGraphics();
     
    		if(b.equals(Menu.b5))
    				pic.paintFramePingPong(mpp, g);
    	}
     
    }
     
    public class ExitListener extends WindowAdapter{
    	@Override
    	public void windowClosing(WindowEvent e){
    		System.exit(0);
    	}
    }
     
    public static void main(String[] args)
    {
    	MovePingPong mpp=new MovePingPong(5);
    	new Menu("This is an Applicaion Biiaaaaacch!!",mpp);
    }
     
    }
    The code for the class using the repaint() event which doesn't work.
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.*;
    public class MovePingPong extends Frame{
    	int x_speed=5,y_speed=5;
    	Ball b=new Ball(200,300,Color.black);
    	Timer timer;
     public MovePingPong(int seconds){
    	 timer=new Timer();
    	 timer.schedule(new RemindTask(), 5*100,10*5);
     }
     class RemindTask extends TimerTask {
         public void run() {
         	if(b.GetX()!=1335 && b.GetX()!=10 && b.GetY()!=10 && b.GetY()!=730)
         	{
         		 b.Move(x_speed, y_speed);
         		 repaint();
         	}
         	if(b.GetX()==1335 || b.GetX()==10){
         		x_speed=-x_speed;
         		b.Move(x_speed,y_speed);
         		repaint();
         	}
         	if(b.GetY()==730 || b.GetY()==10)
         	{
         		y_speed=-y_speed;
         		b.Move(x_speed,y_speed);
         		repaint();
         	}
     }
    }
     public void drawPingPong(Graphics g)
     {
     
    	 b.DrawCircle(g);
     }
    }
    This is my SSCCE.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    The code you just posted does not compile. I get these errors:


    TestMenu.java:12: cannot find symbol
    symbol : class Picture
    location: class TestMenu
    private static Picture pic;
    ^
    TestMenu.java:74: cannot find symbol
    symbol : class Ball
    location: class TestMenu.MovePingPong
    Ball b=new Ball(200,300,Color.black);
    ^
    TestMenu.java:30: cannot find symbol
    symbol : class Picture
    location: class TestMenu
    pic=new Picture(this.mpp);
    ^
    TestMenu.java:45: cannot find symbol
    symbol : variable b5
    location: class java.awt.Menu
    if(b.equals(Menu.b5))
    ^
    TestMenu.java:60: non-static variable this cannot be referenced from a static context
    MovePingPong mpp=new MovePingPong(5);
    ^
    TestMenu.java:74: cannot find symbol
    symbol : class Ball
    location: class TestMenu.MovePingPong
    Ball b=new Ball(200,300,Color.black);
    ^
    TestMenu.java:72: warning: [serial] serializable class TestMenu.MovePingPong has no definition of serialVersionUID
    public class MovePingPong extends Frame {
    ^
    TestMenu.java:11: warning: [serial] serializable class TestMenu has no definition of serialVersionUID
    public class TestMenu extends Frame{
    ^
    6 errors
    2 warnings

    8 error(s)

  14. #14
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    There are classes I use here which I didn't think that are relevant.
    Picture:
     
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Panel;
     
    public class Picture extends Panel{
    	public Picture(MovePingPong mpp)
    	{
    		super();
    		this.setLocation(0,85);
    		this.setLayout(new FlowLayout());
    		this.setSize(800,600);
    	}
     
    public void paintFramePingPong(MovePingPong mpp,Graphics g){
    	g.setColor(Color.black);
    	g.clearRect(0, 0, 1500, 1000);
    	mpp.drawPingPong(g);
    }
    }
    Ball:
     
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Ball {
      private double x,y,radius;
      Color c;
     
      public Ball(Color c){
    	  this.x=300;
    	  this.y=300;
    	  this.radius=20;
    	  this.c=c;
      }
      public Ball(double x,double y,Color c)
      {
    	  this.x=x;
    	  this.y=y;
    	  this.radius=20;
    	  this.c=c;
      }
      public double GetX()
      {
    	  return this.x;
      }
      public double GetY()
      {
    	  return this.y;
      }
      public void Move(double dx,double dy)
      {
    	  x+=dx;
    	  y+=dy;
      }
      public void DrawCircle(Graphics g){
    	  g.drawOval((int)x, (int)y, 2*(int)radius, 2*(int)radius);
      }
    }

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    Can you make ONE source that compiles and executes and shows the problem. You have posted at least 4 so far. One source that shows the problem would be bettter.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    You do not override any paint methods. That is the method that the JVM will can after you call repaint.

    I've never seen your technique for animation. There are dozens of code samples on the forum on how to draw moving shapes. None look like your code. Do a Search for paintComponent and repaint and see how others have done it.

    First suggestion would be to use Swing components instead of AWT components.

  17. #17
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    I don't override any paint methods because my drawing method is in a Panel class.If repaint() only works with the overrided paint method then what should I do to update the screen and create an animation?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    what should I do to update the screen and create an animation
    A bit of rewriting. Use the timer to call you code that sets the new/next position of the shape, calls repaint and exits. In the paint method draw the shape at the new location.
    Next timer call to your method which changes the location and calls repaint. The paint method is called shortly afterwards and can draw the shape at the new location.

  19. #19
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    Isn't that exactly what I did in MovePingPong class?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    No. I don't see an override of the paint method in that class.
    The JFrame that MovePingPong extends is never set visible nor does it have any components added to it.
    Add a paint method to MovePingPong and put a println statement in it to see if it is called following a call to repaint. Also set the size of the MovePingPong frame and make it visible.

  21. #21
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    I changed the drawPingPong method to an Override paint method.The method is entered when compiled but the same affect,the screen is only updated when the button is pressed again,the repaint() does not work.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    I made the changes I suggested in post #20 and added this one extra line to the new paint method:
    b.DrawCircle(g);

    This causes a circle to move around in the MovePingPong frame.

  23. #23
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    I don't understand,I thought I did it as well.
    Does the changed class look like this?
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.*;
    public class MovePingPong extends Frame{
    	int x_speed=5,y_speed=5;
    	Ball b=new Ball(200,300,Color.black);
    	Timer timer;
     public MovePingPong(int seconds){
    	 timer=new Timer();
    	 timer.schedule(new RemindTask(), 5*100,10*5);
     }
     class RemindTask extends TimerTask {
         public void run() {
         	if(b.GetX()!=1335 && b.GetX()!=10 && b.GetY()!=10 && b.GetY()!=730)
         	{
         		 b.Move(x_speed, y_speed);
         		 repaint();
         	}
         	if(b.GetX()==1335 || b.GetX()==10){
         		x_speed=-x_speed;
         		b.Move(x_speed,y_speed);
         		repaint();
         	}
         	if(b.GetY()==730 || b.GetY()==10)
         	{
         		y_speed=-y_speed;
         		b.Move(x_speed,y_speed);
         		repaint();
         	}
     }
    }
    @Override
    public void paint(Graphics g){
    	b.DrawCircle(g);
    }
    }

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint() event does not work.

    Where did you do this:
    set the size of the MovePingPong frame and make it visible.

  25. #25
    Junior Member
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint() event does not work.

    I did it in the MovePingPong class.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with repaint
    By AraHabs in forum AWT / Java Swing
    Replies: 5
    Last Post: November 5th, 2011, 04:40 PM
  2. Drawing a line and repaint()
    By rogerbacon in forum AWT / Java Swing
    Replies: 3
    Last Post: October 19th, 2011, 07:08 PM
  3. My JFrame won't repaint correctly please help me out!
    By ocolegrove in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2011, 10:04 PM
  4. Repaint,
    By Time in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2010, 11:23 PM
  5. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM

Tags for this Thread