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.

Results 1 to 5 of 5

Thread: Can't call paint() method from another class

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Can't call paint() method from another class

    Hi, so I'm planning to create a basic game with levels which are each painted from their own seperate level classes. I've made a class called 'Level1' which has the paint method which I want to use in and then also a class which I want to call the method in. When I try to call 'paint(g)' it gives the error g cannot be resolved as a variable.

    Here's my code, any help will be much appreciated.

    Level1
    package maze;
     
    import java.awt.*;
     
     
     
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
     
     
     
    public class Level1 extends JPanel {
     
    	private Image wall;
     
    	public Level1(){
     
     
    	ImageIcon ii = new ImageIcon(this.getClass().getResource("wall.png"));
        	wall = ii.getImage();
     
     
    	}
     
     
    	public void paint(Graphics g) 
    	{
    		super.paint(g);			
    		Graphics2D g2 = ( Graphics2D )g;   
     
    		for(int i = 0; i <= 18; i++){
     
        		g2.drawImage(wall, i*30, 0, this);
        		g2.drawImage(wall, i*30, 540, this);
        		g2.drawImage(wall, 0, i*30, this);
        		g2.drawImage(wall, 570, i*30, this);
        		g2.drawImage(wall, 120, i*30, this);
     
        }
      }
     
     
    }

    Board
    package maze;
     
    import java.awt.*;
     
    import java.util.ArrayList;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    import maze.Level1;
     
     
     
    public class Board extends JPanel {
     
     
    	Level1 level1 = new Level1();
     
    	 public Board() {
     
    	        setFocusable(true);
    	        setBackground(Color.DARK_GRAY);
    	        setDoubleBuffered(true);
     
     
    	        setSize(400, 300);
     
    	        level1.paint(g);
     
    	 }
     
     
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't call paint() method from another class

    First, you should override paintComponent, and not paint. Second, you should not call this method directly. Rather, add each component to its parent. This alone when added to a top level component hierarchy is often all that is needed. If you wish to repaint the component, then call repaint(). If you are still having problems, then I suggest posting an SSCCE and describe exactly what the problem is.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't call paint() method from another class

    Sorry to sound really stupid, but how would I do all that?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Can't call paint() method from another class

    The first place I'd start looking would be the Swing Custom Painting Tutorials. I think that most of us learned our Swing graphics starting at this site.

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    Huw (September 18th, 2012)

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't call paint() method from another class

    Had a quick look at that and looks very helpful, I'll go through it properly afterschool, thankyou very much!

Similar Threads

  1. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  2. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM
  3. [SOLVED] How do you get this fountain to animate with paint method?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 48
    Last Post: May 31st, 2011, 04:00 PM
  4. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM
  5. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM

Tags for this Thread