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 30

Thread: Help with checking to see which menuItem has been selected

  1. #1
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Help with checking to see which menuItem has been selected

    Hey, I've uploaded my program, so that you can test it, seeing as pasting large chunks of code wont let you see what my problem is.

    Link: http://www.filedropper.com/calculations_1

    In MyControlPanel class I have this line:

    DrawShape drawShape = new DrawShape(20,20,shapeSize,theShape);

    Which is used to send the coords 'x' and 'y', 'shapeSize' and 'theShape' to the DrawShape class

    I then have a paintComponent which is used to draw one of the three shapes depending on which is chosen from the menu, for example if the Square is chosen, a square will be drawn, using the coords x and y(default 20,20) and also the shapeSize(taken from the sliders value)

    but, before it draws a shape it needs to recognize which shape has been chosen, seeing as I have passed in myShape, and assigned it to theShape, I should be able to, some how, compare 'theShape' to 'Circle' and if it matches it will drawn a circle, etc.

    I tried using:

    if(theShape == Square)

    but then found that i cant just simply do that.

    Can anyone help me ? how do i check to see which shape it is that has been chosen.

    It would be much easier if you downloaded my program, if possible.

    I will post all the code if needed.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with checking to see which menuItem has been selected

    Did you consider using instanceof?

    But I think I've given you this advice before: Each shape should know how to draw itself, so simply specifying thisShape.draw() with the necessary parameters, including the Graphics instance the shape will be drawn on, should do what needs to be done.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    So in inside each of my shape classes(circle, square and triangle), i should add a paint component ?

    for example,
    public void paintComponent(Graphics g)
                {
                    Graphics2D g2 = (Graphics2D) g;
     
                    g2.fillRect( x , y , length, length);
                    g2.setColor(Color.BLACK);
                }

    Edit:
    instanceof didnt work, no shape appeared when i selected one

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with checking to see which menuItem has been selected

    No. You pass the Graphics object to the shape's draw method from inside the paintComponent() method of the component on which the drawing is being done, and the shape draws itself on that object.

    I don't have time to write an example for you but will later if you'd like.

  5. #5
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    could you please write an example later for me, I'm completely lost! I'm making this program messier and messier! I've got bits of everything everywhere. Are you able to download my program? If so could you have a look at it and give me a clue to what needs fixed, if not an example of the painting will be great!

    thanks.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with checking to see which menuItem has been selected

    Will do. Sometime today, but I can't promise when. When you make a statement like:
    instanceof didnt work, no shape appeared when i selected one.
    include code, even a line or two, to show what you tried. You may have done it completely wrong, in the wrong place, or . . . We can't help much with "I changed it and it didn't work."

  7. #7
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    Well I'm kind of desperate to get this finished today!

    Sorry, I'm just giving up all hope, I should be able to figure this out, but I can't.

    well i tried this:

    package calculations;
     
    import java.awt.*;
     
    import javax.swing.JPanel;
     
    class DrawShape extends JPanel
    {
         private int x = 50, y = 10; 
         int length = 50;
     
         private MyShape theShape;//selected shape
     
         public DrawShape(int newX, int newY, int newLength, MyShape myShape)
         {
             x = newX;
             y = newY;
             length = newLength;
             theShape = myShape;
     
             setPreferredSize(new Dimension(800, 650));
     
             setBackground(Color.WHITE);
     
             repaint() ;
         }
     
         //used to get the selected shape from
         public void setShape(MyShape suppliedShape)
         {       
             theShape = suppliedShape;
         }
     
         @Override
                public void paintComponent(Graphics g)
                {
                    Graphics2D g2 = (Graphics2D) g;
     
                    super.paintComponent(g2);
     
                    BasicStroke pen = new BasicStroke(4F);
                    g2.setStroke(pen);
     
                    if(theShape instanceof Square);
                    {
                    //Square
                        g2.fillRect( x , y , length, length);
                        g2.setColor(Color.BLACK);
                    }
                    if(theShape instanceof Circle)
                    {
                    //Circle
                        g2.fillOval(x+100 , y , length, length);
                        g2.setColor(Color.BLUE);
                    }
                    if(theShape instanceof Triangle)
                    {
                    //Triangle
                        int xCoords[] = {x+200,x+200,x+260};
                        int yCoords[] = {y,y+50,y+50};
     
                        g2.fillPolygon(xCoords, yCoords, 3);
                        g2.setColor(Color.RED);
                    }
                }
     }

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with checking to see which menuItem has been selected

    What does your posted code do? What do you expect it to do? This would all be so much easier to help you with if you started with an SSCCE instead of chunks of your whole program.

    One thing that stands out to me is this line, which will do nothing: if(theShape instanceof Square);

    Notice the semicolon at the end of the line. That's like saying "if the shape is a square, don't do anything". And then the block that follows inside { } happens regardless of that if statement. That probably isn't what you want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    The code does not draw any shapes, just produces a white box in my program(where the shape is meant to be drawn):
    wefwef.jpg

    That code is meant to, when i select a shape from the menu, say i picked Square, it would draw a square, with width/height = to the sliders value. Meaning when i increase the slider value, the shape grows in size.

    That semicolon was there by accident, even though it was there though, it didn't do what was in the {}.

    EDIT: Oh yeah, I removed the semicolon and tried it, still nothing.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with checking to see which menuItem has been selected

    Did the results change when the semicolon was removed?

  11. #11
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    no nothing changed, still no shape being drawn

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with checking to see which menuItem has been selected

    When do you add the DrawShape to the JFrame? When do you call the setShape() method?

    I recommend hardcoding a shape and putting it in a basic JFrame by itself and seeing if that works. Test your code in isolation instead of working on the whole project. This is the idea of an SSCCE.

    Also, after you set the shape, you have to tell the JPanel to redraw itself. The repaint() method is your friend.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    I add the DrawShape within MyControlPanel constructor, is it called a constructor?

        public MyControlPanel() 
        {
            initComponents();
     
            //JSlider setup
            jSlider = new JSlider(JSlider.HORIZONTAL, min, max, initial);
            jSlider.setMajorTickSpacing(10);
            jSlider.setMinorTickSpacing(1);
            jSlider.setPaintLabels(true);
            jSlider.setPaintTicks(true);
            jSlider.setValue(50);
     
            jSlider.addChangeListener(new MyChangeAction());
     
            //create instance of jTexts          
            jText1 = new JTextField();
            jText2 = new JTextField();
     
            //setText for jTexts
            jText1.setText("          ");
            jText2.setText("          ");
     
            //create instance of labels
            jLabel1 = new JLabel();
            jLabel2 = new JLabel();
            jLabel3 = new JLabel();
            jLabel4 = new JLabel();
     
            //setText for jLabels
            jLabel1.setText("Shape Dimension = ");
            jLabel2.setText("     Boundary Length = ");
            jLabel3.setText("     Area = ");
            jLabel4.setText("");
     
            jPanelPaint= new JPanel();
            jPanelSlider = new JPanel();
     
            DrawShape drawShape = new DrawShape(20,20, shapeSize, theShape);
     
            //adding components
            this.add(drawShape);
            this.add(jSlider);
            this.add(jLabel1);
            this.add(jLabel4);
            this.add(jLabel2);
            this.add(jText1);
            this.add(jLabel3);
            this.add(jText2);
     
            setLayout(new FlowLayout());
        }

    I call the setShape() method within each of the three actionListeners for the menuItems within the MyFrame class, allowing me to tell which shape has been selected.

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with checking to see which menuItem has been selected

    Your DrawShape instance is inside your MyControlPanel constructor. How are any of the listeners accessing that instance to pass in the selected shape or size?

    Again, I highly suggest taking this apart and working on one small piece at a time. Can you get a JPanel that simply draws a square? Can you get a JPanel that draws either a square or a circle based on a hardcoded value? Can you change that value from a listener in another class? Start small and work your way up instead of starting with the whole thing and then debugging the individual pieces. And not to beat a dead horse, but this process is why we ask for an SSCCE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    I have this method within the MyControlPanel class:
    public void setShape(MyShape suppliedShape)
        {       
            theShape = suppliedShape;
        }

    The actionListeners look like this, within the MyFrame class:
    public class MySquareAction implements ActionListener
        {
        @Override
            public void actionPerformed(ActionEvent e)
            {
                myShape = new Square();//sets myShape as an instance of Square
                controlPanel.setShape(myShape);//sends myShape to MyControlPanel
            }  
        }

    I get the size from the slider changeListener, which is in the MyControlPanel class:
    Slider changeListener
    public class MyChangeAction implements ChangeListener
            {
                @Override
                public void stateChanged(ChangeEvent e) 
                {                
                    int sliderValue = jSlider.getValue();
                    shapeSize = sliderValue;
                    double area;
                    double boundaryLength;
     
                    String str = Double.toString(sliderValue);
                    jLabel4.setText(str);
     
                    area = theShape.getArea(sliderValue);
                    boundaryLength = theShape.getBoundaryLength(sliderValue);
     
                    jText1.setText(Double.toString(Math.round(boundaryLength)));
                    jText2.setText(Double.toString(Math.round(area))); 
                    repaint();
                }            
            }

    before i tried passing in the shape and value, i hardcoded the size, and used only the code to draw a square, this worked, i could increase the hardcoded value, run the program, and the shape would be drawn bigger. Then i attempted to pass in the values with what i have now, and it isn't working.

  16. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with checking to see which menuItem has been selected

    I see that you're setting the value of theShape in your MyControlPanel class, but that's a completely different variable than theShape in your DrawShape class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #17
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    this is what happens when i force the shape to be a square, then select square and move the slider:
    sderh.jpg

    how do i get them to be the same variable ?

    EDIT: yeah, when i use a 'System.out.println("shape = "+myShape);' in the DrawShape constructor it comes back as null, do you know how i can fix this ?

  18. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with checking to see which menuItem has been selected

    You need to set the value of theShape in DrawShape whenever the user changes it. Java doesn't automagically update variables for you. For example:

    String s = "one";
    String t = s;
    s = "two";
    System.out.println(t);

    What do you think this prints out? Your code seems to be working off the assumption that it would print "two". But it won't.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    I understand what you mean, but how do I fix my problem ? I cant figure it out

  20. #20
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with checking to see which menuItem has been selected

    You already set the shape value in MyControlPanel, why can't you do something similar with the DrawShape class?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  21. #21
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    do you mean the setShape() method which i used?

    I don't understand how i would do it with the DrawShape class, because it requires three int values when creating an instance of it:

    public DrawShape(int newX, int newY, int newLength)

    so i would have to do this within the MyFrame class:

    DrawShape drawShape = new DrawShape(int,int,int);

    though what values would i use for the int's ?

  22. #22
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with checking to see which menuItem has been selected

    You don't pass a Shape value into the MyControlPanel constructor, do you? And you call a setShape() function on the MyControlPanel class, right? So why not do something similar with your DrawShape class?

    Don't pass any shape into the constructor, and call a setter when the user makes a selection.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  23. #23
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    how can i call setShape() function on drawShape? seeing as i cant create an instance of DrawShape because it requires 3 int values ??

    DrawShape drawShape = new DrawShape(int,int,int);

    ertherth.jpg

  24. #24
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    right, so in the DrawShape class if I run:

    public DrawShape(int newX, int newY, int newLength, MyShape shape)
         {
             x = newX;
             y = newY;
             length = newLength;
             theShape = shape;
             System.out.println("shape = "+theShape);
     
             setPreferredSize(new Dimension(800, 650));
     
             setBackground(Color.WHITE);
     
             repaint() ;
         }

    the sout, returns the correct shape when i click on a shape and then move the slider, plus it draws the correct shape, my only problem is that it does this:
    multiple paints.jpg
    It paints god knows how many white boxes, with the shape drawn in, and the slider/textboxes and labels disappear, this is probably to do with my layout, can you tell me what would do this ? or what the best way to lay out this would be?

  25. #25
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    Thanked 5 Times in 1 Post

    Default Re: Help with checking to see which menuItem has been selected

    I found what the problem is, it is because i am calling add(drawShape); within my jSlider stateChanged.

    But it needs to be called within the stateChanged or else it will not work, is there a way to remove a object from the screen? i tried remove(drawShape); at the start so that it would remove it, then add it at the bottom again, but that didnt work.

    any help with this ?

Page 1 of 2 12 LastLast

Similar Threads

  1. Getting Selected Text From Combo Box
    By tommyacton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 6th, 2013, 07:14 PM
  2. Trying to out.println of random selected record
    By Walters in forum What's Wrong With My Code?
    Replies: 17
    Last Post: November 13th, 2012, 03:44 PM
  3. Getting the Selected Value in JComboBox
    By aStudentofJava in forum AWT / Java Swing
    Replies: 2
    Last Post: March 9th, 2012, 10:35 AM
  4. How to Delete selected table data from DB???? HELP
    By lanepulcini in forum JDBC & Databases
    Replies: 0
    Last Post: February 21st, 2012, 07:07 PM
  5. Help with Java MenuItem and Boolean
    By Just Incredible in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 8th, 2010, 03:15 PM