PDA

Click to See Complete Forum and Search --> : [RESOLVED]Java draw circles on image


jlbovo
Nov 3rd, 2006, 02:45 PM
hello, i found the answer to my last post finally. now i have a new task to implement. I need to draw a circle or oval when a point on an image is clicked. For instance if you were to click on the eye of a person in a image in a jpanel, i need to draw a red circle of where i just clicked. i am researdhing and figured out this, and i can post more of my code when i have more time, for i am pressed right now...


some thing laong the lines of...


setColor(red);
fillCircle(newX,newY, 20);



my only question now si what does the 20 mean ?

remind you i am very new to java, but i will sit down later with my manual to try to implement. I'm sure it would be more helpful if i posted my code, but If anyone knows of what along the lines I am trying to do you willingness to help would be appercaited thanks. justin

ComputerJy
Nov 3rd, 2006, 03:11 PM
I'm almost sure you didn't write any of the code, because if you did. You'd know that fillCircle is not Java built-in method. And since you are not calling it from any other object then I'd take a wild guess and say the body of the method is in your class so take another look and tell us what does it look like!!

jlbovo
Nov 3rd, 2006, 03:47 PM
i know, it not mine at all, i was just running thru a manual trying to get an idea of what i'm going for, just so other know. here's my code....


private class mouseImgEventHandler implements MouseListener, MouseMotionListener {

//empty body of methods.
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}

//get X-Y location when mouse is clicked.
public void mouseClicked(MouseEvent e) {
int currentX = e.getX();
int currentY = e.getY();

//display X-Y on the message board.
if(panelID == 1)
parent.leftDisplay.setText("X: " + currentX + ", Y: " + currentY);
if(panelID == 2)
parent.rightDisplay.setText("X: " + currentX + ", Y: " + currentY);
if(panelID == 3)
parent.resultDisplay.setText("X: " + currentX + ", Y: " + currentY);
//repaint();
}
}



i just put that up the as an example of what i am tryign to do.

based on the x/y coordinates i get when i click the mouse, I want to draw circles..


public void paintComponent(Graphics graphics) {
Graphics2D g = (Graphics2D)graphics.create();
// eyes
g.setColor(Color.red);
g.fillOval(30, 30, 8, 8);
g.fillOval(62, 30, 8, 8);




.but how do i pass the mouse coordnates to the drawing of the oval ?

if(panelID == 1)
parent.leftDisplay.setText("X: " + currentX + ", Y: " + currentY);
// eyes
g.setColor(Color.red);
g.fillOval(30, 30, 8, 8);
g.fillOval(62, 30, 8, 8);


and where would i incldue a line like this

public void paintComponent(Graphics graphics) {
Graphics2D g = (Graphics2D)graphics.create();

...at the begginign of my mouse handler, or some where else ?



does that clear my idea more, i thanks you again for stopping to help. justin

ComputerJy
Nov 3rd, 2006, 03:53 PM
but how do i pass the mouse coordnates to the drawing of the oval ?
Are you serious?? int currentX = e.getX();
int currentY = e.getY();
and where would i incldue a line like this
Anywhere you want, just make sure you declare the object before you start using it
And BTW it'sGraphics2D g = (Graphics2D)graphics;

jlbovo
Nov 3rd, 2006, 04:01 PM
i'm sorry so sorry, like i say i am a begginer. No formal learning/training in this.

so....


if(panelID == 1)
parent.leftDisplay.setText("X: " + currentX + ", Y: " + currentY);
// eyes
g.setColor(Color.red);
g.fillOval(currentX, currentY, 8, 8);



i think thats what i'm going for, will that draw a small oval on the spot that i click ??
thanks again - justin

jlbovo
Nov 3rd, 2006, 04:09 PM
here's my code


// mouse event handler, as an inner class of ImagePanel class.
//
private class mouseImgEventHandler implements MouseListener, MouseMotionListener {

//empty body of methods.
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}

//get X-Y location when mouse is clicked.
public void mouseClicked(MouseEvent e) {
int currentX = e.getX();
int currentY = e.getY();
Graphics2D g = (Graphics2D)graphics;

//display X-Y on the message board.
if(panelID == 1)
parent.leftDisplay.setText("X: " + currentX + ", Y: " + currentY);
// eyes
g.setColor(Color.red);
g.fillOval(currentX, currentY, 8, 8);
if(panelID == 2)
parent.rightDisplay.setText("X: " + currentX + ", Y: " + currentY);
if(panelID == 3)
parent.resultDisplay.setText("X: " + currentX + ", Y: " + currentY);
//repaint();
}
}



here's my errors...

UIDDemo1.java:349: cannot find symbol
symbol : variable graphics
location: class ImagePanel.mouseImgEventHandler
Graphics2D g = (Graphics2D)graphics;
^
1 error

.....i will continue working on this code and i'm on my way to work. i thanks you for all your time and effort !!! justin

ComputerJy
Nov 3rd, 2006, 04:44 PM
replaceGraphics2D g = (Graphics2D)graphics; withGraphics2D g = (Graphics2D)this.getGraphics();

jlbovo
Nov 4th, 2006, 02:51 AM
thaks again but i keep gettin an error...

UIDDemo1.java:350: cannot find symbol
symbol : method getGraphics()
location: class ImagePanel.mouseImgEventHandler
Graphics2D g = (Graphics2D)this.getGraphics();
^
1 error


..do i need to import something at the begging of the file so i can call Graphics ? -justin

ComputerJy
Nov 4th, 2006, 05:07 AM
Does your class extend "JFrame, JPanel, Panel, Applet or ...etc"? This method "getGraphics" is inherited from the component class

jlbovo
Nov 4th, 2006, 09:50 AM
it's a class that uses jpanel..


class ImagePanel extends JPanel {

ComputerJy
Nov 4th, 2006, 10:41 AM
remove the "this", just use the "getGraphics();"

jlbovo
Nov 4th, 2006, 11:17 AM
awesome dude, thansk for all your help man. it works, it draws the circles !! i am so thanksful, I have learned alot from you over this. Thanks again for helping...I'm sure i'll have more questions, but now this topics is resolved. if i need more info i'll be sure to ask again ! - justin