首页 > 解决方案 > GUI application that the shape changes on a radio button click

问题描述

What my application is meant to do is change the background and foreground on a click of a radio button and change the shape of the item based on a radio button.I am trying to get my application to actively change shape based on the radio button that is selected.I have the background and foreground working just not the shape. I have seen another post kinda like this but it has a submit button and does not use the JSlider

https://i.stack.imgur.com/fZD3U.png

Below is what I have been messing with and cannot seem to get the program to execute correctly. I have gotten the shape to change but then the slider breaks. Am i approaching this the wrong way?

public class OvalPanel extends JPanel
    {


        private int diameter = 10; // default diameter

        // draw an oval of the specified diameter

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            if(rectFillRadioButton.isSelected()){
                g.fillRect(10,10,10,10);
                //repaint();
            }
            if(ovalFillRadioButton.isSelected()){
                g.fillOval(10,10,10,10);
                //repaint();
            }
        }

        // validate and set diameter, then repaint
        public void setDiameter(int newDiameter)
        {
            // if diameter invalid, default to 10
            diameter = (newDiameter >= 0 ? newDiameter : 10);
            repaint(); // repaint panel
        }

        // used by layout manager to determine preferred size
        public Dimension getPreferredSize()
        {
            return new Dimension(200, 200);
        }

        // used by layout manager to determine minimum size
        public Dimension getMinimumSize()
        {
            return getPreferredSize();
        }
    }

this is the class that i initially have that sets the paintComponent. I also have

private class TopRadioButtonHandler extends JFrame implements ItemListener {
        private Graphics panel;

        public TopRadioButtonHandler(Graphics p) {
            panel = p;
        }

        @Override
        public void itemStateChanged(ItemEvent event) {
            if(rectFillRadioButton.isSelected()){
                panel = myPanel.getGraphics();
                panel.fillRect(10,10,10,10);
                repaint();
            }
            if(ovalFillRadioButton.isSelected()){
                panel = myPanel.getGraphics();
                panel.fillOval(10,10,10,10);
                repaint();
            }

        }
    }

i dont think i need the repaint but when i use this method my JSlider stops working.

标签: javaswingjframejpanel

解决方案


Am i approaching this the wrong way?

Yes, the paintComponent() method should not be referencing another Swing component.

When you do custom painting, the paintComponent() should only paint the current state of your component.

For example when you use a Jlabel you have methods like setText() and setIcon() to set the text and icon you want to paint.

You already have a method, setDiameter() which is a good start. However, your painting code just hard codes the size of the oval/rectangle. The painting methods should reference you diameter variable.

Now, you need another property to idicate whether to paint an oval or a rectangle. So maybe you need a property like setPaintOval(boolean paintOval).

Then your painting code could be:

If (paintOval)
    g.fillOval(10, 10, diameter, diameter);
else
    g.fillRect(10, 10, diameter, diameter);

Of course the problem with this approach is that you can only paint two objects.

Also, you should never invoke repaint() in a painting method. The repaint() should only be invoked from your setter methods when you change the state of the component.

but then the slider breaks

The code you posted has nothing to do with a slider.

I'm guessing you want the slider to change the diameter of the oval? Well you need to add a ChangeListener to the slider and then invoke your setDiameter() method with the slider value.

Read the section from the Swing tutorial on How to Use Sliders for a working example.


推荐阅读