This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Applets and the fly likes change color of an oval in a polygon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "change color of an oval in a polygon" Watch "change color of an oval in a polygon" New topic
Author

change color of an oval in a polygon

Alessandro Gigli
Greenhorn

Joined: Nov 30, 2003
Posts: 2
Hello,
I have created this applet in which I have a polygon in which I have to drag an oval, when the oval touches the edges of the polygon, the shape should change color. the applet works but i can't find a way of changing the color of my oval when it touches the edges, how can i do that??
The applet follows:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//import javax.swing.*;

class maze extends Applet
implements ActionListener, MouseListener, MouseMotionListener
{
Panel down;
Button reset;
Dimension size;
Image buffer;
Graphics g;
private int x = 200, y = 480;
private Font f;
public void init ()
{
this.setSize (600, 600);
setVisible (true);
this.g = getGraphics ();
reset = new Button ("Reset Game");
reset.addActionListener (this);
this.addMouseListener (this);
this.addMouseMotionListener (this);
f = new Font ("Helvetica", Font.BOLD, 36);
setLayout (new BorderLayout ());
down = new Panel ();
add ("South", down);
down.add (reset);
}

public void paint (Graphics g)
{
//Dimension d = size();
//if ((g == null) || (d.width != size.width) || (d.height != size.height)){
//size = d;
//buffer = createImage(d.width,d.height);
//g = buffer.getGraphics();
//}

g.setColor (Color.blue);
g.fillOval (x - 30, y - 30, 60, 60);

int xValues[] = {160, 240, 360, 360, 440, 440, 360, 240, 240, 160};
int yValues[] = {80, 80, 360, 80, 80, 520, 520, 240, 520, 520};
Polygon Polyg = new Polygon (xValues, yValues, 10);
g.drawPolygon (Polyg);

if ((x > 360) && (y < 90))
{
g.setColor (Color.yellow);
g.fillOval (x - 30, y - 30, 60, 60);
g.setColor (Color.black);
g.setFont (f);
g.drawString ("Congratulations, You Win!", 60, 50);
}
//if ((x <= 190) && (y > 80))
//{
//g.setColor (Color.red);
//g.fillOval (x - 30, y - 30, 60, 60);
//}
//if ((y < 110) && (x < 240))
//{
//g.setColor (Color.red);
//g.fillOval (x - 30, y - 30, 60, 60);
//}
//if ((x <= 180) && (y >= 500))
//{
//g.setColor (Color.red);
//g.fillOval (x - 30, y - 30, 60, 60);
//}
//if ((x >= 210) && (y > 80)){
//g.setColor (Color.red);
//g.fillOval (x - 30, y - 30, 60, 60);
//}


}

public void actionPerformed (ActionEvent e)
{
if (e.getSource () == reset)
{
x = 200;
y = 480;
repaint ();
}
}
public void mouseDragged (MouseEvent e)
{
x = e.getX ();
y = e.getY ();
repaint ();
}

public void mouseClicked (MouseEvent e)
{
}

public void mouseEntered (MouseEvent e)
{
}

public void mouseExited (MouseEvent e)
{
}

public void mouseMoved (MouseEvent e)
{
}

public void mouseReleased (MouseEvent e)
{
}

public void mousePressed (MouseEvent e)
{
}
//public void update(Graphics g) {paint(g);}
}
thank you very much.
[ December 16, 2003: Message edited by: Alessandro Gigli ]
[ December 16, 2003: Message edited by: Alessandro Gigli ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: change color of an oval in a polygon
 
Similar Threads
Why does it not show the new coordinates?!!??!!
Problem With GUI of Net Beans
runtime error - null pointer exception
help with oval
Need to add a getclickCount()