Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: How to fix this code?

  1. #1
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to fix this code?

    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

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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to fix this code?

    First, I'd encourage you to use a JFrame rather than a Frame (see How to use JFrames). Second, show is deprecated. Use setVisible(true). Next, you shouldn't override the paint method, especially of the Frame itself. Create a JPanel, add it to the JFrame, and override the paintComponent method of the JPanel (see Performing Custom Painting)

  3. #3
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Hi copeg

    Thanks very much for reply, now I changed Frame to Jframe and use method paintcompinent, but it does not make any difference. (You said show() is depreciated, but actually it works fine as it does make frame dispaly, so I didn't change that)

    Any more thoughts?

    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;
     
        public void init()
        {
            setTitle("Poppy");
            setSize(300, 300);
            show();
            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 ice; November 22nd, 2010 at 08:04 PM.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    490
    Thanks
    8
    Thanked 46 Times in 44 Posts

    Default Re: How to fix this code?

    A word of advice: if you really want members here to take interest in helping you, format your code according to convention. The creative indents, code lines starting with { etc have put me off even trying to read it.

    So, without attempting to read your code, here are some of the most common mistakes.
    -- not adding the component that does the custom painting to the visible component hierarchy
    -- not using an appropriate layout manager and/or not ensuring an appropriate preferredSize, resulting in a component of default size (usually [0, 0])
    -- not calling repaint() on the component that does the custom painting but on some other component

    db

  5. #5
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Hi db
    I have modified it with adding component and repaint() into init() method, please see above new code, but it still doesn't make a difference.
    Any more ideas?
    Thanks heaps

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    490
    Thanks
    8
    Thanked 46 Times in 44 Posts

    Default Re: How to fix this code?

    You're still not repainting the component that actually does the custom painting.

    db

    edit Nor are you adding the mouse listeners to the component that draws the Polygon. Each component has its own coordinate system. A Point on the JFrame that is contained in the Polygon isn't necessarily within the display are of the Polygon painted on the panel.
    Last edited by Darryl.Burke; November 21st, 2010 at 07:15 AM.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,655
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: How to fix this code?

    ...
        MyPanel panel = new MyPanel();
            f.getContentPane().add(panel);
            repaint();
        }
     
        class MyPanel extends JPanel
        {
            public void paintComponent(Graphics g)
            {
                g.setColor(Color.black);g.fillPolygon(p);
            }
     
       }

    To add to what Darryl.Burke said, when you make the call repaint();, you are calling the OctagonDrag's default repaint() method. For example, you are effectively saying this.repaint(). What you want to do is call the MyPanel's repaint() method. To do that, you need to say panel.repaint();.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Quote Originally Posted by Darryl.Burke View Post
    You're still not repainting the component that actually does the custom painting.

    db

    edit Nor are you adding the mouse listeners to the component that draws the Polygon. Each component has its own coordinate system. A Point on the JFrame that is contained in the Polygon isn't necessarily within the display are of the Polygon painted on the panel.
    Hi db
    Thanks for more idea. I don't inderstand why you said I didn't add mouse listeners to the component? But "addMouseListener(new MousePressListener());" was in init method. ?
    Last edited by ice; November 22nd, 2010 at 04:14 AM.

  9. #9
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Quote Originally Posted by aussiemcgr View Post
    ...
        MyPanel panel = new MyPanel();
            f.getContentPane().add(panel);
            repaint();
        }
     
        class MyPanel extends JPanel
        {
            public void paintComponent(Graphics g)
            {
                g.setColor(Color.black);g.fillPolygon(p);
            }
     
       }

    To add to what Darryl.Burke said, when you make the call repaint();, you are calling the OctagonDrag's default repaint() method. For example, you are effectively saying this.repaint(). What you want to do is call the MyPanel's repaint() method. To do that, you need to say panel.repaint();.
    Hi aussiemcgr
    Thanks for your advice, I have changed repaint(); to panel.repaint();, then ran it, it still display exactly same as before: an empty frame and it can't be closed by pressing "x".
    more ideas?

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to fix this code?

    Are you reading the stack trace? Any exceptions? This is important information...on close look the variable 'f' is never initialized, and the program sequence sets the frame to visible then, when you try to call a function of f you should get a NullPointerException. A recipe for the behavior you have. In addition, show is deprecated: you should use setVisible.

  11. #11
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    490
    Thanks
    8
    Thanked 46 Times in 44 Posts

    Default Re: How to fix this code?

    Why does the class extend Frame? It's never used as a Frame.

    db

  12. #12
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Hi copeg
    I just read a bit stack trace, then looked at the error I got, which is:
    run:
    Exception in thread "main" java.lang.NullPointerException
    at brainydraw.OctagonDrag.init(OctagonDrag.java:24)
    at brainydraw.OctagonDrag.main(OctagonDrag.java:103)
    Line24 is f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); But why? Why?why ?
    why can't 'f' get initialized? I declared it in "private JFrame f;" in line 17.
    (You said show() is depreciated, but actually it works fine as it does make frame dispaly, so I didn't change that)

    Guys, do I need something else to initialize "f"?

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to fix this code?

    Quote Originally Posted by ice View Post
    Hi copeg
    I just read a bit stack trace, then looked at the error I got, which is:

    Line24 is f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); But why? Why?why ?
    why can't 'f' get initialized? I declared it in "private JFrame f;" in line 17.
    You need to initialize the object: (see Creating Objects)
    f = new JFrame();

    (You said show() is depreciated, but actually it works fine as it does make frame dispaly, so I didn't change that)

    Guys, do I need something else to initialize "f"?
    It may work, but you should not rely on deprecated methods. There is a reason they are deprecated, and its better to adhere to documentation than find out the hard way why a method is deprecated.

  14. #14
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Great, guys, thanks a lot, now I solved to close the frame, see below updated code. But that black Polygon is still not displayed, where is it?

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

  15. #15
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to fix this code?

    You are adding components to the frame after it is realized (visible). Add things prior, in this example move the setVisible method to the last call in the init function (in which case repaint is not necessary). As Darryl.Burke noted, there isn't much reason to have your OctagonDrag class extend JFrame if it is not being used.

  16. #16
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Hi copeg
    Thanks, I have shifted setVisible to below, but it still shows an empty frame. You mean OctagonDrag class not used? Is this the reason that black polygon is not shown?

    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();
            setVisible(true);
        }
    Last edited by ice; November 25th, 2010 at 04:10 PM. Reason: shift setVisible to below

  17. #17
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: How to fix this code?

    NOTE: I'm not trying to offend you, I'm trying to help you.

    You should delete your current code, learn a little about Java swing/AWT, and try again.

    Putting all of your project code into one file makes your code extremely unorganized. (illegible) I like to structure my projects with a main class, a gui class, and a device class. (Keyboard and Mouse)

    //Main class
    import javax.swing.*;
     
    public class ProjectMain {
       public static void main(){
          JFrame f = new JFrame("Poppy");
          f.getContentPane().setPreferredSize(new Dimension(300,300));
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
          gui gui = new gui(); //This is the class that handles all the drawing
          devices dev = new devices(); //Mouse and Keyboard class
          gui.addMouseListener(dev); 
          gui.addMouseMotionListener(dev);
     
          f.add(gui);
          f.pack();
          f.setVisible(true);
     
       }
    }

    Now the GUI class
    //GUI Class
    import javax.swing.*;
    import java.awt.*;
     
    public class gui extends JPanel { //Since this class extends JPanel it can be added to your JFrame in the main
        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;
     
     
       public void paintComponent(Graphics g) {
          g.setColor(Color.black);
          g.fillPolygon(p);
       }
    }

    Now the device class (Mouse and Keyboard)
    //device class
    import java.awt.event.*;
     
    public class devices implements MouseListener, MouseMotionListener {
     
       //implementing the mouse clicking methods
       public void mousePressed(MouseEvent e){}
       public void mouseReleased(MouseEvent e){}
       public void mouseClicked(MouseEvent e){} //mousePressed is more reliable 
     
       //implementing the mouse motion methods
       public void mouseMoved(MouseEvent e){}
       public void mouseDragged(MouseEvent e){}
       public void mouseEntered(MouseEvent e){}
       public void mouseExited(MouseEvent e){}
     
    }

    This code isn't ment to work. You're going to have to add/make changes to it. The code is ment to show you a basic structure for an organized GUI project. Most likely Darryl.Burke and or Copeg are going to correct some of my mistakes, show you better methods, comment on the irony. (If I made mistakes on the Swing/AWT parts) Like I said before, this code is only for knowledge.

  18. #18
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Hi brt93yoda

    Thanks very much for sharing what you know. But the main problem still can't be solved - the black polygon is not shown.
    Any ways to make it visible?

    Thanks heaps

  19. #19
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to fix this code?

    Quote Originally Posted by ice View Post
    Hi brt93yoda

    Thanks very much for sharing what you know. But the main problem still can't be solved - the black polygon is not shown.
    Any ways to make it visible?
    It doesn't work because it only partially implements my suggestion from above. There are 2 setVisible calls...

  20. #20
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Hi copeg
    Sorry, that extra setVisible was there by accident, now I removed it. Actually I fully implented your suggestionn when I ran it, but the black polygon is really not shown.

  21. #21
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    hi guys, the problem is still not solved, anyone got more ideas?

  22. #22
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to fix this code?

    Can you post a short compilable example of the code to this point that demonstrates the problem? With all these posts its hard to be sure where the code stands at this point.

  23. #23
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Ok, copeg, I am not sure which part of the code that causes the problem - it doesn't draw polygon. Here is the whole 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);
            setVisible(true);
            //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();
     
        }
    }

    Thanks heaps

  24. #24
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to fix this code?

    There are still a few things wrong with your code in general. First, no need to have your class extend JFrame when you are using a JFrame variable for most of everything (the init() function has confusion when setting values for OctagonDrag and the variable f, both of which are JFrames, one of which is not necessary in its present state). Second, you should always call super.paintComponent(g) in the paintComponent method. Next, the coordinates are not octagon coordinates, rather a line (change fillPolygon to drawPolygon and you will see what I mean). Next, re-read all the above posts because some of these considerations were noted already and I may have missed a few. Lastly, I'll reiterate Brt93yoda'sI'd recommendation to study the basic GUI tutorials over at sun even if you have already.

  25. #25
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to fix this code?

    Thank you a lot copeg, your suggestion of "replace fillPolygon with drawPolygon" is so useful, I can see it is a line now. But how to change those coordinates to show a octagon?

Page 1 of 2 12 LastLast