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

Thread: Drawing on JFrame

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

    Default Drawing on JFrame

    Hi, I'm fairly new to Java in the sense that I haven't programmed Java in 2 years. I am trying to get back into the swing of things, but I can't find any reference to what I need. I'm trying to write a simple drawing program that only utilizes the brush for now, I'll implement what else I need later.

    My code started out as:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class DrawWithMe extends JFrame
    {
    	int xvalue = -10, yvalue = -10;
    	public DrawWithMe()
    	{
    		getContentPane().add(new Label("Click and drag"), BorderLayout.SOUTH);
    		addMouseMotionListener
    		(
    			new MouseMotionAdapter()
    			{
    				public void mouseDragged(MouseEvent event)
    				{
    					xvalue = event.getX();
    					yvalue = event.getY();
    					repaint();
    				}
    			}
    		);
    		addKeyListener
    		(
    			new KeyAdapter()
    			{
    				public void keyPressed(KeyEvent event)
    				{
    					if(event.getKeyCode() == 27)
    					{
    						System.exit(0);
    					}
    				}
    			}
    		);
    		setSize(500,500);
    		setVisible(true);
    	}
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.black);
    		g.fillOval(xvalue, yvalue, 10, 10);
    	}
    	public static void main(String args[])
    	{
    		DrawWithMe application = new DrawWithMe();
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    As such it worked fairly well, but I want to implement in a JFrame. So now my code looks like:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class DrawWithMe extends JFrame
    {
    	JFrame frame = new JFrame("DrawWithMe");
     
    	int xvalue = -10, yvalue = -10;
    	public DrawWithMe()
    	{
    		frame.getContentPane().add(new Label("Click and drag"), BorderLayout.SOUTH);
    		frame.addMouseMotionListener
    		(
    			new MouseMotionAdapter()
    			{
    				public void mouseDragged(MouseEvent event)
    				{
    					xvalue = event.getX();
    					yvalue = event.getY();
    					repaint();
    				}
    			}
    		);
    		frame.addKeyListener
    		(
    			new KeyAdapter()
    			{
    				public void keyPressed(KeyEvent event)
    				{
    					if(event.getKeyCode() == 27)
    					{
    						System.exit(0);
    					}
    				}
    			}
    		);
    		frame.setSize(500,500);
    		frame.setVisible(true);
    	}
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.black);
    		g.fillOval(xvalue, yvalue, 10, 10);
    	}
    	public static void main(String args[])
    	{
    		DrawWithMe application = new DrawWithMe();
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    But now, it won't even draw. Could you point out what I may need to do? I just need a push in the right direction.

    Thank you in advanced for any and all help given,
    -K


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Drawing on JFrame

    public class DrawWithMe extends JFrame
    {
        JFrame frame = new JFrame("DrawWithMe");

    Inheritance 101, are you wanting to extend a JFrame or create a class that has a JFrame? You have done both and that is going to cause problems for you. When you extend JFrame, your class is a JFrame so you do not create a JFrame inside of it. When you extend JFrame, you have local access to all of JFrame's methods. You can call the JFrame's constructor by saying super("DrawWithMe"); in your class's constructor.

    Look into inheritance and see if you can fix some of the things you have.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: Drawing on JFrame

    And don't try to do custom painting in a top level window. That's just wrong.

    Learn how to do custom painting correctly here:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing on JFrame

    Thank you very much, that has actually cleared up. I had figured that might have been the problem, but, as was said earlier, I haven't done Java programming in 2 years.

    Thank you again.

Similar Threads

  1. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  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 in applets. Tell me why this way is wrong.
    By Asido in forum Java Theory & Questions
    Replies: 9
    Last Post: August 1st, 2010, 01:55 PM
  4. Replies: 4
    Last Post: July 20th, 2010, 07:15 AM
  5. Drawing
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 02:43 PM

Tags for this Thread