I have been working on this code for some time now. It is a basic graphics package. When the user clicks the draw button I need to create a chape based upon the user's input and put the shape into an array of shape references (up to 10). I attempted this but failed. Then I would like to call a paint method to use a for loop which will call me abstract draw method for each shape in the array by using dynamic method binding. I think the dynamic method binding part is screwing with my brain. Anything would help. Ultimately I would like the gui to take the users input and draw the shape. There are suppossed to be 4 subclasses but I only posted one. I believe they are all very similar. - a rectangle, oval, right triangle, and a half oval. Thank you so much!

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;

public class guishape extends JApplet implements ActionListener
{
	int comboc = -1;
	int combos = -1;
	JFrame f = new JFrame("Frame title");
	JLabel title = new JLabel("Title");
	JLabel pclabel = new JLabel("Pick color:");
	JLabel pslabel = new JLabel("Pick shape:");
	JLabel xlabel = new JLabel("x value:");
	JLabel ylabel = new JLabel("y value:");
	JLabel widthlabel = new JLabel("width:");
	JLabel heightlabel = new JLabel("height:");
	JTextField xvalue = new JTextField(7);
	JTextField yvalue = new JTextField(7);
	JTextField width = new JTextField(7);
	JTextField height = new JTextField(7);
	Font tfont = new Font("Courier", Font.BOLD, 30);
	JCheckBox filled;
	JComboBox pshape;
	JButton draw, clear;
	String[] ps = {"Oval", "Rectangle", "Triangle", "Half Circle"};
	JComboBox scolor;
	String[] cc =  {"Blue", "Red", "Pink", "Green", "Yellow", "Orange", "Dark Grey", "Magenta"};

	public void init()
	{
		// Frame
		Container c = f.getContentPane();
		f.setLayout(new GridLayout(5, 5, 5, 10));  // rows then col
		f.setBackground(Color.white);
		f.setTitle("Title of frame");
		f.setResizable(true);
		f.setSize(500, 300);

        title.setForeground(Color.blue);
        xlabel.setForeground(Color.blue);
        ylabel.setForeground(Color.blue);
        pslabel.setForeground(Color.blue);
        pclabel.setForeground(Color.blue);
        heightlabel.setForeground(Color.blue);
        widthlabel.setForeground(Color.blue);
        title.setFont(tfont);
		pshape = new JComboBox(ps);
		filled = new JCheckBox("Fill");
		scolor = new JComboBox(cc);
		scolor.setMaximumRowCount(6);
		draw = new JButton("Draw");
        clear = new JButton("Clear");

        scolor.setSelectedIndex(-1);
        pshape.setSelectedIndex(-1);


        c.add(new JLabel(""));  // for empty cell
        c.add(new JLabel(""));
        c.add(title);
        c.add(new JLabel(""));
        c.add(new JLabel(" "));
		c.add(pslabel);
        c.add(pshape);
        c.add(pclabel);
        c.add(scolor);
        c.add(filled);
        c.add(xlabel);
        c.add(xvalue);
        c.add(heightlabel);
        c.add(height);
        c.add(new JLabel(""));
        c.add(ylabel);
        c.add(yvalue);
        c.add(widthlabel);
		c.add(width);
		c.add(new JLabel(""));
		c.add(new JLabel(""));
        c.add(draw);
        c.add(new JLabel(""));
        c.add(clear);

		scolor.addActionListener(this);
		draw.addActionListener(this);
		clear.addActionListener(this);

		Container ca;

		ca = getContentPane();  // create a container for the Applet
		ca.setBackground(Color.white);  // set the Applet's container background to white

		f.setVisible(true);

	}

	public void paint(Graphics g)
	{
		/*super.paint(g);
		switch (comboc)
			  {
				  case 0: g.setColor(Color.blue);
				  		  break;
				  case 1: g.setColor(Color.red);
				          break;
				  case 2: g.setColor(Color.pink);
				  	      break;
				  case 3: g.setColor(Color.green);
				  	      break;
				  case 4: g.setColor(Color.yellow);
				  	      break;
				  case 5: g.setColor(Color.orange);
				  	      break;
				  case 6: g.setColor(Color.darkGray);
				  	      break;
				  case 7: g.setColor(Color.magenta);
				  	      break;
			  }  // end switch
		g.drawRoundRect(75, 50, 324, 140, 10, 10);
		g.drawLine(183, 50, 183, 190);
		g.drawLine(291, 50, 291, 190);*/

	}

	public void itemStateChanged(ItemEvent e)
		{
			comboc = scolor.getSelectedIndex();
			combos = pshape.getSelectedIndex();
		}


	public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == clear)
			  {
				  filled.setSelected(false);
				  pshape.setSelectedIndex(-1);
				  scolor.setSelectedIndex(-1);
				  xvalue.setText("");
				  yvalue.setText("");
				  height.setText("");
				  width.setText("");
			  }

			if (e.getSource() == draw)
			  {
				  int shapeList[];
				  shapeList[1] = pshape.getSelectedIndex();




			  }

	     }
 }
/*
Abstract super class
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public abstract class inhshape
{
	protected int xvalue;
	protected int yvalue;
	protected int width;
	protected int height;
	protected boolean filled;
	protected Object color = new Object[7];

	public inhshape(int x, int y, int w, int h, boolean fill)
	{
		xvalue = x;
		yvalue = y;
		width = w;
		height = h;
		filled = fill;
	}

	public abstract void draw();  // abstract until overwritten
}
*/
rectangle sub class
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public abstract class rectangle extends inhshape
{
	public rectangle(int x, int y, int w, int h, boolean f)
	{
		super(x, y, w, h, f); // calls the super properties from the super class
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
		draw(g);
		{
			g.drawRect(xvalue, yvalue, width, height);
			if (filled == true)
			  {
				  g.fillRect(xvalue, yvalue, width, height);
			  }
	    }
	}
}

To store your shapes you need a suitable array - although for simplicity an ArrayList is easier. As each shape is created you can add it to the ArrayList, then you can loop thru the ArrayList calling draw() on each element - Java will see what kind of shape it is and call the appropriate draw implementation.

ArrayList<inhshape> shapes = new ArrayList<inhshape>();

shapes.add (new rectangle(...));

for (shape s : shapes) {
   s.draw(...);
}

ps: By convention, your class names should start with a capital letter. In the abstract class you probably want the draw method to take a parameter.

Thank you!

I am getting the error:

H:\2nd Semester\java2\homework\final project\guishape.java:144: cannot find symbol
symbol : constructor oval(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class oval
shapeList[x] = new oval(xvalue.getText(), yvalue.getText(), width.getText(), height.getText());

Here is the new code:

/* Alex Bruso
Hw Shape Group Exercise

*/

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;

public class guishape extends JApplet implements ActionListener
{
	String shape = new String("");
	String[] shapeList = new String[10];
	int y = 0;
	int x = 0;
	int comboc = -1;
	int combos = -1;
	JFrame f = new JFrame("Frame title");
	JLabel title = new JLabel("Title");
	JLabel pclabel = new JLabel("Pick color:");
	JLabel pslabel = new JLabel("Pick shape:");
	JLabel xlabel = new JLabel("x value:");
	JLabel ylabel = new JLabel("y value:");
	JLabel widthlabel = new JLabel("width:");
	JLabel heightlabel = new JLabel("height:");
	JTextField xvalue = new JTextField(7);
	JTextField yvalue = new JTextField(7);
	JTextField width = new JTextField(7);
	JTextField height = new JTextField(7);
	Font tfont = new Font("Courier", Font.BOLD, 30);
	JCheckBox filled;
	JComboBox pshape;
	JButton draw, clear;
	String[] ps = {"Oval", "Rectangle", "Triangle", "Half Circle"};
	JComboBox scolor;
	String[] cc =  {"Blue", "Red", "Pink", "Green", "Yellow", "Orange", "Dark Grey", "Magenta"};



	public void init()
	{
		// Frame
		Container c = f.getContentPane();
		f.setLayout(new GridLayout(5, 5, 5, 10));  // rows then col
		f.setBackground(Color.white);
		f.setTitle("Title of frame");
		f.setResizable(true);
		f.setSize(500, 300);

        title.setForeground(Color.blue);
        xlabel.setForeground(Color.blue);
        ylabel.setForeground(Color.blue);
        pslabel.setForeground(Color.blue);
        pclabel.setForeground(Color.blue);
        heightlabel.setForeground(Color.blue);
        widthlabel.setForeground(Color.blue);
        title.setFont(tfont);
		pshape = new JComboBox(ps);
		filled = new JCheckBox("Fill");
		scolor = new JComboBox(cc);
		scolor.setMaximumRowCount(6);
		draw = new JButton("Draw");
        clear = new JButton("Clear");

        scolor.setSelectedIndex(-1);
        pshape.setSelectedIndex(-1);


        c.add(new JLabel(""));  // for empty cell
        c.add(new JLabel(""));
        c.add(title);
        c.add(new JLabel(""));
        c.add(new JLabel(" "));
		c.add(pslabel);
        c.add(pshape);
        c.add(pclabel);
        c.add(scolor);
        c.add(filled);
        c.add(xlabel);
        c.add(xvalue);
        c.add(heightlabel);
        c.add(height);
        c.add(new JLabel(""));
        c.add(ylabel);
        c.add(yvalue);
        c.add(widthlabel);
		c.add(width);
		c.add(new JLabel(""));
		c.add(new JLabel(""));
        c.add(draw);
        c.add(new JLabel(""));
        c.add(clear);

		scolor.addActionListener(this);
		draw.addActionListener(this);
		clear.addActionListener(this);

		Container ca;

		ca = getContentPane();  // create a container for the Applet
		ca.setBackground(Color.white);  // set the Applet's container background to white

		f.setVisible(true);

	}

	public void paint(Graphics g)
	{
		for (y = 0; y < shapeList.length; y++)
		   {
			   shape = shapeList[y];
			   //draw(shape);
		   }


	}

	public void itemStateChanged(ItemEvent e)
		{
			comboc = scolor.getSelectedIndex();
			combos = pshape.getSelectedIndex();
		}


	public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == clear)
			  {
				  filled.setSelected(false);
				  pshape.setSelectedIndex(-1);
				  scolor.setSelectedIndex(-1);
				  xvalue.setText("");
				  yvalue.setText("");
				  height.setText("");
				  width.setText("");
			  }
			  else
				if (e.getSource() == draw)
			  {

				  if (pshape.getSelectedIndex() == 1)
				  	{
						shapeList[x] = new oval(xvalue.getText(), yvalue.getText(), width.getText(), height.getText());
						x++;
					}
					else
					if (pshape.getSelectedIndex() == 2)
					  {
						  shapeList[x] = new rectangle(xvalue.getText, yvalue, width, height, cc, filled);
						  x++;
					  }
					  else
					  if (pshape.getSelectedIndex() == 3)
					  	{
							shapeList[x] = new triangle(xvalue.getText(), yvalue.getText(), width.getText(), height.getText(), cc.getSelectedIndex(), filled.getBoolean);
							x++;
						}
						else
						if (pshape.getSelectedIndex() == 4)
							{
								shapeList[x] = new HalfOval(xvalue, yvalue, width, height, cc, filled);
								x++;
							}
					  repaint();

			  }

	     }
	 }
/* Alex Bruso
HW Basic Graphics Package
rectangle
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class rectangle extends inhshape
{
	public rectangle(int x, int y, int w, int h, String c, boolean f)
	{
		super(x, y, w, h, c, f); // calls the super properties from the super class
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
		{
			g.drawRect(xvalue, yvalue, width, height);
			if (filled == true)
			  {
				  g.fillRect(xvalue, yvalue, width, height);
			  }
	    }
	}
}
/* Alex Bruso
HW Basic Graphics Package
triangle
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Polygon;

public class triangle extends inhshape
{

	public triangle(int x, int y, int w, int h, String c, boolean f)
	{
		super(x, y, w, h, c, f); // calls the super properties from the super class
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
			Polygon poly = new Polygon();
			g.drawPolygon(poly);
			poly.addPoint(xvalue, yvalue);
			if (filled == true)
			  {
				  g.fillPolygon(poly);
			  }
	    }
	}
/* Alex Bruso
Hw Basic Graphics Package
Abstract super class
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public abstract class inhshape
{
	protected int xvalue;
	protected int yvalue;
	protected int width;
	protected int height;
	protected boolean filled;
	protected String color;
	protected String shape;

	public void inhshape()
	{
		xvalue = 0;
		yvalue = 0;
		width = 0;
		height = 0;
		color = null;
		filled = false;
	}

	public inhshape(int x, int y, int w, int h, String c, boolean fill)
	{
		xvalue = x;
		yvalue = y;
		width = w;
		height = h;
		color = c;
		filled = fill;
	}

	public abstract void draw(Graphics g);  // abstract until overwritten
}

1. I can't see your oval class in what you posted. In any case, all the other constructors take 4 ints as their first parameters, but you are passing Strings in your call. You need to convert the Strings from the text fields into ints.
2. Your array is an array of Strings, but what you need to put in it are inhshapes.
3. You loop thru all the elements of the shape array, regardless of whether they have been filled or not, so when you hit the first empty element you will get an Exception.
The suggestions in my previous post would avoid (2) and (3)

Sorry for the mis-post. Here is everything.

/* Alex Bruso
Hw Shape Group Exercise

*/

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList.*;

public class guishape extends JApplet implements ActionListener
{
	ArrayList<inhshape> shape = new ArrayList<inhshape>(10);
	String[] shapeList = new String[10];
	int y = 0;
	int x = 0;
	int comboc = -1;
	int combos = -1;
	JFrame f = new JFrame("Frame title");
	JLabel title = new JLabel("Title");
	JLabel pclabel = new JLabel("Pick color:");
	JLabel pslabel = new JLabel("Pick shape:");
	JLabel xlabel = new JLabel("x value:");
	JLabel ylabel = new JLabel("y value:");
	JLabel widthlabel = new JLabel("width:");
	JLabel heightlabel = new JLabel("height:");
	JTextField xvalue = new JTextField(7);
	JTextField yvalue = new JTextField(7);
	JTextField width = new JTextField(7);
	JTextField height = new JTextField(7);
	Font tfont = new Font("Courier", Font.BOLD, 30);
	JCheckBox filled;
	JComboBox pshape;
	JButton draw, clear;
	String[] ps = {"Oval", "Rectangle", "Triangle", "Half Circle"};
	JComboBox scolor;
	String[] cc =  {"Blue", "Red", "Pink", "Green", "Yellow", "Orange", "Dark Grey", "Magenta"};



	public void init()
	{
		// Frame
		Container c = f.getContentPane();
		f.setLayout(new GridLayout(5, 5, 5, 10));  // rows then col
		f.setBackground(Color.white);
		f.setTitle("Title of frame");
		f.setResizable(true);
		f.setSize(500, 300);

        title.setForeground(Color.blue);
        xlabel.setForeground(Color.blue);
        ylabel.setForeground(Color.blue);
        pslabel.setForeground(Color.blue);
        pclabel.setForeground(Color.blue);
        heightlabel.setForeground(Color.blue);
        widthlabel.setForeground(Color.blue);
        title.setFont(tfont);
		pshape = new JComboBox(ps);
		filled = new JCheckBox("Fill");
		scolor = new JComboBox(cc);
		scolor.setMaximumRowCount(6);
		draw = new JButton("Draw");
        clear = new JButton("Clear");

        scolor.setSelectedIndex(-1);
        pshape.setSelectedIndex(-1);


        c.add(new JLabel(""));  // for empty cell
        c.add(new JLabel(""));
        c.add(title);
        c.add(new JLabel(""));
        c.add(new JLabel(" "));
		c.add(pslabel);
        c.add(pshape);
        c.add(pclabel);
        c.add(scolor);
        c.add(filled);
        c.add(xlabel);
        c.add(xvalue);
        c.add(heightlabel);
        c.add(height);
        c.add(new JLabel(""));
        c.add(ylabel);
        c.add(yvalue);
        c.add(widthlabel);
		c.add(width);
		c.add(new JLabel(""));
		c.add(new JLabel(""));
        c.add(draw);
        c.add(new JLabel(""));
        c.add(clear);

		scolor.addActionListener(this);
		draw.addActionListener(this);
		clear.addActionListener(this);

		Container ca;

		ca = getContentPane();  // create a container for the Applet
		ca.setBackground(Color.white);  // set the Applet's container background to white

		f.setVisible(true);

	}

	public void paint(Graphics g)
	{
		for (y = 0; y < shapeList.length; y++)
		   {
			   shape = shapeList[y];
			   //draw(shape);
		   }


	}

	public void itemStateChanged(ItemEvent e)
		{
			comboc = scolor.getSelectedIndex();
			combos = pshape.getSelectedIndex();
		}


	public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == clear)
			  {
				  filled.setSelected(false);
				  pshape.setSelectedIndex(-1);
				  scolor.setSelectedIndex(-1);
				  xvalue.setText("");
				  yvalue.setText("");
				  height.setText("");
				  width.setText("");
			  }
			  else
				if (e.getSource() == draw)
			  {

				  if (pshape.getSelectedIndex() == 1)
				  	{
						shapeList[x] = new oval(xvalue.getText(), yvalue.getText(), width.getText(), height.getText());
						x++;
					}
					else
					if (pshape.getSelectedIndex() == 2)
					  {
						  shapeList[x] = new rectangle(xvalue.getText, yvalue, width, height, cc, filled);
						  x++;
					  }
					  else
					  if (pshape.getSelectedIndex() == 3)
					  	{
							shapeList[x] = new triangle(xvalue.getText(), yvalue.getText(), width.getText(), height.getText(), cc.getSelectedIndex(), filled.getBoolean);
							x++;
						}
						else
						if (pshape.getSelectedIndex() == 4)
							{
								shapeList[x] = new HalfOval(xvalue, yvalue, width, height, cc, filled);
								x++;
							}
					  repaint();

			  }

	     }
	 }
/* Alex Bruso
Hw Basic Graphics Package
Abstract super class
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public abstract class inhshape
{
	protected int xvalue;
	protected int yvalue;
	protected int width;
	protected int height;
	protected boolean filled;
	protected String color;
	protected String shape;

	//public void inhshape()
	//{
	//	xvalue = 0;
	//	yvalue = 0;
	//	width = 0;
	//	height = 0;
	//	color = null;
	//	filled = false;
	//}

	public inhshape(int x, int y, int w, int h, String c, boolean fill)
	{
		xvalue = x;
		yvalue = y;
		width = w;
		height = h;
		color = c;
		filled = fill;
	}

	public abstract void draw(Graphics g);  // abstract until overwritten
}
/* Alex Bruso
HW Basic Graphics Package
Half Oval
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class HalfOval extends inhshape
{
	public HalfOval(int x, int y, int w, int h, String c, boolean f)
	{
		super(x, y, w, h, c, f); // calls the super properties from the super class
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
		{
			g.drawOval(xvalue, yvalue, width, height);
			if (filled == true)
			  {
				  g.fillOval(xvalue, yvalue, width, height);
			  }
	    }
	}
}
/* Alex Bruso
HW Basic Graphics Package
oval
*/
import java.awt.*;

public class Oval extends inhshape
{
	public Oval(int x, int y, int w, int h, String c, boolean f)
	{
		super(x, y, w, h, c, f); // calls the super properties from the super class
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
		{
			g.drawOval(xvalue, yvalue, width, height);
			if (filled == true)
			  {
				  g.fillOval(xvalue, yvalue, width, height);
			  }
	    }
	}
}
/* Alex Bruso
HW Basic Graphics Package
triangle
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Polygon;

public class Triangle extends inhshape
{

	public Triangle(int x, int y, int w, int h, String c, boolean f)
	{
		super(x, y, w, h, c, f); // calls the super properties from the super class
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
			Polygon poly = new Polygon();
			g.drawPolygon(poly);
			poly.addPoint(xvalue, yvalue);
			if (filled == true)
			  {
				  g.fillPolygon(poly);
			  }
	    }
	}
/* Alex Bruso
HW Basic Graphics Package
rectangle
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Rectangle extends inhshape
{
	public Rectangle(int x, int y, int w, int h, String c, boolean f)
	{
		super(x, y, w, h, c, f); // calls the super properties from the super class
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
		{
			g.drawRect(xvalue, yvalue, width, height);
			if (filled == true)
			  {
				  g.fillRect(xvalue, yvalue, width, height);
			  }
	    }
	}
}

OK, everything I said before still applies.

OK, everything I said before still applies.

" cannot find symbol
symbol : class ArrayList
location: class guishape
ArrayList<inhshape> shape = new ArrayList<inhshape>(10);" I was getting this error

You need to import the ArrayList class:
import java.util.ArrayList;

I did import java.util.ArrayList;

I fixed the previous error. How would I add the rectangle(xvalue, yvalue...) into the first memory location of the ArrayList. (never used an Array list before....

if (pshape.getSelectedIndex() == 2)
					  {
						  shapeList[x] = new Rectangle(xvalue.getText, yvalue, width, height, cc, filled);
						  x++;
					  }

I tried

shape.add(new Oval(xvalue.getText(), yvalue.getText(), width.getText(), height.getText(), scolor.getSelectedIndex(), filled.getSelectedObjects()));

and still got cannot find symbol

You must convert the text to ints, as I said earlier

I don't quite understand the looping through the ArrayList portion. for (shape s : shapes) {
s.draw(...);

Something like:

public void paint(Graphics g)
	{
		for (y = 0; y < shape.size(); y++)
		   {
			   Rectangle.draw(g);

		   }

I hope that is close...

The enhanced "for" loop was new in Java 1.5. It's a much simpler and cleaner way to loop thru arrays and Collections. The example was:

for (shape s : shapes) {
   s.draw(...);
}

... which is spoken as
for each shape "s" in "shapes" call the "draw" method for "s"

In the first pass of the loop, local variable s is set to the first element of shapes, and the draw method is called for it. Then in the second pass it's set the second element and the draw method is called for that one, etc etc.

You can use the old loop format if you like (tho' I don't know why you would), in which case your code is still not quite right - it would need to be:

for (y = 0; y < shape.size(); y++) {
	 shape.get(y).draw(g);
}
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.