Java Reference
In-Depth Information
case RECTANGLE:
// return new Element.Rectangle(start, end,
// theApp.getWindow().getElementColor());
case CIRCLE:
// return new Element.Circle(start, end,
// theApp.getWindow().getElementColor());
case CURVE:
// return new Element.Curve(start, end,
// theApp.getWindow().getElementColor());
default:
assert false; // We should never get to here
}
return null;
}
If you compile and run Sketcher you should be
able to draw a figure like that shown below.
You can only draw lines at this point though. Trying to draw anything else will result in an assertion
because we will fall through the cases in the createElement() method until we hit the default case.
How It Works
As you drag the mouse Element.Line objects are being repeatedly created and drawn to produce the
rubber banding effect. Each line is from the point where you pressed the mouse button to the current
cursor position. Try drawing different colors. It should all work. If it doesn't, maybe you forgot to
remove the getContentPane().setBackground(color) call that we put temporarily in
ColorActions actionPerformed() method in SketchFrame .
If you are typing in the code as you go (and I hope that you are!), you may have made a few mistakes,
as there's been such a lot of code added to Sketcher. In this case don't look back at the code in the topic
first to find out why. Before you do that, try using the Java debugger that comes with the JDK, or even
just instrumenting the methods that might be the problem with println() calls of your own so you
can trace what's going on. It's good practice for when you are writing your own code.
Search WWH ::




Custom Search