Results 1 to 8 of 8

Thread: Drawing a shape in java???

  1. #1

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Resolved Drawing a shape in java???

    Im supposed to Draw a shape or place an image on the screen and invite the user to click on it. When the user clicks on the shape, move it to a new random location. It has to be when the user clicks on the image/shape, not around it so I need to get the coordinates for the mouseclick event and compare them with location.x and location.y in an if-else statement.

    If the mouse coordinates == location.x and location.y then move image randomly. If not, do nothing.

    What I need to know how to do is draw a shape, like a square, then get the coordinates of the mouse click. Can anyone help?

    Code:
    package course;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * Title:        Draw Name
     * Description:  graphic example
     * Copyright:    Copyright (c) 2002
     * Company:      SCC
     * @author       Rich M
     * @version
     */
    
    public class DrawName extends JFrame{
    
      public DrawName() {
        super("Draw Name");
    
        //add the canvas panel
        DrawPanelApp canvas = new DrawPanelApp();
        getContentPane().add(canvas, BorderLayout.SOUTH);
    
        //add second panel
        JPanel exit = new JPanel();
        exit.setPreferredSize(new Dimension(100, 100));
        JButton exitButton = new JButton("Exit");
        exit.add(exitButton, BorderLayout.NORTH);
        exitButton.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
             	System.exit(0);
                }
            });
        this.getContentPane().add(exit);
    
      }
    
    
    
    
     public static void main(String[] args) {
        JFrame frame = new DrawName();
        frame.setSize(300,200);
        frame.pack();
        frame.setVisible(true);
     }
     
    }//end DrawName
    
    
    
    
    
    class DrawPanelApp extends JPanel{
      Point location;
      //constructor
      
      
      public DrawPanelApp(){
    
        this.setPreferredSize(new Dimension(200,150));
        this.addMouseListener(new MouseHandler());
        location = new Point(50,50);
      }
    
    
    
      //use the graphics context to draw
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("Painting ...");
        g.setColor(Color.red);
        g.drawString("Matt", location.x , location.y);
      }
      
      
      class MouseHandler extends MouseAdapter{
    
        public void mouseClicked(MouseEvent e){//e.getPoint
          location.x = (int)(Math.random()* 100);
          location.y = (int)(Math.random()* 100);
          repaint();
        }
      }
    }

    <Moderator added green checkmark in the first thread>
    Last edited by NoteMe; Feb 17th, 2005 at 07:28 AM.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Drawing a shape in java???

    fill(new Rectangle(x, y, w, h));


    that's how to draw a rectangle.


    public void mouseClicked(MouseEvent me)
    {
    int x, y;
    x = me.getX();
    y = me.getY();
    }


    and that will get the coordinates of a mouse click.



    I wish I could help you a little more, but my compiler has something wrong with it, and goes crazy when I do anything with graphics.

  3. #3

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Drawing a shape in java???

    can anyone else help me???

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Drawing a shape in java???

    Quote Originally Posted by ddmeightball
    can anyone else help me???

    LOL, AM I NOT GOOD ENOUGH!!

  5. #5

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Drawing a shape in java???

    No, its not that. I did what you mentioned. I just want to see if anyone else has other ideas....

  6. #6

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Drawing a shape in java???

    Here is the full code of what I have so far....Anymore help would be appreciated. I added an .Zip attachment of the file that I am working on and a score sheet for the assignment.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * Title:        Assign 1
     * @author       Matthew Davis
     * 				 SECC
     */
    
    public class DrawName extends JFrame{
    
      public DrawName() {//where its drawing the buttons, name and square.
        super("Moving Square, WHOOPIE!!!!");
    
        //add the canvas panel
        DrawPanelApp canvas = new DrawPanelApp();
        
        getContentPane().add(canvas, BorderLayout.SOUTH);
    
        //add second panel
        JPanel exit = new JPanel();
        exit.setPreferredSize(new Dimension(100, 100));
        JButton exitButton = new JButton("Exit");
        exit.add(exitButton, BorderLayout.CENTER);
    
        exitButton.addActionListener(new ActionListener() 
        	{
    			public void actionPerformed(ActionEvent e) 
    				{
             			System.exit(0);
                	}
            }
            );
            
        this.getContentPane().add(exit);
        
    	//add third panel for name drawing
    	JPanel namePan = new JPanel();
    
    
      }
    
    public class MyPanel extends JPanel
    {
      //....
    }
    
    
    
     public static void main(String[] args) 
     {
        JFrame frame = new DrawName();
        frame.setSize(300,200);
        frame.pack();
        frame.setVisible(true);
     }
     
    }//end DrawName
    
    
    
    
    
    class DrawPanelApp extends JPanel//Draws image or shape in a panel, use a seperate class that exteends Jpanel
    {
      Point location;
      Point mouseLocation;
      //constructor
      
      
      public DrawPanelApp(){
    
        this.setPreferredSize(new Dimension(200,150));
        this.addMouseListener(new MouseHandler());
        location = new Point(50,50);
        
      }
    
    
    
      //use the graphics context to draw
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("The red square should be moving ...");
    
        g.setColor(Color.red);
    	g.drawRect(50, 50, 50, 50);
    
      }
      
      
      class MouseHandler extends MouseAdapter{//use MouseAdapter Class
    
        public void mouseClicked(MouseEvent e){//e.getPoint MouseClicked method redraws image or shape.
        mouseLocation=e.getPoint();
    
    	if (mouseLocation.x==location.x && mouseLocation.y==location.y)
    		{
    			location.x = (int)(Math.random()* 100);
    			location.y = (int)(Math.random()* 100);
    			repaint();
    		}
    		//repaint();
        }
      }
    }

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Drawing a shape in java???

    Quote Originally Posted by ddmeightball
    No, its not that. I did what you mentioned. I just want to see if anyone else has other ideas....

    I know!! I was just playing around with you!

  8. #8

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Drawing a shape in java???

    I figured out the MouseHandler error. That was the name of the class that I was calling to decide whether or not the square was clicked. I changed it to MouseAdapter and it worked just fine now. Thanks everyone for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width