Last minute attempts to find what I am doing wrong here. I am a beginning Java Programmer, as you will see, and need help understanding why my paint method will not draw onto the panel. So far all I have tried to accomplish is to get the circle object to display when I select the circle radio button. Please advise...

import javax.swing.JFrame;

public class ConcentricShapesViewer
{
	public static void main(String[] args)
	{
		JFrame frame = new ConcentricShapes ();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setSize(500,700);
}
}
import java.awt.BorderLayout;
   import java.awt.Font;
   import java.awt.GridLayout;
   import java.awt.Color;
   import java.awt.Graphics;
   import java.awt.Graphics2D;
   import java.awt.Rectangle;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import javax.swing.ButtonGroup;
   import javax.swing.JCheckBox;
   import javax.swing.JComboBox;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JRadioButton;
   import javax.swing.border.EtchedBorder;
   import javax.swing.border.TitledBorder;
   import javax.swing.JComponent;
	import javax.swing.SwingConstants;

/**
 *  This class uses a recursive method to draw
 *  concentric shapes. 
 */

    public class ConcentricShapes extends JFrame
{   
       public ConcentricShapes()
      {
		      
          class ChoiceListener implements ActionListener
         {
             public void actionPerformed(ActionEvent event)
            {
               repaint();
            }
         }
         ChoiceListener listener = new ChoiceListener();
         createControlPanel();
      }
		
       public void createControlPanel()
      {     
			JPanel aButton = createRadioButton();
			JPanel aComboBox = createComboBox();
			JPanel aCount = createCountOfLoops();
			JPanel drawing = createDrawing();
      
         JPanel controlPanel = new JPanel();
			JPanel controlPanelb = new JPanel();
         controlPanel.setLayout(new GridLayout(4,1));
         controlPanel.add(aButton);
			controlPanel.add(aComboBox);
			controlPanel.add(aCount);
			controlPanelb.add(drawing);
			
			add(controlPanel, BorderLayout.CENTER);
			add(controlPanelb, BorderLayout.SOUTH);
      }
		
		public JPanel createComboBox()
		{
			JComboBox shapeColorCombo = new JComboBox();
			shapeColorCombo.addItem("Red");
			shapeColorCombo.addItem("Blue");
			shapeColorCombo.addItem("Green");
			shapeColorCombo.addItem("Black");
			shapeColorCombo.addActionListener(listener);
			
			colorString = (String)shapeColorCombo.getSelectedItem();
						
			JPanel panel = new JPanel();
			panel.add(shapeColorCombo);
			panel.setBorder(new TitledBorder(new EtchedBorder(), "Pick a color"));
			return panel;
		}
   
	
       public JPanel createRadioButton()
      {
         squareButton = new JRadioButton("Square", false);
         squareButton.addActionListener(listener);
      
         circleButton = new JRadioButton("Circle", false);
         circleButton.addActionListener(listener);
      
         ButtonGroup aGroup = new ButtonGroup();
         aGroup.add(squareButton);
         aGroup.add(circleButton);
      
         JPanel buttonPanel = new JPanel();
         buttonPanel.add(squareButton);
         buttonPanel.add(circleButton);
         buttonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Pick a shape"));
         return buttonPanel;
      }
		
		public JPanel createCountOfLoops()
		{
			JComboBox createCountOfLoops = new JComboBox();
			createCountOfLoops.addItem("1");
			createCountOfLoops.addItem("2");
			createCountOfLoops.addItem("3");
			createCountOfLoops.addItem("4");
			createCountOfLoops.addItem("5");
			createCountOfLoops.addItem("6");
			createCountOfLoops.addItem("7");
			createCountOfLoops.addItem("8");
			createCountOfLoops.addItem("9");
			createCountOfLoops.addItem("10");
			createCountOfLoops.addItem("11");
			createCountOfLoops.addItem("12");
			createCountOfLoops.addItem("13");
			createCountOfLoops.addItem("14");
			createCountOfLoops.addItem("15");
			createCountOfLoops.addItem("16");
			createCountOfLoops.addItem("17");
			createCountOfLoops.addItem("18");
			createCountOfLoops.addItem("19");
			createCountOfLoops.addItem("20");
			createCountOfLoops.addActionListener(listener);
			
			loops = (String)createCountOfLoops.getSelectedItem();
			countLoops = Integer.parseInt(loops);
						
			JPanel panel = new JPanel();
			panel.add(createCountOfLoops);
			panel.setBorder(new TitledBorder(new EtchedBorder(), "How many??"));
			return panel;
		}
		
		public JPanel createDrawing()
		{
			paint(g);
			
			JPanel panel = new JPanel();
			panel.setBorder(new TitledBorder(new EtchedBorder()));
			return panel;
		}
		
		
   	public void paint(Graphics g)
		{						
			if (circleButton.isSelected())
   		{  
				int n=10;
				int topXY = 5;
				int size=300;
					if (n > 0)
     			    	{
        			    g.drawOval(topXY, topXY-1, size+15, size-30);
         			}
						
			}
   		else if (squareButton.isSelected())
   		{ 
				int n=10;
				int topXY = 5;
				int size=300;
					if (n > 0)
     		    	{
       	  		 g.drawRect(topXY, topXY-1, size+15, size-30);
         		}
			}
   	}

   
  		private String colorString;		
		private String loops;
		private int countLoops;    
		private ActionListener listener;
      private JRadioButton squareButton;
      private JRadioButton circleButton;
		private JComboBox shapeColorCombo;
		private JLabel sampleField;
		private JPanel contentPane;
      private Graphics g;
   }

Recommended Answers

All 4 Replies

please use code tags.

please use code tags.

I thought I did what was requested with the code.

Firstly,
you defined a constructor which only repaints.
you defined functions but you never called them,
you defined a listener but you never created it.

I think you should revise your approach.

You mixed up different styles.

Have a look at here

Thank you very much

(code Viewer)
(code class code)

just replace aboves with (code=JAVA).

anyway,
Firstly,
you defined a constructor which only repaints.
you defined functions but you never called them,
you defined a listener but you never created it.

I think you should revise your approach.

You mixed up different styles.

Have a look at here:
http://java.sun.com/docs/books/tutorial/uiswing/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.