asian_al 0 Newbie Poster

I cant get the program to drawy the triangle properly. Can some help me with the draw the polgygon triangle?

/* Alex Bruso (student 2) + Tim Mumford (student 1)
Hw Shape Group Exercise
This program will create an applet which will draw shapes.
The input from the user will be done from a seperate frame.
The frame will have a title bar, a title, a combobox,
labeled textfields properties of the shape.  There is a checkbox
to fill the shape or not.  There is also a combobox for the colors (seven of them).
The fram will be sized appropriately with two buttons. One for clearing the
input and the other which will draw the shapes from an array of shape references.
It will call the paint method to use a for loop which calls the draw method
for each shape in the array (dynamic method binding).
*/

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


public class guishape extends JApplet implements ActionListener
{
	inhshape [] shape = new inhshape [10];  // array of shape references
	int y, x;
	int w, h, q;
	int ph, ph1, ph2, ph3, ph4;
	int comboc = -1;
	int combos = -1;
	JFrame f = new JFrame("Frame title");
	JLabel title = new JLabel("Input");
	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;
	String s,m,n,b,v,c;
	JButton draw, clear;
	String[] ps = {"Oval", "Rectangle", "Triangle", "Half Oval"};
	JComboBox scolor;
	Object[] color = new Object[9];
	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("Shape Creator");
		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);

		color[1] = Color.blue;
		color[2] = Color.red;
		color[3] = Color.pink;
		color[4] = Color.green;
		color[5] = Color.yellow;
		color[6] = Color.orange;
		color[7] = Color.darkGray;
		color[8] = Color.magenta;

		for (y = 0; y <= 10; y++)
		   {
			   shape[y].draw(g);
		   }

		super.paint(g);


	}

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


	public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == clear)
			  {
				  filled.setSelected(false); // change the checkbox to unchecked
				  pshape.setSelectedIndex(-1);
				  scolor.setSelectedIndex(-1);
				  xvalue.setText(""); // set the text inside the xvalue text area to nothing
				  yvalue.setText("");
				  height.setText("");
				  width.setText("");
			  }
			  else
				if (e.getSource() == draw)
			  {
				  x = Integer.parseInt(xvalue.getText());
				  y = Integer.parseInt(yvalue.getText());
				  w = Integer.parseInt(width.getText());
				  h = Integer.parseInt(height.getText());

				  if (pshape.getSelectedIndex() == 0)
				  	{
						shape[q] = new Oval(x, y, w, h, scolor.getSelectedIndex(), filled.isSelected());
						q++;
					}
					else
					if (pshape.getSelectedIndex() == 1)
					  {
						  shape[q] = new Rectangle(x, y, w, h, scolor.getSelectedIndex(), filled.isSelected());
						  q++;
					  }
					  else
					  if (pshape.getSelectedIndex() == 2)
					  	{
							shape[q] = new Triangle(x, y, w, h, scolor.getSelectedIndex(), filled.isSelected());
							q++;
						}
						else
						if (pshape.getSelectedIndex() == 3)
							{
								shape[q] = new HalfOval(x, y, w, h, scolor.getSelectedIndex(), filled.isSelected());
								q++;
							}
					  repaint();

			  }

	     }
	 }
/* Alex Bruso
HW Basic Graphics Package
This sub class will define a half oval
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList.*;

public class HalfOval extends inhshape
{
	public HalfOval(int x, int y, int w, int h, int 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
	{
			  switch (color)
			  				  {
			  				  		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;

							}
							g.drawArc(xvalue, yvalue, width, height, 0, 180);
										if (filled == true)
										  {
											  g.fillArc(xvalue, yvalue, width, height, 0 ,180);

	    }
	}
}
/* Alex Bruso
Hw Basic Graphics Package
Abstract super class
This abstract super class will declare all of the protected variables.
There is a public abstract void draw method with the argument Graphics
g.This method will be abstract until overwritten

*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList.*;

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


	public inhshape(int x, int y, int w, int h, int 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
oval
This sub class will define an oval
*/
import java.awt.*;
import java.util.ArrayList.*;

public class Oval extends inhshape
{
	public Oval(int x, int y, int w, int h, int 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
	{
			  switch (color)
			  				  {
			  				  		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;

							  }  // close switch

							g.drawOval(xvalue, yvalue, width, height);
										if (filled == true)
										  {
											  g.fillOval(xvalue, yvalue, width, height);

			  }
	    }
	}
/* Alex Bruso
HW Basic Graphics Package
This sub class will define a rectangle
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList.*;

public class Rectangle extends inhshape
{
	public Rectangle(int x, int y, int w, int h, int 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
	{
			   switch (color)
			  		 {
			  		     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;
					  } // close switch

				if (filled == true)
				  {
					 g.fillRect(xvalue, yvalue, width, height); // fill the rectangle
				  }
				 g.drawRect(xvalue, yvalue, width, height);
	    }
	}
/* Alex Bruso
HW Basic Graphics Package
This sub class will define a triangle
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Polygon;
import java.util.ArrayList.*;

public class Triangle extends inhshape
{


	public Triangle(int x, int y, int w, int h, int c, boolean f)
	{
		super(x, y, w, h, c, f); // calls the super properties from the super class
		int x1[] = new int[]{x, w, h};
    	int y1[] = new int[]{y, w, h};
	}

	public void draw(Graphics g) //  will override the abstract method in the super
	{
			   switch (color)
			  				  {
			  				  		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;
				}

							if (filled == true)
								  g.fillPolygon(x1, y1, 3);
								  else
								  g.drawPolygon(x1, y1, 3);
			  }
	    }
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.