|
-
Jan 17th, 2005, 02:29 PM
#1
Thread Starter
Addicted Member
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.
-
Jan 17th, 2005, 04:18 PM
#2
Frenzied Member
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.
-
Jan 17th, 2005, 06:47 PM
#3
Thread Starter
Addicted Member
Re: Drawing a shape in java???
can anyone else help me???
-
Jan 17th, 2005, 08:21 PM
#4
Frenzied Member
Re: Drawing a shape in java???
 Originally Posted by ddmeightball
can anyone else help me???
LOL, AM I NOT GOOD ENOUGH!!
-
Jan 18th, 2005, 10:22 AM
#5
Thread Starter
Addicted Member
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....
-
Jan 18th, 2005, 12:09 PM
#6
Thread Starter
Addicted Member
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();
}
}
}
-
Jan 18th, 2005, 03:23 PM
#7
Frenzied Member
Re: Drawing a shape in java???
 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!
-
Jan 19th, 2005, 10:32 AM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|