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

    Default How does frame.repaint and paintComponent work?

    Hi,

    Here is my code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class SimpleGui3C implements ActionListener 
    {
    	JFrame frame;
    	public static void main(String[] args) 
    	{
    		SimpleGui3C gui = new SimpleGui3C();
    		gui.go();
    	}
    	
    	public void go()
    	{
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JButton button = new JButton("Change colors!");
    		button.addActionListener(this);
    		
    		MyDrawPanel drawPanel = new MyDrawPanel();
    		
    		frame.getContentPane().add(BorderLayout.SOUTH,button);
    		frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
    		frame.setSize(300,300);
    		frame.setVisible(true);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) 
    	{
    		frame.repaint();
    	}
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class MyDrawPanel extends JPanel
    {
    	public void paintComponent(Graphics g)
    	{
    		int i;
    		i = (int) (Math.random()*100);
    		
    		if (i<50)
    		{
    			g.setColor(Color.red);
    			g.fillRect(i,i,100,100);
    		}
    		else
    		{
    			g.setColor(Color.orange);
    			g.fillRect(i,i,100,100);
    		}
    		System.out.println(i);
    	}
    }
    This code just works fine, every time I click the button the drawpanel gets either an orange color or red color depending on the random number i.

    This is in method paintComponent as you can see. But my actionPerformed on button clicked is just frame.repaint.

    How is paintComponent triggered in this code, can anyone explain me?

    Thanks.

    How does frame.repaint and paintComponent work?-1.png

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How does frame.repaint and paintComponent work?

    Painting in AWT and Swing

    (And remember that your JFrame instance is also an instance of Component.)

Similar Threads

  1. Replies: 3
    Last Post: 11-20-2011, 05:50 PM
  2. Replies: 0
    Last Post: 10-24-2011, 09:26 PM
  3. Replies: 3
    Last Post: 02-04-2011, 06:30 AM
  4. repaint class doesnt work anymore... dunno why..
    By Addez in forum New To Java
    Replies: 9
    Last Post: 11-07-2009, 09:10 PM
  5. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 04:43 PM

Posting Permissions

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