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();
    }
}

Please put the code in (CODE) before posting it!!

Man you are mixing between your class which is Frame and the private Frame.

Replace these lines in the init method:

f = new Frame("Poppy");
f.setSize(300, 300);
f.show();

by these lines:

setTitle("Poppy");
setSize(300, 300);
show();

Also call the method setDefaultCloseOperation wiht the parameter EXIT_ON_CLOSE .

Anyway use JFrame instead of Frame .

Regards,
Sami Andoni

Hi Sami

Thanks very much for your advice, now I have changed to below, but it still only shows an empty frame without a black Polygon on it; and I can't close it by clicking "x" at the right top corner.

Any more thoughts?

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 JFrame f;

public void init()
{
setTitle("Poppy");
setSize(300, 300);
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();
}
}

Hi Guy

The problem is still not solved, anyone has more ideas?

Thanks heaps

Hi Guy

The problem is still not solved, anyone has more ideas?

Thanks heaps

For your first problem where you cannot see the polygon is because you have not entered valid coordinates for polygon. Enter the coordinates of polygon in clockwise or anticlockwise manner.

Secondly to close your frame add this line in you init method

addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        System.exit(0);

Thank you gunjannigamfor, plus others'advice, I finally solved these 2 problems. Thank you all again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.