Greetings,
I am creating an applet to draw a shape based on the selected item within the awt choice object.
It currently draws the selected shape based on selection and quickly dissappears.
I believe that I should be doing all drawing elements from the paint method (which I am not).
I have created seperate methods to draw the element when the item state has changed.
Can someone please help explain how I can draw the shapes and when I need to repaint (what am I doing wrong?) .
Thanks
Rob
package wk8exercise3;
import java.applet.Applet;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
/**
*
* @author Rob
*/
public class wk8Exercise3 extends Applet implements ItemListener {
Choice myChoice;
int rectX;
int rectY;
int rectWidth ;
int rectHeight;
String shape;
public void init()
{
// Create the choice and add some choices
myChoice = new Choice();
myChoice.addItem("Pick a shape to draw");
myChoice.addItem("Draw a rectangle");
myChoice.addItem("Draw a Line");
myChoice.addItem("Draw an Oval");
add(myChoice);
myChoice.addItemListener(this);
}
public void itemStateChanged (ItemEvent e)
{
// Declare integer for use with index of choice
int Selection;
Selection = myChoice.getSelectedIndex();
// Declare variables to hold paramater to be used to draw selected item
if (Selection == 1)
{
drawARectangle(50,50,100,100);
}
if (Selection == 2)
{
drawALine(50,50,200,50);
}
if (Selection == 3)
{
drawAnOval(50,50,200,50);
}
}
public void paint(Graphics g)
{
// Not sure what to do here
}
public void drawARectangle(int RectX, int RectY, int RectWidth, int RectHeight)
{
repaint();
Graphics g = getGraphics();
g.drawRect(RectX, RectY, RectWidth, RectHeight);
}
public void drawALine(int lineX1, int lineY1, int lineX2, int lineY2)
{
repaint();
Graphics g = getGraphics();
g.drawLine(lineX1,lineY1,lineX2,lineY2);
}
public void drawAnOval(int ovalX, int ovalY, int ovalWidth, int ovalHeight)
{
repaint();
Graphics g = getGraphics();
g.drawOval(ovalX, ovalY, ovalWidth, ovalHeight);
}
}