Results 1 to 14 of 14
  1. #1
    G4CKT is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Need some GUI help!

    [SOLVED]

    Hi again everyone.

    So my issue was that the code which I initially had kept on opening new GUI's.
    My initial GUI would be created and plotted with a set of points. The GUI interface I had set up would take in a certain "Epsilon" (Error) and then a simplify button would be pressed in order to plot new points using the epsilon factor.

    First of all in order to make the GUI plot a new plot in the same GUI I used the removeAll() and reValidate() functions. By using these functions I was able to get the GUI to clear each time the button was pressed and then re-validated with the new epsilon and new points. I then found another error which was due to an error with scaling my epsilon to work with the coordinates which I had.

    This is what the last edit looked like with all my problems fixed. There are missing class files from here. Yeah, its really messy because in my initial lab I began by using a Double[] which later I had to switch to a self created class called Point[] which would hold x&y

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JPanel;
    import java.awt.event.*;
    
    public class simplecode2 {
    
        static double i;
        static JButton itt;
        static JButton applyIndex;
        static JLabel displayIndex;
        static JTextField indexField;
        protected static JTextArea textArea;
        public static Double[] x_screen2;
        public static Double[] y_screen2;
        public static Double[] x_orig;
        public static Double[] y_orig;
        private final static String newline = "\n";
        public static JPanel main = new JPanel();
        public static JPanel subPanel1 = new JPanel();
    
        public static void draws(Double[] x_screen, Double[] y_screen)
            {
    
            x_screen2 = new Double[x_screen.length];
            y_screen2 = new Double[y_screen.length];
            x_screen2 = x_screen;
            y_screen2 = y_screen;
            x_orig = new Double[x_screen.length];
            y_orig = new Double[y_screen.length];
            x_orig = x_screen;
            y_orig = y_screen;
    
            i = 0;
    
    
        JFrame frame = new JFrame("Polygon Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        ButtonListener listener = new ButtonListener();
        itt = new JButton("Simplify");
        itt.addActionListener(listener);
        itt.setAlignmentX(Component.CENTER_ALIGNMENT);
    
        subPanel1.setPreferredSize (new Dimension(200, 600));
        subPanel1.setBackground (Color.white);
        JLabel label2 = new JLabel ("Andrew Ma");
        subPanel1.add(label2);
        label2.setAlignmentX(Component.CENTER_ALIGNMENT);
    
        main.setBackground(Color.black);
        
        JPanel textPanel = new JPanel();
        
        textPanel.setMaximumSize(new Dimension(250,40));
        textPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
        
        JLabel setIndex = new JLabel("Epsilon");
        indexField = new JTextField(10);
        indexField.setSize(new Dimension(50, 20));
        textPanel.add(setIndex);
        textPanel.add(indexField);
        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
          
        subPanel1.setLayout( new BoxLayout (subPanel1 , BoxLayout.Y_AXIS));
        subPanel1.add(Box.createRigidArea (new Dimension (0,175)));
        subPanel1.add(itt);
        subPanel1.add(Box.createRigidArea (new Dimension (0,100)));
        subPanel1.add(textPanel);
    
        //main.removeAll();
        MyPanel p = new MyPanel(x_screen2, y_screen2);    
        main.add(p);
        main.add(subPanel1);
        frame.getContentPane().add(main);
        frame.pack();
        frame.setVisible(true);
    
    }
    
    private static class ButtonListener implements ActionListener {
        public void actionPerformed (ActionEvent event ) {
           
            String text = indexField.getText();
            i = Double.parseDouble(indexField.getText());
            
            i = i * 1.8682771440242234;
    
            textArea.append(text + newline);
            indexField.selectAll();
            textArea.setCaretPosition(textArea.getDocument().getLength());
            
            if (i == 0)
                {
                 x_screen2 = x_orig;
                 y_screen2 = y_orig;
    
                main.removeAll();
                MyPanel p = new MyPanel(x_screen2, y_screen2);
                main.add(p);
                main.add(subPanel1);
                main.revalidate();
                }
    
            else if (i > 0)
           {
                Point[] resultSet = new Point[x_screen2.length];
                Point[] pointList = new Point[x_screen2.length+y_screen2.length];
                resultSet = douglasPeucker.comp(x_orig, y_orig, i, pointList);
                Double[] x_screen = new Double[resultSet.length];
                Double[] y_screen = new Double[resultSet.length];
    
                for (int i = 0 ; i<resultSet.length;i++)
                {          
                    x_screen[i] = resultSet[i].getX();
                    y_screen[i] = resultSet[i].getY();
    
                }
    
                main.removeAll();
                MyPanel p = new MyPanel(x_screen, y_screen);
                main.add(p);
                main.add(subPanel1);
                main.revalidate();
           }
    
         }
    
       }
    
     }


    Thanks,

    G4CKT
    Last edited by G4CKT; 11-14-2010 at 08:54 PM.

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

    Default

    It's missing classes and won't compile or run for us. Please check out the link in my signature about creating and posting an SSCCE. Doing this will help you get help.

    Edit 1: Also, you most definitely should not have static fields and methods in this class, such as "draws(...)".

    Edit 2: And your draws method is creating a new JFrame inside of it, so there should be no surprise that you're seeing what you're seeing. I think that you should scrap this code, and start afresh, but instead of creating JFrames inside of drawing methods, have your SimpleCode extend JPanel and place in your main method, create a single SimpleCode object, create one JFrame, place your SimpleCode object in the JFrame's contentPane, and in the draw method, add points to an ArrayList of Point held by SimpleCode. Then override the JPanel's paintComponent method and use the ArrayList of Points to draw with.

    Luck!
    Last edited by Fubarable; 11-13-2010 at 09:15 PM.

  3. #3
    G4CKT is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Ah, sorry about that. I will try to figure out the solution on my own xD. Too many different classes to add

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

    Default

    The goal here is to share all problems and their solutions with everyone. So please don't delete your question but rather leave it there then post a solution when you've found it. thank you for your cooperation.

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

    Default

    Ah, okay =3. I will repost it in a sec. I think I have found a solution =3

  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 Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    Quote Originally Posted by G4CKT View Post
    Ah, okay =3. I will repost it in a sec.
    Very long sec.

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Obviously a planetary distance from the sun problem here

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

    Default

    I still have some of his code though with the statics removed, some skeleton classes added (to make it compilable) and other small adjustments...

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class SimpleCode2 {
    
       int i;
       int runNumber;
       JButton itt;
       JButton applyIndex;
       JLabel displayIndex;
       JTextField indexField;
       protected static JTextArea textArea;
       static Double[] x_screen2;
       static Double[] y_screen2;
       static Double[] x_orig;
       static Double[] y_orig;
       final static String newline = "\n";
    
       public void draws(Double[] x_screen, Double[] y_screen) {
          x_screen2 = new Double[x_screen.length];
          y_screen2 = new Double[y_screen.length];
          x_screen2 = x_screen;
          y_screen2 = y_screen;
          x_orig = new Double[x_screen.length];
          y_orig = new Double[y_screen.length];
          x_orig = x_screen;
          y_orig = y_screen;
    
          i = 0;
    
          JFrame frame = new JFrame("Polygon Display");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          MyPanel p = new MyPanel(x_screen, y_screen);
    
          ButtonListener listener = new ButtonListener();
          itt = new JButton("Simplify");
          itt.addActionListener(listener);
          itt.setAlignmentX(Component.CENTER_ALIGNMENT);
    
          JPanel subPanel1 = new JPanel();
          subPanel1.setPreferredSize(new Dimension(200, 600));
          subPanel1.setBackground(Color.white);
          JLabel label2 = new JLabel("Andrew Ma");
          subPanel1.add(label2);
          label2.setAlignmentX(Component.CENTER_ALIGNMENT);
    
          JPanel main = new JPanel();
          main.setBackground(Color.black);
    
          JPanel textPanel = new JPanel();
    
          textPanel.setMaximumSize(new Dimension(250, 40));
          textPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    
          JLabel setIndex = new JLabel("Epsilon");
          indexField = new JTextField(10);
          indexField.setSize(new Dimension(50, 20));
          textPanel.add(setIndex);
          textPanel.add(indexField);
          textArea = new JTextArea(5, 20);
          textArea.setEditable(false);
          JScrollPane scrollPane = new JScrollPane(textArea);
    
          // indexField.addActionListener(listener);
          displayIndex = new JLabel(Integer.toString(i));
    
          subPanel1.setLayout(new BoxLayout(subPanel1, BoxLayout.Y_AXIS));
          subPanel1.add(Box.createRigidArea(new Dimension(0, 175)));
          subPanel1.add(itt);
          subPanel1.add(Box.createRigidArea(new Dimension(0, 100)));
          subPanel1.add(textPanel);
          subPanel1.add(displayIndex);
    
          main.add(p);
          main.add(subPanel1);
    
          frame.getContentPane().add(main);
          frame.pack();
          frame.setVisible(true);
    
       }
    
       private class ButtonListener implements ActionListener {
          public void actionPerformed(ActionEvent event) {
    
             String text = indexField.getText();
             i = Integer.parseInt(indexField.getText());
             textArea.append(text + newline);
             indexField.selectAll();
             textArea.setCaretPosition(textArea.getDocument().getLength());
             System.out.println(i);
    
             if (i == 0) {
                draws(x_orig, y_orig);
             }
    
             else if (i > 0) {
                Point[] resultSet = new Point[x_screen2.length];
                Point[] pointList = new Point[x_screen2.length + y_screen2.length];
                resultSet = douglasPeucker.comp(x_screen2, y_screen2, i, pointList);
                Double[] x_screen = new Double[resultSet.length];
                Double[] y_screen = new Double[resultSet.length];
    
                for (int i = 0; i < resultSet.length; i++) {
                   x_screen[i] = resultSet[i].getX();
                   y_screen[i] = resultSet[i].getY();
    
                }
                draws(x_screen, y_screen);
             }
    
          }
    
       }
       
       public static void main(String[] args) {
          SimpleCode2 sc2 = new SimpleCode2();
          
          
       }
    
    }
    
    class MyPanel extends JPanel {
    
       public MyPanel(Double[] x_screen, Double[] y_screen) {
          // TODO Auto-generated constructor stub
       }
    
    }
    
    class douglasPeucker {
    
       public static Point[] comp(Double[] x_screen2, Double[] y_screen2, int i, Point[] pointList) {
          Point[] foo = {new Point(1, 1), new Point(2, 2), new Point(3, 3)};
          return foo;
       }
    
    }

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    The thread title gives no indication of what he was at though Fubarable, maybe add it to the top of the post you just made.

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

    Default

    His problem was that he desired to add points to a JPanel on button push, but instead created a new JFrame with each button push, and of course the code was only doing what he told it to.

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    That should do the trick :)

  13. #13
    G4CKT is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks everyone, didn't see all these posts but I got the entire coding working finally xD.

    Now onto my MatLab assignment T_T


    Quote Originally Posted by Darryl.Burke View Post
    Very long sec.
    Sorry about that I got a bit caught up with other errors when I finally fixed that segment haha xD
    Last edited by G4CKT; 11-14-2010 at 08:52 PM.

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

Posting Permissions

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