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

Thread: Line Drawing Program

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Line Drawing Program

    Hello, I'm attempting to make a program which will draw a line with a length of 10 units wherever the user clicks. However, it's not working, and clicking anywhere in the window does nothing.

    Here is the program:

     
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.MouseInfo;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
     
    public class LineCreator extends Applet implements MouseListener{
    	int MouseClicked = 0;
    	public void init(){
    		resize(501,501);
    		this.addMouseListener(this);
                    resize(501, 501);
    	}
    	public void paint (Graphics g)
    	{
    		if(MouseClicked == 1)
    		{
    			int MouseXValue = MouseInfo.getPointerInfo().getLocation().x;
    			int MouseYValue = MouseInfo.getPointerInfo().getLocation().y;
    			g.drawLine(MouseXValue, MouseYValue, MouseXValue+10, MouseYValue+10);
    			MouseClicked = MouseClicked + 1;
    		}
    	}
    	public void mouseClicked(MouseEvent e) {
    		MouseClicked = MouseClicked + 1;
    	}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    }

    Any help would be greatly appreciated!


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Line Drawing Program

    Here, try this.

     
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.MouseInfo;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
     
    public class LineCreator extends Applet implements MouseListener{
     
     
    	public void init(){
    		resize(501,501);
    		this.addMouseListener(this);
                    resize(501, 501);
    	}
     
    	public void paint (Graphics g)
    	{
                int MouseXValue = MouseInfo.getPointerInfo().getLocation().x;
                int MouseYValue = MouseInfo.getPointerInfo().getLocation().y;
                g.drawLine(MouseXValue, MouseYValue, MouseXValue+10, MouseYValue+10);
     
    	}
     
    	public void mouseClicked(MouseEvent e) {
                    repaint();
    	}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    }

  3. The Following 2 Users Say Thank You to relixus For This Useful Post:

    javapenguin (November 13th, 2010), The_Mexican (November 12th, 2010)

  4. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Line Drawing Program

    Ok, I just added the MouseClicked = MouseClicked + 1; part right above the repaint(); and it worked! Thanks!

  5. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Line Drawing Program

    Variable names should start with a lowercase letter.

    db

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Line Drawing Program

    They don't have to, but, that is the Java convention.

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Line Drawing Program

    Quote Originally Posted by The_Mexican View Post
    Hello, I'm attempting to make a program which will draw a line with a length of 10 units wherever the user clicks. However, it's not working, and clicking anywhere in the window does nothing.

    Here is the program:

     
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.MouseInfo;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
     
    public class LineCreator extends Applet implements MouseListener{
    	int MouseClicked = 0;
    	public void init(){
    		resize(501,501);
    		this.addMouseListener(this);
                    resize(501, 501);
    	}
    	public void paint (Graphics g)
    	{
    		if(MouseClicked == 1)
    		{
    			int MouseXValue = MouseInfo.getPointerInfo().getLocation().x;
    			int MouseYValue = MouseInfo.getPointerInfo().getLocation().y;
    			g.drawLine(MouseXValue, MouseYValue, MouseXValue+10, MouseYValue+10);
    			MouseClicked = MouseClicked + 1;
    		}
    	}
    	public void mouseClicked(MouseEvent e) {
    		MouseClicked = MouseClicked + 1;
    	}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    }

    Any help would be greatly appreciated!
    mouseClicked() has a parameter which you didn't pass in your if statement.

    Also, mouseClicked is void anyway and so cannot return anything so it isn't equal to anything.

    oh, I see, that's an int.

    To get it to draw a line from where it was pressed to where it was released, you could have some variable that will show location of mouse is where it was pressed and where it was released with the other methods you implemented.

Similar Threads

  1. [SOLVED] Drawing on JFrame
    By kbarrett1989 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 31st, 2010, 03:41 AM
  2. drawing a BufferedImage onto a JPanel
    By nemo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2010, 07:48 PM
  3. Drawing
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 02:43 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM