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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: paintComponent() instead of paint()

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Thanked 1 Time in 1 Post

    Default paintComponent() instead of paint()

    (sorry, previously posted in wrong topic)

    OK, I am, as a programming rite, programming my own little version of conway's game of life, and it works fine, apart from trying to paint. I previously used the Paint method, which worked fine. This however, I have been told, is an inefficient way of doing this, so I tried to rearrange the code to use paintComponent(). This is the output that should have been produced, (and was produced using paint()):

    Now, I have moved the paintComponent method out of the main swing class and into a separate one that extends JPanel. I apologise for the messiness of my layout code, I was rearranging everything to see whether it was in the right order :/

    The grid of squares does not draw at all.

    (I have not included the game code, but I run the GUI with InvokeLater.)

    Main swing code(I have left out the other methods-yes i deleted the paint() method entirely):

    (scroll down halfway and you will see my comment as I add the drawing.

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SpringLayout;
    import javax.swing.SwingUtilities;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.geom.*;
    public class GUI extends JFrame implements MouseListener, ComponentListener{
    	Game gam = new Game();
    	JPanel mainPanel = new JPanel();
    	JPanel gPanel = new JPanel();
    	JPanel iPanel = new JPanel();
        JLabel Gen = new JLabel("Generation: " + gam.getGen());
        JLabel FPS = new JLabel("FPS: ");
    	public GUI(){
    	public final void initUI(){
    		SpringLayout layout = new SpringLayout();
    		SpringLayout gLayout = new SpringLayout();
    		setTitle("Game Of Life");
    	    setSize(620, 750);
    		gPanel.setBounds(0, 0, getWidth(), (getHeight()/6)*5);
    		iPanel.setBounds(0, (getHeight()/6)*5, getWidth(), getHeight()/6);
    	    //---------------------------- ADDING THE drawComponent class ------------------------------------------------------
    	    GridDraw drawG = new GridDraw();
    		gLayout.putConstraint(SpringLayout.NORTH, drawG, 20, SpringLayout.NORTH, gPanel);
    		gLayout.putConstraint(SpringLayout.EAST, drawG, 10, SpringLayout.EAST, gPanel);
    	       JButton PButton = new JButton("Pause");
    	       layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, PButton, 0, SpringLayout.HORIZONTAL_CENTER, iPanel);
    	       layout.putConstraint(SpringLayout.NORTH, Gen, 2, SpringLayout.NORTH, iPanel);
    	       layout.putConstraint(SpringLayout.WEST, Gen, 0, SpringLayout.WEST, iPanel);
    	       layout.putConstraint(SpringLayout.NORTH, FPS, 2, SpringLayout.SOUTH, Gen);
    	       PButton.addActionListener(new ActionListener() {
    	           public void actionPerformed(ActionEvent event) {

    and the PaintComponent code(much more palatable, but still unorganised at the moment):

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JPanel;
    public class GridDraw extends JPanel{
    	Graphics g;
    	Game gam = new Game();
    //	GUI gui = new GUI();
    	public void paintComponent(Graphics g){
    //			super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D)g;
    		boolean[][] grid = gam.getGrid();
    		int gWidth = gam.getWidth();
    		int gHeight = gam.getHeight();
    //		gui.printGen();
    		for(int i=1; i < gHeight+1; i++){
    			for(int j=1; j < gWidth+1; j++){ //selecting an individual cell
    				if(grid[j][i] == true){
    					g2.fillRect(j*5 + j + 5, i*5 + i + 26, 5, 5);
    					g2.fillRect(j*5 + j + 5, i*5 + i + 26, 5, 5);

    Now everything shows up apart from the drawing drawn with the paintcomponent method. I have been googling for hours, but i can't understand why.

    Thank you.
    Last edited by Ciaran54; February 22nd, 2011 at 01:47 PM. Reason: SOLVED

  2. #2
    Junior Member
    Join Date
    Feb 2011
    Thanked 1 Time in 1 Post

    Smile Re: paintComponent() instead of paint()

    Never mind, I solved it:

    Instead of adding GridDraw to the panel like i did here:

    	    GridDraw drawG = new GridDraw();

    I should have actually made the JPanel as a GridDraw Panel. (I am sure some tutorials told me to do as above, but...)

    	GridDraw gPanel = new GridDraw();
    	JPanel iPanel = new JPanel();

    I guess I hadn't googled enough but thanks to the very helpful page which helped to solve my problem on overiding methods:

    Golden T Studios - TUTORIAL: Overriding methods using swing

Similar Threads

  1. paintComponent(Graphics g)
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2011, 07:55 AM
  2. Paint only part of screen
    By bonus_clip in forum AWT / Java Swing
    Replies: 7
    Last Post: October 16th, 2010, 07:21 PM
  3. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM
  4. HELP with Java Paint Program Code
    By CheekySpoon in forum AWT / Java Swing
    Replies: 1
    Last Post: April 5th, 2010, 12:03 PM
  5. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 05:13 PM

Tags for this Thread