zhrh 0 Newbie Poster

Moderator comment:This question was originally posted in http://www.daniweb.com/forums/thread184423.html and moved as original thread is old

Hi there,
i need help with my assignment please help me.
i want to put your code to my code, which is my code is;
i tried but i couldn't solve it.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.Vector;

public class sketcher2 extends JApplet implements ActionListener
{

  JButton red, green, blue, yellow, cyan, white, gray, triangle, orange, line, circle, square, myText, reset;
  JLabel Tag, textLabel;
  JTextField Entry, textString;

  shapeCanvas Drawing;

  int size;


  public sketcher2 () {

	    Drawing = new shapeCanvas ();
   		Drawing.setDiameter (20);
   		Drawing.repaint ();

		Panel canvasPanel = new Panel();
		canvasPanel.setLayout(new FlowLayout());
		canvasPanel.setBackground(Color.gray);



	    Tag = new JLabel ();
		Tag.setText ("Size:");
		Tag.setHorizontalAlignment (JLabel.RIGHT);

		// Size text field
	    Entry = new JTextField ();
		Entry.setColumns (3);
		Entry.setText ("20");
		size = 20;

		Entry.setHorizontalAlignment (JTextField.RIGHT);
		Entry.addActionListener(this);

	    textLabel= new JLabel ();
		textLabel.setText ("Text:");
		textLabel.setHorizontalAlignment (JLabel.RIGHT);

		// Size text field
	    textString = new JTextField ();
		textString.setColumns (20);
		textString.setText ("Type your text");


		reset = new JButton("Clear");
		canvasPanel.add(reset);
		reset.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.reset(); //= Color.red;  // code to execute when button is pressed
				}
			}
		);

		// Buttons
		orange = new JButton("Orange");
		canvasPanel.add(orange);
		orange.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.orange;  // code to execute when button is pressed
				}
			}
		);
		red = new JButton("Red");
		canvasPanel.add(red);
		red.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.red;  // code to execute when button is pressed
				}
			}
		);

		green = new JButton("Green");
		canvasPanel.add(green);
		green.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.green;  // code to execute when button is pressed
				}
			}
		);
		cyan = new JButton("Cyan");
		canvasPanel.add(cyan);
		cyan.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.cyan;  // code to execute when button is pressed
				}
			}
		);

		blue = new JButton("Blue");
		canvasPanel.add(blue);
		blue.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.blue;  // code to execute when button is pressed
				}
			}
		);
		yellow = new JButton("Yellow");
		canvasPanel.add(yellow);
		yellow.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.yellow;  // code to execute when button is pressed
				}
			}
		);
		gray = new JButton("Gray");
		canvasPanel.add(gray);
		gray.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.gray;  // code to execute when button is pressed
				}
			}
		);
		white = new JButton("White");
		canvasPanel.add(white);
		white.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.col = Color.white;  // code to execute when button is pressed
				}
			}
		);
	
		square = new JButton("Triangle");
		canvasPanel.add(square);
		square.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.shape = 1;  // code to execute when button is pressed
					updateTextField("Triangle");
				}
			}
		);

		square = new JButton("Line");
		canvasPanel.add(square);
		square.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.shape = 1;  // code to execute when button is pressed
					updateTextField("Line");
				}
			}
		);
		circle = new JButton("Circle");
		canvasPanel.add(circle);
		circle.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.shape = 0;  // code to execute when button is pressed
					updateTextField("Circle");
				}
			}
		);

		square = new JButton("Square");
		canvasPanel.add(square);
		square.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Drawing.shape = 1;  // code to execute when button is pressed
					updateTextField("Square");
				}
			}
		);

		myText = new JButton("Text");
		canvasPanel.add(myText);
		myText.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					 // code to execute when button is pressed
					Drawing.shape = 2;
					Drawing.strText = textString.getText();
				}
			}
		);
		canvasPanel.add(myText);
		canvasPanel.add(Tag);
		canvasPanel.add(Entry);
		canvasPanel.add(textLabel);
		canvasPanel.add(textString);

		add(canvasPanel , BorderLayout.NORTH);

	    add(Drawing, BorderLayout.CENTER);
  }

  public void updateTextField(String strText)
  {
	textString.setText( strText + " " + Entry.getText());
  }


  public void sizeOfShape()
  {
    String Text;
    Text = Entry.getText ();
    size = Integer.parseInt (Text);
    Drawing.setDiameter (size);
  }

  public void actionPerformed (ActionEvent e)
  {
	  sizeOfShape();
  }

}


class shapeCanvas extends Canvas implements MouseListener
{

  int shape;
  int size;
  int xpos;
  int ypos;
  Color col;
  String strText;

  Vector objects = new Vector();

  public class Shape
  {
  	public int xpos, ypos;
  	public int size;
  	public int shape;
  	public Color col;
        public String strText;
  }

  public void reset() {
     objects.clear();
     repaint();
  }

  public shapeCanvas () {
	shape = -1;
	col = Color.red;
    setBackground (Color.black);
	addMouseListener (this);

  }

  public void setDiameter (int Value) {
    size = Value;
    repaint ();
  }

  public void paint (Graphics g)
  {
    Graphics2D g2;
    Ellipse2D Circle;

    int Width;
    int Height;

    g2 = (Graphics2D) g;
    Width = getWidth ();
    Height = getHeight ();



	for(int i=0; i< objects.size(); i++)
	{
		Shape s = (Shape)objects.elementAt(i);
	    g2.setColor (s.col);
		if(s.shape == 0)
		{
			Circle = new Ellipse2D.Double
					 ((double) (s.xpos),
					  (double) (s.ypos),
					  (double) s.size, (double)s.size);
			g2.fill (Circle);
		}

		if(s.shape == 1)
		{
			g2.fillRect(s.xpos, s.ypos, s.size, s.size);
		}
		if(s.shape == 2)
		{
			g2.drawString(s.strText,s.xpos,s.ypos);
		}
	}

  }


  public void mouseExited (MouseEvent e ) {}
  public void mouseEntered (MouseEvent e ) {}
  public void mouseReleased (MouseEvent e ) {}
  public void mouseClicked (MouseEvent e ) {}


  public void mousePressed (MouseEvent me )  // mouse clicked and released handler
  {
    xpos = me.getX();
    ypos = me.getY();
 	System.out.println (xpos + "," + ypos);
 	Shape s = new Shape();
	s.xpos = xpos;
	s.ypos = ypos;
	s.size = size;
	s.shape = shape;
	s.col = col;
	s.strText = strText;
 	objects.add(s);

 	repaint();

  }

}