Results 1 to 12 of 12
  1. #1
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default How to display the black Polygon that I draw on the panel?

    Hi guys

    I have 2 problems here:

    1. Below code does not show a black Polygon that I draw on the panel, 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

    Java Code:
    package brainydraw;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import javax.swing.*;
    import javax.swing.JFrame;
    
    
    public class OctagonDrag extends JFrame 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 = new JFrame();
    
        public void init()
        {
            setTitle("Poppy");
            setSize(300, 300);
            setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addMouseListener(new MousePressListener());
            addMouseMotionListener(this);
            MyPanel panel = new MyPanel();
            f.getContentPane().add(panel);
            panel.repaint();
        }
    
        class MyPanel extends JPanel
        {
            public void paintComponent(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();
            
        }
    }
    Last edited by BeijingDuck; 11-23-2010 at 02:39 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got too many JFrames in your program, the first one being the program itself, or this, which extends JFrame and which is displayed, and the second being the variable f, which is null and is throwing a NPE when you call methods on it.

    Your next problem is that your Polygon's x and y points are all the same, and this will create a straight line, not a polygon.

  3. #3
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    Hi Fubarable
    Thanks for your advice, I have just initialised "f", now the frame can be closed.(second problem is soloved)
    As for my first problem, how did I make "Polygon's x and y points are all the same", at moment I can't even display that straight line, I'd be happier at least if I can see something the panel.
    Any more solutions?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BeijingDuck View Post
    Thanks for your advice, I have just initialised "f", now the frame can be closed.(second problem is soloved)
    If you are going to be using f as your JFrame, then your class itself should not extend JFrame and in fact should probably not extend anything. I see you've modified your code above, and I suggest you don't do that. Leave it as is and post your new code in a new post so folks later can see what changes have been made. I also see that you're adding a MouseListener to the main class, not f, and also setting the size of the main class, not f, which is confusing to me and the JVM. Again, the main class should not be a JFrame, and you shouldn't be doing these things to it.


    As for my first problem, how did I make "Polygon's x and y points are all the same", at moment I can't even display that straight line, I'd be happier at least if I can see something the panel.
    Any more solutions?
    For each pair of x's and y's, x equals y, which is the equation of a straight line. Sure it's a polygon, but it has no area and so can't be displayed. I suggest you vary your x's and your y's so they are not the same.

  5. #5
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    Hi Fubarable

    Thanks for your advice. Now I got more to ask.
    You mean the header "public class OctagonDrag extends JFrame implements MouseMotionListener" should not extend anything - should be like "public class OctagonDrag"?

    You said I should add a MouseListener to f instead of the main class, how to achieve that? change "addMouseListener(new MousePressListener());" to "f.addMouseListener(new MousePressListener());"

    You said the main class should not be a JFrame, but the main class has been OctagonDrag, has it?

    So I need to vary each pair of x's and y's, if I have this :
    private int[] x = {150, 175, 175, 150, 100, 75, 75, 100};and private int[] y = {100, 140, 130, 130, 110, 100, 30, 160};
    , have I varied them?

    Thanks heaps

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What happens when you try these changes?
    Hi Fubarable
    After these changes, it has two errors on line 25 f.addMouseMotionListener(this);and line 54 repaint();

    error on line 25 says:"method addMouseMotionListener in class java.awt.Component cannot be applied to given types required: java.awt.event.MouseMotionListener"

    line 54 says cannot find symbol method repaint()

    Any more thought on how to fix it?

    Java Code:
    package brainydraw;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import javax.swing.*;
    import javax.swing.JFrame;
    
    
    public class OctagonDrag //extends JFrame implements MouseMotionListener
    {
        private int[] x = {150, 175, 175, 150, 100, 75, 75, 100};
        private int[] y = {100, 140, 130, 130, 110, 100, 30, 160};
        private Polygon p = new Polygon(x, y, 8);
        private int oldx; //Saves previous Polygon position
        private int oldy;
        private JFrame f = new JFrame(); // you need to initialize it before you can call methods on it, eg f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); that was why the frame couldn't be closed.
        public void init()
        {
            f.setTitle("Poppy");
            f.setSize(300, 300);
            
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addMouseListener(new MousePressListener());
            f.addMouseMotionListener(this);
            MyPanel panel = new MyPanel();
            f.getContentPane().add(panel);
            //panel.repaint();
            f.setVisible(true);
        }
    
        class MyPanel extends JPanel
        {
            public void paintComponent(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();
        }
    }

  10. #10
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    Hi Guys
    The problem is still not solved, anyone got more ideas?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please see Darryl's post above. Are there any other places that you've cross-posted this question? Most of us avoid answering cross-posted questions for fear of performing effort (volunteer effort mind you) for no benefit, as the post has been answered or well discussed in cross-posts. If you do have to cross-post, you should notify all threads involved about all cross-posts.
    Last edited by Fubarable; 11-29-2010 at 04:05 AM.

  12. #12
    BeijingDuck is offline Member
    Join Date
    Nov 2010
    Posts
    23
    Rep Power
    0

    Default

    Hi Fubarable, glad you have said something. I am courious how does this thing operates.

    I thought the forum websites were non-profits and volunteers, all helpers here were volunteers, it's their kindness or hobby to heplp others here, they certainly won't have benefits from answering others'questions. So what benefits are you talkiing about here? - (If you involve in cross posts, you will not have benefits?)

    I sometimes post my questions in different websites is because I want to get more information or ideas from different thinkers or the question got stucked in one site.

    Thanks

Similar Threads

  1. red black tree
    By ahmakki in forum New To Java
    Replies: 1
    Last Post: 03-21-2010, 06:49 AM
  2. How to display image in a panel with JFileChooser
    By srisar in forum AWT / Swing
    Replies: 14
    Last Post: 10-11-2009, 06:06 PM
  3. Replies: 1
    Last Post: 02-20-2009, 04:16 PM
  4. Display graph in tabbed panel
    By Laura Warren in forum New To Java
    Replies: 3
    Last Post: 01-12-2009, 11:34 PM
  5. How to Draw a Polygon in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •