Shared Shape Methods

All of the types of visible objects are placed on a DrawingCanvas, which is where they actually are drawn. They all have methods to remove them from a DrawingCanvas or add them back to it. They also all have methods to determine in what order they will be drawn, and to hide or show them without actually removing them from the canvas. Also, all shapes have methods for getting and setting their color.

Table of Contents

1. Hiding, showing, and removing from the canvas

Your browser doesn't do Java.
HideAndShow.java
You can make a shape disappear temporarily by calling its hide() method:

public void hide()

When you want the object to reappear, call its show() method:

public void show()

So, for example, this code will hide and show an object as the mouse is pressed, then released:

public void onMousePress(Location point) {
   rect.hide();
}
public void onMouseRelease(Location point) {
   rect.show();
}

Your browser doesn't do Java.
ToggleHide.java
Sometimes you will want to ask an object whether it is hidden or not. There is a method for this as well:

public boolean isHidden()

So, for example, this code would toggle an object back and forth between hidden and visible every time you click:

public void onMouseClick(Location point) {
   if(rect.isHidden()) {
      rect.show();
   } else {
      rect.hide();
   }
}

If you want an object to disappear permanently - you aren't intending to show() it again later - then it is better to remove it from the canvas completely so that it won't be taking up space in the canvas' list of objects. You do this with the removeFromCanvas() method:

public void removeFromCanvas()

Actually, as it turns out, removing an object from the canvas is not irrevocably permanent, because there is another method to add it back on again:

public void addToCanvas(DrawingCanvas c)

In the unlikely even that you have two canvases and try to add an object to one canvas that was already on the other, it will automatically remove itself from its current canvas before adding itself to the other.

You can retrieve the DrawingCanvas that an object belongs to with this method:

public DrawingCanvas getCanvas()

This is useful mostly for if you have, for example, an ActiveObject that controls one object but occasionally creates another. You can then get the canvas of one object to place the new object on the same canvas.

2. Changing the order of drawing

Your browser doesn't do Java.
PushBack.java
Every time that the canvas has been updated and needs to be redrawn, it goes through its entire list of shapes one by one and draws them in the order they were created. It is important to remember that the canvas works this way, because it means that shapes I created later will end up drawing on top of shapes drawn earlier. So, for example, if I created a white rectangle filling the whole window, it would look like I had cleared the canvas, when in reality I had merely covered everything up.

Sometimes you will want to change the order in which things are drawn. You can send an object forward or backward one in the draw order using these methods:

public void sendForward()
public void sendBackward()

You can also send an object all the way to the front or back with these methods:

public void sendToBack()
public void sendToFront()

So, for example, in a game or animation, when you start a new scene and want to change the background, you would remove the old background from the canvas, and create a new VisibleImage with the new background. But it would then be covering up everything in the scene, since it would be the last object to have been created. You could fix this simply by telling it to sendToBack().

3. Dealing with the shape's color

All shapes have a color. You can set the color of a shape to a new value, and you can also retrieve its current Color.

public void setColor(Color color)
public Color getColor()

The Color class used here is part of the standard Java libraries. If you'd like to know more about creating and using colors, read Color.

4. Figuring out if a Location is inside the shape

Your browser doesn't do Java.
ClickLines.java
Quite often you will want to know if a particular point is inside a shape or not. Most often this will be because the user has clicked, you have the Location he clicked at, and you want to figure out what shape he clicked on. You can ask a shape whether it contains a particular point with this method:

public boolean contains(Location point)

For Lines and FramedArcs, because they don't actually have any area "inside" them, contains() merely checks whether the point is within 4 pixels of the line. To get some idea for where the clickable area of a Line or FramedArc, try clicking around in the applet to the left; red points will show up if your click was inside one of the shapes. Or, just hit the space bar to have the computer automatically "click" on every point on the canvas.

For Text, the contains() method checks whether the point is inside of a rectangle wrapped around the outside of the text, rather than checking individually whether it is inside of any character in the text.

5. Moving the shape around

Every shape can be moved around. You can either tell it how far to move in each direction (x and y), or tell it exactly where to move to:

public void move(double dx, double dy)
public void moveTo(double x, double y)
public void moveTo(Location point)

Your browser doesn't do Java.
DragAnything.java
The fact that the moving methods and contains() are all part of the DrawableInterface - shared by all objects - makes it easy to write programs that drag objects around without needing to know what sort of object is being dragged. Making the variable that hold the dragged object be of type DrawableInterface allows it to access all the important methods without worrying about the details of what sort of objects are being dragged.

One particularly helpful method if you are doing something like this is the getDrawables() method in the DrawingCanvas class; you can read more about it in DrawingCanvas.

6. Summary of the specifications for shared methods

Here is a complete list of methods that every drawable object knows how to perform:

public void hide()
public void show()
public boolean isHidden()

public void removeFromCanvas()
public void addToCanvas(DrawingCanvas canvas)
public DrawingCanvas getCanvas()

public void sendForward()
public void sendBackward()
public void sendToBack()
public void sendToFront()

public void setColor(Color color)
public Color getColor()

public boolean contains(Location point)

public void move(double dx, double dy)
public void moveTo(double x, double y)
public void moveTo(Location point)