Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

change color of an oval in a polygon

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic