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);
}
}
}
}