NEED HELP FAST please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpecialKay
    New Member
    • Mar 2008
    • 109

    NEED HELP FAST please

    i have a menu bar, when i run the code the first time everything is fine. If i click on the screen i print a Circle or a Square, store the shape in an arraylist and repaint the frame. When the frame repaints the menuBar gets painted again under the orignal.
    Any ideas?

    [code=JAVA]
    public class X2Frame extends JFrame {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu menuTool = new JMenu();
    private JMenuItem menuToolSquare = new JMenuItem();
    private JMenuItem menuToolCircle = new JMenuItem();
    private BorderLayout borderLayout1 = new BorderLayout();
    private CanvasPanel canvasPanel = new CanvasPanel();

    public X2Frame() {
    try {
    jbInit();
    } catch (Exception e) {
    e.printStackTra ce();
    }
    }

    private void jbInit() throws Exception {
    this.setJMenuBa r( menuBar );
    this.getContent Pane().setLayou t(borderLayout1 );
    this.setSize( new Dimension(400, 300) );
    menuTool.setTex t("Tool");
    menuTool.setMne monic('T');
    menuToolSquare. setText("Square ");
    menuToolSquare. setMnemonic('S' );
    menuToolSquare. addActionListen er( new ActionListener( ) { public void actionPerformed ( ActionEvent e ) {
    menuToolSquare_ ActionPerformed (e);
    } } );
    menuToolCircle. setText("Circle ");
    menuToolCircle. setMnemonic('C' );
    menuToolCircle. addActionListen er(new ActionListener( ) {
    public void actionPerformed (ActionEvent e) {
    menuToolCircle_ actionPerformed (e);
    }
    });
    menuTool.add( menuToolSquare );
    menuTool.add(me nuToolCircle);
    menuBar.add(men uTool);
    this.getContent Pane().add(canv asPanel, BorderLayout.CE NTER);
    }
    [/code]
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Ahhh!!! so you are using JDeveloper ...am i right?
    What jdk did you use?
    it is recommended that you use jdk 5.0.... try it again, and update us....

    sukatoa

    Comment

    • SpecialKay
      New Member
      • Mar 2008
      • 109

      #3
      Whats a JDK, and how do i change it?

      Comment

      • SpecialKay
        New Member
        • Mar 2008
        • 109

        #4
        Update to problem:
        Not only is the menubar being repainted, but if i draw a square, then change tools and draw something else, the first square and the menubar gets doubled directly under the first. If i resize the window it goes back to normal. Paint again and it doubles again.

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          Originally posted by SpecialKay
          Whats a JDK, and how do i change it?
          You can download it at Sun Microsystems... .

          jdk(Java development kit) 5.0.... i can't remember the procedure... that was last 3 month's ago when i tried to use it.... but seems, the code generator fail sometimes....
          I prefer to use netbeans if making GUI in a hurry.....

          So, i came back to JCreator Lite....


          I like to test your code, can you post the whole code?

          sukatoa...

          Comment

          • SpecialKay
            New Member
            • Mar 2008
            • 109

            #6
            its split up in 6 classes,
            not sure that posting it is the easiest way.

            [code=java]
            import java.awt.Graphi cs2D;
            import java.awt.geom.E llipse2D;

            public class Circle extends Shape {

            public Circle(int x, int y) {
            super.setX(x);
            super.setY(y);
            }
            public void draw( Graphics2D g2 )
            {
            int CircleX = super.getX() - (super.SHAPE_WI DTH/2);
            int CircleY = super.getY() - (super.SHAPE_WI DTH/2);
            //Problem Area
            Ellipse2D.Doubl e circle = new Ellipse2D.Doubl e(CircleX,Circl eY,super.SHAPE_ WIDTH,super.SHA PE_WIDTH);
            g2.draw(circle) ;
            }
            }

            import java.awt.Border Layout;
            import java.awt.Dimens ion;
            import java.awt.event. ActionEvent;
            import java.awt.event. ActionListener;

            import javax.swing.JFr ame;
            import javax.swing.JMe nu;
            import javax.swing.JMe nuBar;
            import javax.swing.JMe nuItem;

            public class X2Frame extends JFrame {
            private JMenuBar menuBar = new JMenuBar();
            private JMenu menuTool = new JMenu();
            private JMenuItem menuToolSquare = new JMenuItem();
            private JMenuItem menuToolCircle = new JMenuItem();
            private BorderLayout borderLayout1 = new BorderLayout();
            private CanvasPanel canvasPanel = new CanvasPanel();

            public X2Frame() {
            try {
            jbInit();
            } catch (Exception e) {
            e.printStackTra ce();
            }
            }

            private void jbInit() throws Exception {
            this.setJMenuBa r( menuBar );
            this.getContent Pane().setLayou t(borderLayout1 );
            this.setSize( new Dimension(400, 300) );
            this.setTitle( "Kevin Cameron" );
            menuTool.setTex t("Tool");
            menuTool.setMne monic('T');
            menuToolSquare. setText("Square ");
            menuToolSquare. setMnemonic('S' );
            menuToolSquare. addActionListen er( new ActionListener( ) { public void actionPerformed ( ActionEvent e ) {
            menuToolSquare_ ActionPerformed (e);
            } } );
            menuToolCircle. setText("Circle ");
            menuToolCircle. setMnemonic('C' );
            menuToolCircle. addActionListen er(new ActionListener( ) {
            public void actionPerformed (ActionEvent e) {
            menuToolCircle_ actionPerformed (e);
            }
            });
            menuTool.add( menuToolSquare );
            menuTool.add(me nuToolCircle);
            menuBar.add(men uTool);
            this.getContent Pane().add(canv asPanel, BorderLayout.CE NTER);
            }

            private void menuToolSquare_ ActionPerformed (ActionEvent e) {
            canvasPanel.set Circle(false);
            }

            private void menuToolCircle_ actionPerformed (ActionEvent e) {
            canvasPanel.set Circle(true);
            }
            }


            import java.awt.Graphi cs2D;

            public class Square extends Shape {

            public Square(int x, int y) {
            super.setX(x);
            super.setY(y);
            }

            public void draw( Graphics2D g2 )
            {
            g2.drawRect(sup er.getX(), super.getY(), super.SHAPE_WID TH, super.SHAPE_WID TH);
            }
            }

            [/code]

            Comment

            • SpecialKay
              New Member
              • Mar 2008
              • 109

              #7
              [code=java]
              import java.awt.Graphi cs2D;

              public abstract class Shape {
              static int SHAPE_WIDTH = 20;
              int x;
              int y;

              abstract public void draw( Graphics2D g2 );

              public Shape() {
              }

              public void setX(int x) { this.x = x; }
              public int getX() { return x; }
              public void setY(int y) { this.y = y; }
              public int getY() { return y; }
              }


              import java.awt.Graphi cs2D;
              import java.awt.geom.E llipse2D;

              public class Circle extends Shape {

              public Circle(int x, int y) {
              super.setX(x);
              super.setY(y);
              }
              public void draw( Graphics2D g2 )
              {
              int CircleX = super.getX() - (super.SHAPE_WI DTH/2);
              int CircleY = super.getY() - (super.SHAPE_WI DTH/2);
              //Problem Area
              Ellipse2D.Doubl e circle = new Ellipse2D.Doubl e(CircleX,Circl eY,super.SHAPE_ WIDTH,super.SHA PE_WIDTH);
              g2.draw(circle) ;
              }
              }


              import java.awt.Graphi cs;
              import java.awt.Graphi cs2D;
              import java.awt.event. MouseAdapter;

              import java.awt.event. MouseEvent;

              import java.util.Array List;

              import javax.swing.JPa nel;

              public class CanvasPanel extends JPanel {

              private ArrayList shapeList = new ArrayList();
              private boolean circle = false;

              public CanvasPanel() {
              try {
              jbInit();
              } catch (Exception e) {
              e.printStackTra ce();
              }
              }

              public void setCircle(boole an circle) {
              this.circle = circle;
              }

              private void jbInit() throws Exception {
              this.addMouseLi stener(new CanvasPanel_thi s_mouseAdapter( this));
              }

              void this_mouseClick ed(MouseEvent e) {
              if(circle == false) {
              Square square = new Square(e.getX() , e.getY());
              shapeList.add(s quare);
              }
              else {
              Circle circle = new Circle(e.getX() , e.getY());
              shapeList.add(c ircle);
              }
              repaint();
              }
              public void paintComponent ( Graphics g) {
              for ( int i = 0; i < shapeList.size( ); i++)
              {
              Graphics2D g2 = (Graphics2D)g;
              Shape shape = (Shape)shapeLis t.get(i);
              shape.draw(g2);
              }
              }

              }
              final class CanvasPanel_thi s_mouseAdapter extends MouseAdapter {
              private CanvasPanel adaptee;

              CanvasPanel_thi s_mouseAdapter( CanvasPanel adaptee) {
              this.adaptee = adaptee;
              }

              public void mouseClicked(Mo useEvent e) {
              adaptee.this_mo useClicked(e);
              }
              }

              [/code]

              Comment

              • sukatoa
                Contributor
                • Nov 2007
                • 539

                #8
                store the shape in an arraylist and repaint the frame. When the frame repaints the menuBar gets painted again under the orignal.
                Where is it? sorry if i can't find it....

                Is this your complete code? only to choose shape then click to show those shapes? what about the quotes above?

                I've test your code, no problem.... but i have doubt the quotes above.....
                can you please add more details?

                You didn't even set the close operation of your frame.... if you are using Windows, have a look at your task manager... many java process might still running.... they consumes your temp memory..... maybe anytime your system will crash....

                sukatoa

                Comment

                • SpecialKay
                  New Member
                  • Mar 2008
                  • 109

                  #9
                  Nope nothing is running, yes that is all my code.
                  It is suspost to be 6 classes. All the code does is allow you to chose a tool (Square, Circle) then paint that shape on the frame. It is all working fine except the menubar, and the shapes are being doubled under the orignal. If i re-size the window the doubles disapear, untill i draw another shap, then everything on the frame is being doubled.

                  Comment

                  • SpecialKay
                    New Member
                    • Mar 2008
                    • 109

                    #10
                    i have set the exit
                    frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );

                    Comment

                    • sukatoa
                      Contributor
                      • Nov 2007
                      • 539

                      #11
                      Originally posted by SpecialKay
                      Nope nothing is running, yes that is all my code.
                      It is suspost to be 6 classes. All the code does is allow you to chose a tool (Square, Circle) then paint that shape on the frame. It is all working fine except the menubar, and the shapes are being doubled under the orignal. If i re-size the window the doubles disapear, untill i draw another shap, then everything on the frame is being doubled.
                      It is all working fine except the menubar, and the shapes are being doubled under the orignal
                      I don't understand... what's going on in your menubar? doubled? Sorry if im too logical about this... just to make sure.

                      At this moment, when i choose square, clicked, no doubles anywhere just a one square per click... and also in circle....

                      No problem... also when i try to resize, nothings wrong.....

                      I've integrate your codes... i've done nothing.... try this.

                      Code:
                      import java.awt.Graphics2D;
                      import java.awt.Graphics;
                      import java.awt.Graphics2D;
                      import java.awt.event.MouseAdapter;
                       
                      import java.awt.event.MouseEvent;
                       
                      import java.util.ArrayList;
                       
                      import javax.swing.JPanel;
                      
                      import java.awt.geom.Ellipse2D;
                      import java.awt.Graphics2D;
                       
                      import java.awt.BorderLayout;
                      import java.awt.Dimension;
                      import java.awt.event.ActionEvent;
                      import java.awt.event.ActionListener;
                       
                      import javax.swing.JFrame;
                      import javax.swing.JMenu;
                      import javax.swing.JMenuBar;
                      import javax.swing.JMenuItem;
                      
                       
                      abstract class Shape {
                         static int SHAPE_WIDTH = 20;
                         int x;
                         int y;
                         
                         abstract public void draw( Graphics2D g2 );
                         
                         public Shape() {
                         }
                       
                         public void setX(int x) { this.x = x; }
                         public int getX() { return x; }
                         public void setY(int y) { this.y = y; }
                         public int getY() { return y; }
                      }
                      
                       
                      class Circle extends Shape {
                       
                         public Circle(int x, int y) {
                            super.setX(x);
                            super.setY(y);
                         }
                         public void draw( Graphics2D g2 )
                         {
                            int CircleX = super.getX() - (super.SHAPE_WIDTH/2);
                            int CircleY = super.getY() - (super.SHAPE_WIDTH/2);
                            //Problem Area
                            Ellipse2D.Double circle = new Ellipse2D.Double(CircleX,CircleY,super.SHAPE_WIDTH  ,super.SHAPE_WIDTH);
                            g2.draw(circle);
                         }
                      }
                      
                       
                      public class X2Frame extends JFrame {
                         private JMenuBar menuBar = new JMenuBar();
                         private JMenu menuTool = new JMenu();
                         private JMenuItem menuToolSquare = new JMenuItem();
                         private JMenuItem menuToolCircle = new JMenuItem();
                         private BorderLayout borderLayout1 = new BorderLayout();
                         private CanvasPanel canvasPanel = new CanvasPanel();
                       
                         public X2Frame() {
                            try {
                               jbInit();
                            } catch (Exception e) {
                               e.printStackTrace();
                            }
                         }
                       
                         private void jbInit() throws Exception {
                            this.setJMenuBar( menuBar );
                            this.getContentPane().setLayout(borderLayout1);
                            this.setSize( new Dimension(400, 300) );
                            this.setTitle( "Kevin Cameron" );
                            menuTool.setText("Tool");
                            menuTool.setMnemonic('T');
                            menuToolSquare.setText("Square");
                            menuToolSquare.setMnemonic('S');
                            menuToolSquare.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {
                                        menuToolSquare_ActionPerformed(e);
                                     } } );
                            menuToolCircle.setText("Circle");
                            menuToolCircle.setMnemonic('C');
                            menuToolCircle.addActionListener(new ActionListener() {
                                     public void actionPerformed(ActionEvent e) {
                                        menuToolCircle_actionPerformed(e);
                                     }
                                  });
                            menuTool.add( menuToolSquare );
                            menuTool.add(menuToolCircle);
                            menuBar.add(menuTool);
                            this.getContentPane().add(canvasPanel, BorderLayout.CENTER);
                         }
                       
                         private void menuToolSquare_ActionPerformed(ActionEvent e) {
                            canvasPanel.setCircle(false);
                         }
                       
                         private void menuToolCircle_actionPerformed(ActionEvent e) {
                            canvasPanel.setCircle(true);
                         }
                      
                      	public static void main(String args[]){
                      		new X2Frame().setVisible(true);
                      	}
                      }
                       
                      class Square extends Shape {
                         
                         public Square(int x, int y) {
                            super.setX(x);
                            super.setY(y);
                         }
                         
                         public void draw( Graphics2D g2 )
                         {
                               g2.drawRect(super.getX(), super.getY(), super.SHAPE_WIDTH, super.SHAPE_WIDTH);
                         }
                      }
                       
                      
                      class CanvasPanel extends JPanel {
                       
                         private ArrayList shapeList = new ArrayList();
                         private boolean circle = false;
                         
                         public CanvasPanel() {
                            try {
                               jbInit();
                            } catch (Exception e) {
                               e.printStackTrace();
                            }
                         }
                       
                         public void setCircle(boolean circle) {
                            this.circle = circle;
                         }
                       
                         private void jbInit() throws Exception {
                            this.addMouseListener(new CanvasPanel_this_mouseAdapter(this));
                         }
                       
                         void this_mouseClicked(MouseEvent e) {
                            if(circle == false) {
                               Square square = new Square(e.getX(), e.getY());
                               shapeList.add(square);
                            }
                            else {
                               Circle circle = new Circle(e.getX(), e.getY());
                               shapeList.add(circle);
                            }
                         repaint();
                         }
                         public void paintComponent ( Graphics g) {
                            for ( int i = 0; i < shapeList.size(); i++)
                            {
                               Graphics2D g2 = (Graphics2D)g;
                               Shape shape = (Shape)shapeList.get(i);
                               shape.draw(g2);
                            }
                         }
                       
                      }
                      final class CanvasPanel_this_mouseAdapter extends MouseAdapter {
                         private CanvasPanel adaptee;
                       
                         CanvasPanel_this_mouseAdapter(CanvasPanel adaptee) {
                            this.adaptee = adaptee;
                         }
                       
                         public void mouseClicked(MouseEvent e) {
                            adaptee.this_mouseClicked(e);
                         }
                      }
                      test it on a file only....

                      im using JDK 1.6.03

                      Comment

                      • sukatoa
                        Contributor
                        • Nov 2007
                        • 539

                        #12
                        Originally posted by SpecialKay
                        i have set the exit
                        frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );
                        Ok, that's good....

                        Update us,
                        sukatoa

                        Comment

                        • SpecialKay
                          New Member
                          • Mar 2008
                          • 109

                          #13
                          You have not changed anything in your integration that would help with the problem that i am having.
                          Thank you for your time tho, i dont think im going to solve this one, as it makes no sence to me why it is happening.

                          Comment

                          • sukatoa
                            Contributor
                            • Nov 2007
                            • 539

                            #14
                            Originally posted by SpecialKay
                            You have not changed anything in your integration that would help with the problem that i am having.
                            Thank you for your time tho, i dont think im going to solve this one, as it makes no sence to me why it is happening.
                            Why i should change your code if your code doesn't do wrong....

                            You like the proof? images?

                            Update us,
                            sukatoa

                            Comment

                            • SpecialKay
                              New Member
                              • Mar 2008
                              • 109

                              #15
                              No No i believe you.
                              I have no idea what is wrong with mine.

                              Comment

                              Working...