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