PDA

Click to See Complete Forum and Search --> : Drawing a shape in java???


ddmeightball
Jan 17th, 2005, 01:29 PM
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?

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>

System_Error
Jan 17th, 2005, 03:18 PM
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.

ddmeightball
Jan 17th, 2005, 05:47 PM
can anyone else help me???

System_Error
Jan 17th, 2005, 07:21 PM
can anyone else help me???


LOL, AM I NOT GOOD ENOUGH!!

ddmeightball
Jan 18th, 2005, 09:22 AM
No, its not that. I did what you mentioned. I just want to see if anyone else has other ideas....

ddmeightball
Jan 18th, 2005, 11:09 AM
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.

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();
}
}
}

System_Error
Jan 18th, 2005, 02:23 PM
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! :bigyello:

ddmeightball
Jan 19th, 2005, 09:32 AM
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.