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

}

You basically want to set it up so that everything gets repainted, not just your rectangles. So put your drawRectangle statements in the method where all the other graphics are drawn (paint I believe), and use an "if" statement and a boolean to determine if you actually want to draw the rectangle. I think they should be put in the paint method. I'm not completely positive about all of this advice, so wait for someone to verify it. In the meantime, Here is an example from the Java Sun Tutorials http://java.sun.com/docs/books/tutorial/2d/geometry/examples/ShapesDemo2D.java

Correct. The drawing code needs to either be moved into the paint() method or called from it with a reference to the Graphics object. See comments in edited code:

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;
    int Selection;  // moved this up here

    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)
    {
         // set new selection index
         Selection = myChoice.getSelectedIndex();
         repaint();
    }

 public void paint(Graphics g)
	{
         super.paint(g);

         if (Selection == 1)
         {  
             // you can call these methods directly here
             g.drawRect(50,50,100,100);
         }
         if (Selection == 2)
         {
             g.drawLine(50,50,200,50);
         }
         if (Selection == 3)
         {
             // or you can pass the Graphics reference as a parameter
             drawAnOval(50,50,200,50,g);
         }
	}

  // added Graphics as a parameter here
 public void drawAnOval(int ovalX, int ovalY, int ovalWidth, int ovalHeight, Graphics g)
  {
    g.drawOval(ovalX, ovalY, ovalWidth, ovalHeight);
  }

}

Thanks, I see now how making the selection global I can used it within other methods.

I will now go away and read up on a few things, very interesting (especially the part of passing the graphics reference as a parameter).

Cheers, much appreciated.

Oh one last thing for clearing the on-screen display for an applet is their another method commonly used rather that getting the dimension size and background colour and simply drawing an applet sized rectangle over the top?

Cheers

Oh one last thing for clearing the on-screen display for an applet is their another method commonly used rather that getting the dimension size and background colour and simply drawing an applet sized rectangle over the top?

Well, you can just not paint anything else after the call to super.paint()
:)

This article has been dead for over six months. Start a new discussion instead.