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 15 of 15

Thread: paintComponent error

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default paintComponent error

    i am painting a rect in player.java and i want to print in main.java class.
    in main i am using paintComponent but in player class iam using paint method.

    my 1st question is that shoud i use paintComponent in both classes? or is it right that way i have it.

    my 2nd question is that. i have a error:
    The method paint(Graphics, main) in the type player is not applicable for the arguments (Graphics, main.Display)

    error on line: player_class.paint(g, this)

    note if i send 'this' to different class than i get error. but if i only send 'g' than it works fine.
    main.java
    public class main extends JApplet
    {   
        Timer timer;
        Display display_class;
        player player_class;
     
     
        /*** init method ***/
        public void init()
        {
            setSize(800, 400);
            display_class = new Display(); 
            setContentPane(display_class);  
        }/*** end of init method ***/
     
     
     
        /*** stat method ***/
        public void start()
        {   player_class = new player();
            timer = new Timer(30, this);  
            timer.start();               
        }/*** end of start method ***/
     
     
     
        /*** game loop ***/
        public void actionPerformed(ActionEvent e)
        {   
            repaint();
        }/*** end of run method ***/
     
     
     
        /*** paint method ***/ 
        public class Display extends JPanel
        {
            public void paintComponent(Graphics g)
            {   
                super.paintComponent(g);     
     
                player_class.paint(g, this);   
            }/** end of paint component method ***/
         }/*** end of display class ***/
    }/*** end of main class ***/

    player.java
    public class player
    {
        /*** PLAYER VARIABLES ***/
        private int x = 10;
        private int y = 200;
        private int width = 15;
        private int height = 100;
     
     
        /*** CONSTRUCTOR METHOD ***/
        public player()
        {
     
        }
     
     
        public void paint(Graphics g, main m)
        {
            g.setColor(Color.red);
            g.fillRect(x, y, width, height); 
        }   
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: paintComponent error

    Override the paintComponent() method in a Swing class object.

    There is no magic about using most method names in a class the does not extend any class. However using common method names like paint() in a class can be confusing.
    Since all classes extend the Object class, using any of the Object class's method names should be done with care.

    The method paint(Graphics, main) in the type player is not applicable for the arguments (Graphics, main.Display)
    Using the this variable in the inner class returns a reference to the inner class, not to the enclosing class. There is a syntax for getting the a this reference to the enclosing class. It could be: Main.this but I'm not sure.
    Why do you need to send a reference to the Main class to paint?

    BTW Coding conventions are that class names start with Uppercase names.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    game06 (March 23rd, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent error

    thanks got it to working. just one last question. i didnt want to post another theard bc its kind of same code so ill post it there.

    problem is the my keyListener is not working. i doesnt even go in method public void keyPressed(KeyEvent e). any idea why is this happening. may be this is bc of paintComponent?
    note ActionListener, MouseListener, MouseMotionListener seem to be working fine.

    i made a smaller program that u can run. i added print statment flag1 and flag2 in keypressed methods. it doesnt out when i press the up key.

    also i put @Override on almost every method in main.java. i am not sure if i need them?

    main.java
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
     
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    public class aaa extends JApplet implements ActionListener, KeyListener, MouseListener, MouseMotionListener
    {
     
    	/*** class variables ***/
    	Display display_class;
     
     
    	/*** Timer variable ***/
    	Timer timer_class;
     
     
    	//###########################################################################################################
    	/*** init method ***/
    	@Override
    	public void init()
    	{
    		setSize(900, 500);
    		display_class = new Display(); 
    		setContentPane(display_class);  
    		addKeyListener(this);                 //keys
    		addMouseListener(this);               //clicking
    		addMouseMotionListener(this);         //mouse motion
    	}/*** end of init method ***/
     
     
     
     
    	//###########################################################################################################
    	/*** start method ***/
    	@Override
    	public void start()
    	{
     
    		/*** start timer ***/
    		if(timer_class == null)
    		{
    			timer_class = new Timer(20, this); //set up timer only onces
     
    		}
    		else
    		{
    			timer_class.stop();
    		}
    		timer_class.start();                   //start timer jump inside actionperformed method
    	}/*** end of start method ***/
     
     
     
    	//###########################################################################################################
    	/*** game loop ***/
    	@Override
    	public void actionPerformed(ActionEvent e)
    	{
    		repaint();
    	}/*** end of run method ***/
     
     
     
     
    	//###########################################################################################################
    	/*** paint method ***/ 
    	public class Display extends JPanel
    	{
    		public void paintComponent(Graphics g)
    		{
    			super.paintComponent(g);       //redraw paint method. so it doesnt draw on top of each other
     
     
    		}/** end of paint method ***/
    	}/*** end of display method ***/
     
     
     
     
     
    	//###########################################################################################################
    		/*** KEY PRESSED ***/
    		@Override
    		public void keyPressed(KeyEvent e)
    		{
    			int keys = e.getKeyCode();
     
    			System.out.println("flag 1");
    				if(keys == KeyEvent.VK_UP)
    				{
    					System.out.println("flag 2");
    				}
    		}/*** end of key pressed method ***/
     
    		/*** KEY RELEASED ***/
    		@Override
    		public void keyReleased(KeyEvent e)
    		{
    			int keys = e.getKeyCode();
     
    				if(keys == KeyEvent.VK_UP) { }
    				else if(keys == KeyEvent.VK_DOWN) { }
    				else if(keys == KeyEvent.VK_E) { }
    				else if(keys == KeyEvent.VK_D) { }
    		}/*** end of key pressed method ***/
     
    		/*** KEY TYPED ***/
    		public void keyTyped(KeyEvent e){}
    		/*** end of key typed method ***/
     
     
     
     
     
    		//###########################################################################################################
    		/****** MOUSE *******/
    		@Override
    		public void mousePressed(MouseEvent e) { }
    		public void mouseReleased(MouseEvent e) { }
    		public void mouseClicked(MouseEvent e){}
    		public void mouseEntered(MouseEvent e) {} //mouse enter applet window
    		public void mouseExited(MouseEvent e){}   //mouse exit applet window
     
     
     
     
     
     
    		//###########################################################################################################
    		/****** MOUSE MOSTION *******/
    		@Override
    		public void mouseDragged(MouseEvent e){ } //mouse drag
    		@Override
    		public void mouseMoved(MouseEvent e) {}
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: paintComponent error

    For key listeners to work, the component needs to have the focus. There are several methods that will help the component get the focus. See the API doc for the Component class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    game06 (March 23rd, 2013)

  7. #5
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent error

    i did
    setFocusable(true);
    and it works fine. how about @Override? can you explain which method to use them on?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: paintComponent error

    Use @Override when you want the compiler to check if the method you are coding overrides a method in a super class.
    It will prevent misspelling of method names and incorrect args for a method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent error

    so there is no harm of using @Override before every methods in project?

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: paintComponent error

    No. It's a good idea to have the compiler check if the code is correctly overriding methods.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent error

    but if u mispell class name. wont that give u a error? u end up fixing the class name any way. can i just not use @Override? or is that bad to not use override

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: paintComponent error

    I don't understand you reasoning. If using @Override will have the compiler tell you if there is an error, isn't that better than not knowing there is an error without it?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent error

    if u have a error than the eclipse will tell u that u have a error. even u dont have override. so i dont underend why use orride.

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: paintComponent error

    You can do whatever you want.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent error

    no i am trying to understand why would you want to put override if eclipse or any txt editor tell you the error.

  16. #14
    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: paintComponent error

    You would use @Override before any method that you believe overrides a parent method. This way you will find out at compile time whether your assumption about the method being a true override is correct. Otherwise you won't know if you are correct until run time, or sometimes never at all.

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

    game06 (March 23rd, 2013)

  18. #15
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent error

    got it

Similar Threads

  1. Problem with paintComponent...?
    By *mT in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2012, 12:55 PM
  2. problem with paintComponent
    By jasox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2011, 04:27 PM
  3. paintComponent() instead of paint()
    By Ciaran54 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 22nd, 2011, 02:46 PM
  4. paintComponent(Graphics g)
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: February 8th, 2011, 08:55 AM
  5. why paintComponent method is not invoked?
    By ice in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 11th, 2010, 12:30 AM