Hi guys
I have 2 problems here:
1) Below code does not draw a black Polygon on the frame, only shows an empty Frame named "Poppy"
2) Once the frame shows, I can't close it by clicking "x" at the right top corner.
Does anyone know why?
Thanks heaps
code:
package brainydraw;
import java.awt.*;
import java.awt.event.*;
public class OctagonDrag extends Frame implements MouseMotionListener
{
private int[] x = {150, 175, 175, 150, 100, 75, 75, 100};
private int[] y = {150, 175, 175, 150, 100, 75, 75, 100};
private Polygon p = new Polygon(x, y, 8);
private int oldx; //Saves previous Polygon position
private int oldy;
private Frame f;
public void init()
{
f = new Frame("Poppy");
f.setSize(300, 300);
f.show();
addMouseListener(new MousePressListener());
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.setColor(Color.black);g.fillPolygon(p);
}
public void mouseMoved(MouseEvent event)
{
}
public void mouseDragged(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
if (p.contains(x, y))
{
p.translate(x - oldx, y - oldy);
oldx = x;
oldy = y;
repaint();
}
}
class MousePressListener extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
if (p.contains(x, y))
{
oldx = x;
oldy = y;
}
}
}
class CloseWindow implements WindowListener
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
public void windowOpened(WindowEvent event)
{
}
public void windowClosed(WindowEvent event)
{
}
public void windowIconified(WindowEvent event)
{
}
public void windowActivated(WindowEvent event)
{
}
public void windowDeactivated(WindowEvent event)
{
}
public void windowDeiconified(WindowEvent event)
{
}
}
public static void main(String[] args)
{
OctagonDrag test = new OctagonDrag();
test.init();
}
}