Results 1 to 9 of 9
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default What is wrong with my paintcomponent?

    Java Code:
    package myPackage;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class SimpleGui1B implements ActionListener
    {
    	
    	JButton button,button2,button3;
    	JFrame frame;
    	MyDrawPanel myPanel;
    	
    	int x;
    	int y;
    	
    	
    	public static void main(String[] args)
    	{
    		SimpleGui1B gui = new SimpleGui1B();
    		gui.go();
    	}
    	
    	public void go()
    	{
    		myPanel = new MyDrawPanel();
    		
    		frame = new JFrame();
    		button = new JButton("click me");
    		button2 = new JButton("click me");
    		button3 = new JButton("click me");
    		
    		frame.add(myPanel);
    		
    		button.addActionListener(this);
    		button2.addActionListener(this);
    		button3.addActionListener(this);
    		
    		myPanel.add(button);
    		myPanel.add(button2);
    		myPanel.add(button3);
    
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setSize(300,300);
    
    		
    		
    		frame.setVisible(true);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) 
    	{
    		
    		for(int i = 0; i<125; i++)
    		{
    			try {
    				Thread.sleep(50);
    				System.out.println("sleeping");
    				
    			} catch (InterruptedException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			x++;
    			y++;
    			System.out.println(i);
    			myPanel.repaint();
    		}
    				
    		JButton temp = (JButton) e.getSource();
    		temp.setText("CLICKED");
    		temp.setEnabled(false);
    	}
    	
    
    class MyDrawPanel extends JPanel
    {
    	public void paintComponent(Graphics g)
    	{
    		g.setColor(Color.green);
    		g.fillOval(x, y, 40, 40);
    	}
    }
    
    }

    I am expecting some sort of a trail, my panel to repaint itself during:

    Java Code:
    		for(int i = 0; i<125; i++)
    		{
    			try {
    				Thread.sleep(50);
    				System.out.println("sleeping");
    				
    			} catch (InterruptedException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			x++;
    			y++;
    			System.out.println(i);
    			myPanel.repaint();
    		}
    But all I get is :
    What is wrong with my paintcomponent?-paintcomponent.png

    And the console output is:


    REPAINTING THE COMPONENT
    sleeping
    0
    sleeping
    1
    sleeping
    2
    sleeping
    3
    sleeping
    4
    sleeping
    5
    sleeping
    6
    sleeping
    7
    sleeping
    8
    sleeping
    9
    sleeping
    10
    sleeping
    11
    sleeping
    12
    sleeping
    13
    sleeping
    14
    sleeping
    15
    sleeping
    16
    sleeping
    17
    sleeping
    18
    sleeping
    19
    sleeping
    20
    sleeping
    21
    sleeping
    22
    sleeping
    23
    sleeping
    24
    sleeping
    25
    sleeping
    26
    sleeping
    27
    sleeping
    28
    sleeping
    29
    sleeping
    30
    sleeping
    31
    sleeping
    32
    sleeping
    33
    sleeping
    34
    sleeping
    35
    sleeping
    36
    sleeping
    37
    sleeping
    38
    sleeping
    39
    sleeping
    40
    sleeping
    41
    sleeping
    42
    sleeping
    43
    sleeping
    44
    sleeping
    45
    sleeping
    46
    sleeping
    47
    sleeping
    48
    sleeping
    49
    sleeping
    50
    sleeping
    51
    sleeping
    52
    sleeping
    53
    sleeping
    54
    sleeping
    55
    sleeping
    56
    sleeping
    57
    sleeping
    58
    sleeping
    59
    sleeping
    60
    sleeping
    61
    sleeping
    62
    sleeping
    63
    sleeping
    64
    sleeping
    65
    sleeping
    66
    sleeping
    67
    sleeping
    68
    sleeping
    69
    sleeping
    70
    sleeping
    71
    sleeping
    72
    sleeping
    73
    sleeping
    74
    sleeping
    75
    sleeping
    76
    sleeping
    77
    sleeping
    78
    sleeping
    79
    sleeping
    80
    sleeping
    81
    sleeping
    82
    sleeping
    83
    sleeping
    84
    sleeping
    85
    sleeping
    86
    sleeping
    87
    sleeping
    88
    sleeping
    89
    sleeping
    90
    sleeping
    91
    sleeping
    92
    sleeping
    93
    sleeping
    94
    sleeping
    95
    sleeping
    96
    sleeping
    97
    sleeping
    98
    sleeping
    99
    sleeping
    100
    sleeping
    101
    sleeping
    102
    sleeping
    103
    sleeping
    104
    sleeping
    105
    sleeping
    106
    sleeping
    107
    sleeping
    108
    sleeping
    109
    sleeping
    110
    sleeping
    111
    sleeping
    112
    sleeping
    113
    sleeping
    114
    sleeping
    115
    sleeping
    116
    sleeping
    117
    sleeping
    118
    sleeping
    119
    sleeping
    120
    sleeping
    121
    sleeping
    122
    sleeping
    123
    sleeping
    124
    REPAINTING THE COMPONENT


    Please help?
    What am I doing wrong?
    Why doesn't the panel repaint itself in everystep ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What is wrong with my paintcomponent?

    Thread.sleep(...) is being called on the event dispatch thread, or EDT, which is the main Swing thread for interacting with users and for drawing all graphics. If this thread is put to sleep, it is unable to perform it's functions and you in effect put the entire application to sleep. Much better is to use a Swing Timer instead or a background thread (taking care to make all Swing calls on the EDT).

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: What is wrong with my paintcomponent?

    Thanks I took the part that starts the for loop to go method after setVisible(true) and I have a very smooth animation..

    A bit confusing for now, but thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What is wrong with my paintcomponent?

    Quote Originally Posted by fatabass View Post
    Thanks I took the part that starts the for loop to go method after setVisible(true) and I have a very smooth animation..
    You did what? Can you show your new code?


    A bit confusing for now, but thanks.
    I'm also a bit confused, but you're welcome. But again, to be sure you're doing things correctly, please post the latest code.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: What is wrong with my paintcomponent?

    Java Code:
    package myPackage;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class SimpleGui1B implements ActionListener
    {
    	
    	JButton button,button2,button3;
    	JFrame frame;
    	MyDrawPanel myPanel;
    	
    	int x;
    	int y;
    	
    	
    	public static void main(String[] args)
    	{
    		SimpleGui1B gui = new SimpleGui1B();
    		gui.go();
    	}
    	
    	public void go()
    	{
    		myPanel = new MyDrawPanel();
    		
    		frame = new JFrame();
    		button = new JButton("click me");
    		button2 = new JButton("click me");
    		button3 = new JButton("click me");
    		
    		frame.add(myPanel);
    		
    		button.addActionListener(this);
    		button2.addActionListener(this);
    		button3.addActionListener(this);
    		
    		myPanel.add(button);
    		myPanel.add(button2);
    		myPanel.add(button3);
    
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setSize(300,300);
    
    		
    		
    		frame.setVisible(true);
    		
    		for(int i = 0; i<250; i++)
    		{
    			x++;
    			y++;
    			System.out.println(i);
    			//myPanel.repaint();
    			frame.repaint();
    			
    			try {
    				Thread.sleep(50);
    				System.out.println("sleeping");
    				
    			} catch (InterruptedException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    
    		}
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) 
    	{
    		
    	
    				
    		JButton temp = (JButton) e.getSource();
    		temp.setText("CLICKED");
    		temp.setEnabled(false);
    	}
    	
    
    class MyDrawPanel extends JPanel
    {
    	public void paintComponent(Graphics g)
    	{
    		g.setColor(Color.green);
    		g.fillOval(x, y, 40, 40);
    		
    		System.out.println("REPAINTING THE COMPONENT");
    	}
    }
    
    }
    It works just fine..

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: What is wrong with my paintcomponent?

    Well of course you can't trigger it with a button click anymore :)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: What is wrong with my paintcomponent?

    Quote Originally Posted by fatabass View Post
    Well of course you can't trigger it with a button click anymore :)
    Yeah, as I feared your solution is not a good one and risks stepping on the EDT in many situations. Why not try what I suggested instead?

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: What is wrong with my paintcomponent?

    I will actually..

    I was just searching for Swing timer. ( Never heard of it before. )

    Thanks.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: What is wrong with my paintcomponent?

    There's a tutorial linked from the API for javax.swing.Timer

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. paintComponent
    By zock70 in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2011, 03:19 AM
  2. PaintComponent not working
    By ingy489 in forum AWT / Swing
    Replies: 6
    Last Post: 11-10-2011, 07:49 PM
  3. Help with paintComponent!
    By joeyea in forum Java 2D
    Replies: 6
    Last Post: 12-27-2010, 01:59 PM
  4. paintComponent vs paintComponents
    By alacn in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 03:33 AM
  5. Working around paintcomponent
    By sahhhm in forum New To Java
    Replies: 2
    Last Post: 05-16-2008, 02:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •