1

How do I add a drawing ii a Japplet and not replace it with a new one? I am using repaint() but all it does is replace my existing rectangle. What would be the right codes to add another rectangle and not replace it?

Say I have this code: ( This code is not mine )

import java.applet.Applet;
import java.awt.*;

public class MoveBox extends Applet
{
   private int x = 20, y = 20;
   private int width = 10, height = 10;
   private int inc = 5;
   private Color myColor = Color.red;

   private Button up = new Button("Up");
   private Button down = new Button("Down");
   private Button left = new Button("Left");
   private Button right = new Button("Right");
   private Button increase = new Button("[+]");
   private Button decrease = new Button("[-]");

   // init method
   public void init()
   {
      Panel buttons = new Panel();
      buttons.setLayout(new FlowLayout());
      buttons.add(up);
      buttons.add(down);
      buttons.add(left);
      buttons.add(right);
      buttons.add(increase);
      buttons.add(decrease);

      setLayout(new BorderLayout());
      add("South", buttons);
   }
   // public methods
   public boolean action(Event e, Object o)
   {
      if (e.target == up)
         return handleUp();
      else if (e.target == down)
         return handleDown();
      else if (e.target == left)
         return handleLeft();
      else if (e.target == right)
         return handleRight();
      else if (e.target == increase)
         return handleIncrease();
      else if (e.target == decrease)
         return handleDecrease();
      else
         return super.action(e, o);
   }
   public void paint(Graphics g)
   {
      g.setColor(myColor);
      g.fillRect(x,y,width,height);
   }
   // private methods
   private boolean handleUp()
   {
      y = y - inc;
      repaint();
      return true;
   }
   private boolean handleDown()
   {
      y = y + inc;
      repaint();
      return true;
   }
   private boolean handleRight()
   {
      if (x < size().width)
         x = x + inc;
      else
         x = 0;
      repaint();
      return true;
   }
   private boolean handleLeft()
   {
      if (x > 0)
         x = x - inc;
      else
         x = size().width;
      repaint();
      return true;
   }
   private boolean handleIncrease()
   {
      width += 5;
      height += 5;
      repaint();
      return true;
   }
   private boolean handleDecrease()
   {
      if (width > 5)
      {
         width -= 5;
         height -= 5;
      }
      repaint();
      return true;
   }
}

How do I add another rectangle when I press the buttons, up, down, left and right?

Ma Rk Vilches
  • 63
  • 1
  • 1
  • 6

3 Answers3

3

Paint to a BufferedImage and display it in a JLabel. When it is updated, call label.repaint() to see the changes. E.G. as seen in Do Doodle.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Store the things you want to paint in a collection (for example, the coordinates of your two rectangles), call repaint(), and have your paintComponent() method iterate through the collection of things to paint and paint them one by one.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1
mKorbel
  • 109,525
  • 20
  • 134
  • 319