Results 1 to 17 of 17
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Paint help please!

    hey guys I'm getting errors when I'm trying to paint the lines. basically I'm trying to make a program where u click and it draws a point then u click again and it draws a line, but I'm not sure why I'm getting the errors
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    
    public class changeColor extends JFrame{
    private JPanel panel;
    private JFrame frame;
    ArrayList<Point> posArray = new ArrayList<Point>();
    	
    	changeColor(){	
    		super("hi");
    		frame = new JFrame();
    		frame.setSize(300,300);
    		panel = new JPanel();
    		frame.getContentPane().add(panel);
    		panel.addMouseListener(new MouseClass());
    
    	}
    
    	public class MouseClass extends MouseAdapter{
    		public void mouseClicked(MouseEvent event){
    			if(event.isMetaDown()==false && event.isAltDown()==false){
    				
    				int y = event.getY();
    				int x = event.getX();
    				posArray.add(new Point(x,y));
    				if(posArray.size()>2){
    					for(int a = 0; a < posArray.size(); a++){
    						public void paintComponent(Graphics g){	
    							super.paintComponent(g);
    							g.drawLine(posArray[a], posArray[a+1], Color.black);
    							
    					}
    					}
    				}
    			
    		}
    	
    			
    			
    		
    	}
    	}
    	
    	
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Paint help please!

    You can't define a method in the middle of a loop (see line #30).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Paint help please!

    You're extending JFrame and yet also creating an instance of JFrame. Doing the latter is correct. But you should
    extend JPanel and override paintComponent. No need to create a separate instance of JPanel. And it is rarely appropriate
    to extend JFrame.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Paint help please!

    ok thanks, to both! that makes a lot of sense.

    @JosAH would i make another method like

    Java Code:
    public void drawLine(){
    ***public void paintComponent(Graphics g){ 
    ****************************super.paintComponent(g);
    ****************************g.drawLine(posArray[a], posArray[a+1], Color.black);
    *****************************
    ********************}
    }
    and just run drawLine in the for loop?

    @jim, can you explain the difference between creating an instance vs. extending? also, what does overriding paint component mean? Also, if i don't extend JFrame then i can't do super("title"); without getting "Multiple markers at this line
    - The constructor JPanel(String) is undefined
    - Line breakpoint:changeColor [line: 12] -"

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Paint help please!

    Quote Originally Posted by swedishfished View Post
    ok thanks, to both! that makes a lot of sense.

    @JosAH would i make another method like

    Java Code:
    public void drawLine(){
    ***public void paintComponent(Graphics g){ 
    ****************************super.paintComponent(g);
    ****************************g.drawLine(posArray[a], posArray[a+1], Color.black);
    *****************************
    ********************}
    }
    and just run drawLine in the for loop?
    You can't define a method inside the body of another method either. Methods aren't 'first class citizens' in Java.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Paint help please!

    so what do i do then :C
    i dont have much experience with lava

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Paint help please!

    Quote Originally Posted by swedishfished View Post
    @jim, can you explain the difference between creating an instance vs. extending? also, what does overriding paint component mean? Also, if i don't extend JFrame then i can't do super("title"); without getting "Multiple markers at this line
    - The constructor JPanel(String) is undefined
    - Line breakpoint:changeColor [line: 12] -"
    Creating an instance is simply creating a new object of some class with the new operator. Extending is how you subclass an already existing class. And overriding a method is simply substituting your method for the one that is already defined. Usually when one overrides a method they invoke the super class version via the super keyword.

    Having explained all that I have to say that you seem to be trying things you aren't ready for. I say this because
    I am using standard object oriented programming terminology in my answers and they seem to be foreign to you.
    A good start would be to get a good book and check out the tutorials linked in my signature. Otherwise it is
    going to be a difficult journey.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Paint help please!

    Quote Originally Posted by swedishfished View Post
    so what do i do then :C
    i dont have much experience with lava
    This is just programming; your paintComponent( ... ) method should be aware that it should paiint a line; your MouseListener decides that; it could set a boolean variable to true which makes the paintComponent( ... ) method decide what to do ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Paint help please!

    Ok, i think i got it but its not running in eclipse. can u check it out to seed if there are any errors and also if i did something poorly

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import java.util.ArrayList;
    
    public class changeColor extends JPanel{
    private JPanel panel;
    private JFrame frame;
    ArrayList<Point> posArray = new ArrayList<Point>();
    	
    	changeColor(){	
    					frame = new JFrame("title");
    					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			frame.setSize(300,300);
    		panel = new JPanel();
    		frame.getContentPane().add(panel);
    		panel.addMouseListener(new MouseClass());
    add(frame);
    add(panel);
    
    		frame.setVisible(true);
    
    	}
    
    	public class MouseClass extends MouseAdapter{
    		public void mouseClicked(MouseEvent event){
    			if(event.isMetaDown()==false && event.isAltDown()==false){
    				
    				int y = event.getY();
    				int x = event.getX();
    				posArray.add(new Point(x,y));
    				if(posArray.size()>2){
    					
    					for(int a = 0; a < posArray.size(); a++){
    					repaint();
    					}
    				}
    			}
    	
    		}
    	
    			
    			
    		
    	}
    	
    	public void paintComponent(Graphics g, int q){	
    		super.paintComponent(g);
    		g.drawLine(posArray.get(q).x, posArray.get(q).y, posArray.get(q+1).x,posArray.get(q+1).y);
    		
    
    	}
    	
    	
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Paint help please!

    Your paintComponent method is incorrect. It should be paintComponent(Graphics g). You can't
    pass arguments to the paint routine. The method must use instance fields for painting (or local
    values declared within the paintComponent method). And make a habit of using the @Override
    annotation when you want to override a method. Like this:

    Java Code:
    @Override
    public void paintComponent(Graphics g) {
    }
    That way, the compiler will complain if you are trying to override a method that does not exist (like you were doing).

    Regards,
    Jim
    Last edited by jim829; 08-24-2014 at 12:32 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Paint help please!

    Okay, that makes sense. Thanks for replying! But how could I do that in a for loop, cuz i need to pass in the numbers from the for loop into the drawLine function? sorry i have so many questions i just don't have any experience in graphics this is my first project

    does this work?
    Java Code:
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import java.util.ArrayList;
    
    public class changeColor extends JPanel{
    private JPanel panel;
    private JFrame frame;
    private int q = 0;
    ArrayList<Point> posArray = new ArrayList<Point>();
    	
    	changeColor(){	
    					frame = new JFrame("title");
    					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    			frame.setSize(300,300);
    		panel = new JPanel();
    		frame.getContentPane().add(panel);
    		panel.addMouseListener(new MouseClass());
    add(frame);
    add(panel);
    
    		frame.setVisible(true);
    
    	}
    
    	public class MouseClass extends MouseAdapter{
    		@Override 
    public void mouseClicked(MouseEvent event){
    			if(event.isMetaDown()==false && event.isAltDown()==false){
    				
    				int y = event.getY();
    				int x = event.getX();
    				posArray.add(new Point(x,y));
    				if(posArray.size()>2){
    					
    					for(q = 0; q < posArray.size(); q++){
    					repaint();
    					}
    				}
    			}
    	
    		}
    	
    			
    			
    		
    	}
    	@Override 
    	public void paintComponent(Graphics g){	
    		super.paintComponent(g);
    		g.drawLine(posArray.get(q).x, posArray.get(q).y, posArray.get(q+1).x,posArray.get(q+1).y);
    		
    
    	}
    	
    	
    }
    Last edited by swedishfished; 08-24-2014 at 12:56 AM.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Paint help please!

    You can put the for loop in the paintComponent method. The posArray must be an instance field.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Paint help please!

    Ok, thanks. This is the final code. It doesn't do the drawing part.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import java.util.ArrayList;
    
    public class changeColor extends JPanel{
    private JPanel panel;
    //private JFrame frame;
    private int q = 0;
    int y;
    int x;
    ArrayList<Point> posArray = new ArrayList<Point>();
    	
    	changeColor(){	
    					JFrame frame = new JFrame("title");
    					panel = new JPanel();
    
    					frame.setSize(300,300);
    					frame.add(panel, BorderLayout.CENTER);
    					panel.addMouseListener(new MouseClass());
    					frame.setVisible(true);
    					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    	public class MouseClass extends MouseAdapter{
    public void mouseClicked(MouseEvent event){
    			
    				
    				y = event.getY();
    				x = event.getX();
    				posArray.add(new Point(x,y));	
    			
    				repaint();
    
    				JOptionPane.showMessageDialog(null, "hi");
    				
    				if(posArray.size()==1){
    					JOptionPane.showMessageDialog(null, "1.");
    				}
    				if(posArray.size()==0){
    					JOptionPane.showMessageDialog(null, "0.");
    				}
    				if(posArray.size()==2){
    					JOptionPane.showMessageDialog(null, "2.");
    				
    				
    			}
    
    	
    		}
    	
    			
    			
    		
    	}
    
    	public void paintComponent(Graphics g){	
    		JOptionPane.showMessageDialog(null, "hai");
    
    		super.paintComponent(g);
    		if(posArray.size()>1){
    		for(q = 0; q < posArray.size(); q++){
    			JOptionPane.showMessageDialog(null, "hai");
    			g.drawLine(posArray.get(q).x, posArray.get(q).y, posArray.get(q+1).x,posArray.get(q+1).y);
    			repaint();
    		}
    	}
    
    		
    
    	}
    	
    	
    }

    and


    Java Code:
    class runIt {
    
    	public static void main(String[] args){
    		
    		
    		changeColor draw = new changeColor();
    
    
    	}
    	
    	
    }

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Paint help please!

    Why are you adding an ordinary JPanel to your JFrame? You should add 'this', which is a special JPanel (you just wrote it).

    kind regards,

    Jos

    ps. and don't call repaint() in your paintComponent( ... ) method and don't loop up to the array.size(); you'll get an index out of bounds error.
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    42
    Rep Power
    0

    Default Re: Paint help please!

    OK i got it all now! wow i was so stupid!!!! hahahah


    I'm just a little confused over why i should be adding 'this' to the jframe as opposed to adding the panel i just made which is called 'panel'

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Paint help please!

    Quote Originally Posted by swedishfished View Post
    I'm just a little confused over why i should be adding 'this' to the jframe as opposed to adding the panel i just made which is called 'panel'
    this is the reference to the current instance of your class (the one that extended JPanel).
    And that is the panel in which you overrode the paintComponent method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Paint help please!

    Quote Originally Posted by swedishfished View Post
    OK i got it all now! wow i was so stupid!!!! hahahah


    I'm just a little confused over why i should be adding 'this' to the jframe as opposed to adding the panel i just made which is called 'panel'
    'this' is Java's way of saying yourself' suppose you are a changeColor object that constructs itself; translated to English, you say to yourself: create a JFrame and add yourself to it and add a MouseClass object to yourself. Java uses the word 'this' for the purpose; other languages (such as Python) use 'self' and those words all refer to the particular object itself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Paint--- trying to paint an oval
    By noobie_nate in forum Java 2D
    Replies: 2
    Last Post: 06-05-2013, 06:45 PM
  2. How to get this paint?
    By Monster in forum New To Java
    Replies: 3
    Last Post: 04-15-2013, 02:01 PM
  3. Paint is invalid type for variable paint.
    By minibronya in forum New To Java
    Replies: 3
    Last Post: 05-25-2012, 06:52 AM
  4. Paint
    By ninjaturtlez in forum AWT / Swing
    Replies: 4
    Last Post: 12-17-2011, 05:15 AM
  5. Paint????
    By seanfmglobal in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 09:00 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
  •