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 2 of 2 FirstFirst 12
Results 26 to 30 of 30

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

  1. #26
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

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

    Here's a simple demo to show how shapes can draw themselves on a Graphics object based on the shape chosen with a button and the value of a slider, though I'm not sure you care any more. I can't tell what you're working on now. I think the paintComponent() method of the DrawPanel class outlines the concept the best, though there are important details in how it works with the other classes. Sorry that it was a long day, but this was a pleasant diversion before bed.

    Good luck.

    Too late or not, here it is:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JSlider;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
    // a class to demonstrate how shapes can draw themselves
    public class DrawDemo
    {
        JFrame demoFrame;
        JPanel controlPanel;
        DrawPanel drawPanel;
        JSlider shapeSlider;
        JRadioButton squareButton;
        JRadioButton circleButton;
        ButtonGroup buttonGroup;
        Shape currentShape;
     
        // a default constructor
        public DrawDemo()
        {
            // initialize the instance variables
            controlPanel = new JPanel();
            controlPanel.setPreferredSize( new Dimension( 300, 50 ) );
     
            drawPanel = new DrawPanel();
            drawPanel.setPreferredSize( new Dimension( 300, 250 ) );
     
            shapeSlider = new JSlider( JSlider.HORIZONTAL, 5, 150, 25 );
            shapeSlider.addChangeListener( new SliderListener() );
     
            squareButton = new JRadioButton( "Square" );
            squareButton.addActionListener( new ButtonListener() );
     
            circleButton = new JRadioButton( "Circle" );
            circleButton.setSelected( true );
            circleButton.addActionListener( new ButtonListener() );
     
            buttonGroup = new ButtonGroup();
            buttonGroup.add( squareButton );
            buttonGroup.add( circleButton );
     
            // build the control panel
            controlPanel.add( squareButton );
            controlPanel.add( circleButton );
            controlPanel.add( shapeSlider );
     
            // initialize the frame to display the demo
            demoFrame = new JFrame( "Demo Frame" );
            demoFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            demoFrame.setLocationRelativeTo( null );
     
            // build the frame
            demoFrame.add( controlPanel, BorderLayout.NORTH );
            demoFrame.add( drawPanel, BorderLayout.CENTER );
     
            // pack the frame and display it
            demoFrame.pack();
            demoFrame.setVisible( true );
     
        } // end default constructor
     
        // a JPanel on which to draw - this is where the magic is
        private final class DrawPanel extends JPanel
        {
            // a method to paint or draw on the draw panel 
            @Override
            public void paintComponent( Graphics g )
            {
                // clear the pallette each time the method is called
                super.paintComponent( g );
     
                // determine if a square or circle has been selected
                if ( squareButton.isSelected() )
                {
                    g.setColor( Color.RED );
                    currentShape = new Square(  80, 50, shapeSlider.getValue() );
                }
                else
                {
                    g.setColor( Color.BLUE );
                    currentShape = new Circle( 80, 50, shapeSlider.getValue() );
                }
     
                // draw the correct shape
                currentShape.draw( g );
     
            } // end method paintComponent()
     
        } // end class DrawPanel
     
        // to listen for changes to the slider and act on them
        private final class SliderListener implements ChangeListener
        {
            @Override
            public void stateChanged( ChangeEvent e )
            {
                // repaint the drawing
                drawPanel.repaint();
            }
     
        } // end class SliderListener
     
        // to listen to the buttons and act on them
        private final class ButtonListener implements ActionListener
        {
     
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // redraw when the button values change
                drawPanel.repaint();
     
            } // end method actionPerformed()
     
     
        } // end class Button Listener
     
     
        // a main() method to launch the app on the EDT
        public static void main( String[] args )
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                @Override
                public void run()
                {
                    new DrawDemo();
     
                } // end method run()
     
            } );
     
        } // end method main()
     
     
    } // end class DrawDemo
     
    // an abstract class to define the basic properties of all shapes and
    // specify which methods implementations must have
    abstract class Shape
    {
        int x;
        int y;
        int width;
     
        // an abstract method to define how shapes draw() themselves
        public abstract void draw( Graphics g );
     
        // a mutator to set x
        public void setX( int x )
        {
            this.x = x;
     
        } // end method setX() 
     
        // a mutator to set y
        public void setY( int y )
        {
            this.y = y;
     
        } // end method setY() 
     
    } // end class Shape
     
     
    // a simple class that defines and draws a circle
    class Circle extends Shape
    {
     
        // a constructor that specifies x, y, and the diameter as width
        public Circle( int x, int y, int width )
        {
            this.x = x;
            this.y = y;
            this.width = width;
     
        } // end constructor
     
        // the draw method as required by the abstract Shape class
        public void draw( Graphics g )
        {
            g.fillOval( x, y, width, width );
     
        } // end method draw()
     
    } // end class Circle
     
    //a simple class that defines and draws a square
    class Square extends Shape
    {
     
        // a constructor that specifies x, y, and the diameter as width
        public Square( int x, int y, int width )
        {
            this.x = x;
            this.y = y;
            this.width = width;
     
        } // end constructor
     
        // the draw method as required by the abstract Shape class
        public void draw( Graphics g )
        {
            g.fillRect( x, y, width, width );
     
        } // end method draw()
     
    } // end class Square

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

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

    The above example sorta "cheats" by throwing it all in a single class and using inner classes. I think the OP is going for a solution using multiple classes. I'm not going to spoonfeed him the answer, but I'm trying to coax him in the right direction.

    OP- you're still trying to work out of your constructor, and I'm not sure why. Use a setter, not a constructor.
    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!

  3. #28
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

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

    Point taken, but the construction is for convenience in posting and could be broken apart as needed. I think the demo achieves its purpose which is to show how Shape/Circle/Square (all separate classes) could be constructed so that the child shapes could draw themselves on the Graphics object of a JPanel rather than constructing the shapes in the JPanel's paintComponent() method which then draws the shape. The former approach makes the existence of the shape classes irrelevant.

    I think the OP has moved onto new problems, anyway.

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

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

    I agree that your example works great and accomplishes the goal. The problem the OP has is with the process of updating values of variables between classes, and using inner classes that all refer to a single variable confuses that a bit, imho. I doubt he's learned about inner classes yet, so I hope he doesn't copy-paste your solution and hand it in, as that will be a red flag to the instructor!

    Anyway no harm no foul, I just wanted OP to realize that your approach is a lot different than separating them all into their own classes, which I think is what he's supposed to be doing.
    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!

  5. #30
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

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

    Okay. For completeness, I modified the demo to include two objectives: show how a shape object can draw itself on a Graphics object passed to it, AND how the variables in one class can be updated by another using mutator (or setter) methods.

    For the second objective, the DrawPanel class was separated from the main class, a Shape field, currentClass, was added to DrawPanel, the setCurrentShape() method was added to the DrawPanel class, and the setCurrentShape() method is called by the main class when either the buttons or the slider cause the current shape to change. I also simplified the drawing panel's paintComponent() method that originally had code in it that belonged in other places. Sorry if I caused any confusion.
    // a class to demonstrate shapes drawing themselves
    // GregBrannon, Dec 5, 2013, ver 0.1 Dec 6, 2013
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JSlider;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
    // a class to demonstrate how shapes can draw themselves
    public class DrawDemo
    {
        private JFrame demoFrame;
        private JPanel controlPanel;
        private DrawPanel drawPanel;
        private JSlider shapeSlider;
        private JRadioButton squareButton;
        private JRadioButton circleButton;
        private ButtonGroup buttonGroup;
        private Shape currentShape;
     
        // a default constructor
        public DrawDemo()
        {
            // initialize the instance variables
            controlPanel = new JPanel();
            controlPanel.setPreferredSize( new Dimension( 300, 50 ) );
            drawPanel = new DrawPanel();
     
            // create the slider initially set at 25
            shapeSlider = new JSlider( JSlider.HORIZONTAL, 5, 150, 25 );
            shapeSlider.addChangeListener( new SliderListener() );
            squareButton = new JRadioButton( "Square" );
            squareButton.addActionListener( new ButtonListener() );
     
            // make the circle button selected at start
            circleButton = new JRadioButton( "Circle" );
            circleButton.setSelected( true );
            circleButton.addActionListener( new ButtonListener() );
     
            // add the buttons to a button group
            buttonGroup = new ButtonGroup();
            buttonGroup.add( squareButton );
            buttonGroup.add( circleButton );
     
            // build the control panel
            controlPanel.add( squareButton );
            controlPanel.add( circleButton );
            controlPanel.add( shapeSlider );
     
            // initialize the frame to display the demo
            demoFrame = new JFrame( "Demo Frame" );
            demoFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            demoFrame.setLocationRelativeTo( null );
     
            // initialize the current shape with width 25 and send the
            // object to the drawing class
            currentShape = new Circle( );
            currentShape.setWidth( 25 );
            drawPanel.setCurrentShape( currentShape );
     
            // build the frame
            demoFrame.add( controlPanel, BorderLayout.NORTH );
            demoFrame.add( drawPanel, BorderLayout.CENTER );
     
            // pack the frame and display it
            demoFrame.pack();
            demoFrame.setVisible( true );
     
        } // end default constructor
     
        // to listen for changes to the slider and act on them
        private final class SliderListener implements ChangeListener
        {
            @Override
            public void stateChanged( ChangeEvent e )
            {
                // get the current slider value and set the width of the
                // displayed shape to that value
                currentShape.setWidth( shapeSlider.getValue() );
     
                // update the current shape in the class being drawn
                drawPanel.setCurrentShape( currentShape );
     
                // repaint the drawing
                drawPanel.repaint();
            }
     
        } // end class SliderListener
     
        // to listen to the buttons and act on them
        private final class ButtonListener implements ActionListener
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                // determine if a square or circle has been selected and
                // define the current shape accordingly
                if ( squareButton.isSelected() )
                {
                    currentShape = new Square(  80, 50, shapeSlider.getValue() );
                }
                else
                {
                    currentShape = new Circle( 80, 50, shapeSlider.getValue() );
                }
     
                // send the current shape to the drawing class and
                // redraw when the button values change
                drawPanel.setCurrentShape( currentShape );
                drawPanel.repaint();
     
            } // end method actionPerformed()
     
        } // end class Button Listener
     
        // a main() method to launch the app on the EDT
        public static void main( String[] args )
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                @Override
                public void run()
                {
                    new DrawDemo();
                } // end method run()
            } );
        } // end method main()
     
    } // end class DrawDemo
     
    // an abstract class to define the basic properties of all shapes and
    // specify which methods implementations must have
    abstract class Shape
    {
        // x/y specify the upper left corner, width the width
        int x;
        int y;
        int width;
     
        // an abstract method to require shapes to draw themselves on the
        // Graphics objects passed by the parameter g
        protected abstract void draw( Graphics g );
     
        // a mutator to set x
        public void setX( int x )
        {
            this.x = x;
        } // end method setX()
     
        // a mutator to set y
        public void setY( int y )
        {
            this.y = y;
        } // end method setY()
     
        // a mutator to set width
        public void setWidth( int width )
        {
            this.width = width;
        } // end method setWidth()
     
    } // end class Shape
     
     
    // a simple class that defines and draws a circle
    class Circle extends Shape
    {
        // default constructor
        public Circle()
        {
            x = 80;
            y = 50;
            width = 10;
     
        } // end default constructor
     
        // a constructor that specifies x, y, and the diameter as width
        public Circle( int x, int y, int width )
        {
            this.x = x;
            this.y = y;
            this.width = width;
     
        } // end constructor
     
        // the draw method as required by the abstract Shape class
        protected void draw( Graphics g )
        {
            g.fillOval( x, y, width, width );
     
        } // end method draw()
     
    } // end class Circle
     
    //a simple class that defines and draws a square
    class Square extends Shape
    {
        // default constructor
        public Square()
        {
            x = 80;
            y = 50;
            width = 10;
     
        } // end default constructor
     
        // a constructor that specifies x, y, and the diameter as width
        public Square( int x, int y, int width )
        {
            this.x = x;
            this.y = y;
            this.width = width;
     
        } // end constructor
     
        // the draw method as required by the abstract Shape class
        protected void draw( Graphics g )
        {
            g.fillRect( x, y, width, width );
     
        } // end method draw()
     
    } // end class Square
     
    // a JPanel on which to draw - this is where the magic is
    final class DrawPanel extends JPanel
    {
        // a current shape object for use by the draing panel
        Shape currentShape;
     
        // default constructor
        protected DrawPanel()
        {
            // initialize the current shape object to ensure that
            // the current shape object will never be null
            currentShape = new Circle();
     
            setPreferredSize( new Dimension( 300, 250 ) );
     
        } // end default constructor
     
        // a method to paint or draw on the draw panel
        @Override
        public void paintComponent( Graphics g )
        {
            // clear the pallette each time the method is called
            super.paintComponent( g );
     
            // vary the color, red for square, blue for circle
            if ( currentShape instanceof Square )
            {
                g.setColor( Color.RED );
            }
            else
            {
                g.setColor( Color.BLUE );
            }
     
            // draw the correct shape
            currentShape.draw( g );
     
        } // end method paintComponent()
     
        // a method to set the drawing panel's current shape
        protected void setCurrentShape( Shape currentShape )
        {
            this.currentShape = currentShape;
     
        } // end method setCurrentShape()
     
    } // end class DrawPanel

Page 2 of 2 FirstFirst 12

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, 02:44 PM
  3. Getting the Selected Value in JComboBox
    By aStudentofJava in forum AWT / Java Swing
    Replies: 2
    Last Post: March 9th, 2012, 09: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, 06: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