Java Programming/Canvas

An essential part of programming in Java requires you to build exciting new user interfaces for yourselves. Components that come built into the Java framework are regular UI elements, however for a more rich experience, you need controls of your own. Take, for instance, a charting application. No charting tool comes built into a Java API. You need to manually draw the chart yourself.

Coding drawing, to begin with, is pretty daunting but once you know the basics of Graphics programming in Java, you can create elegant graphics and art in no time. But the question that arises in one's mind is what to draw on. The answer to this question is simpler than it seems. You can start drawing on any component in the Java framework. Whether it be a panel, window or even a button.

Let me break it down for you. A component in the Java language is a class that has been derived from the  class. Each component has a method with a signature  which can be overridden to manually draw something atop it.

Overriding the method
Below is an example on how you need to override the above method. For this very example, the component class that we would be using would be the  class. For more information about the  class, see the section on Understanding the Canvas class

Understanding the class
Code listing 9.1 shows the simplicity and power of the syntax for enabling the graphics functions within Java. Lets begin by understanding what a  class does. A  class is a derivative or a sub-class of the   class and when placed over a , displays as a blank area.

For the purpose of drawing graphics, you may use any other class derived from the  class, for instance,   or even   or. Why we use the  class is purely to grasp the idea of drawing in Java.

Let us refine the above code for the class to be executable and the Canvas to be displayed. For this we will add an entry-point method namely the  method in its body and calling a   class to load the canvas on.

The following code now helps our class to be executable and displays the canvas on top of the frame as it displays. Running this class would result in an empty frame, however it should be clear that the canvas is sitting atop it and is merely not displaying any drawings yet.



Get, set, draw!
Now that the basic structure of our program has been laid out, we need to explore how drawing is actually done by writing Java code. Move to the next section and try your hand at drawing basic shapes and lines. But whilst you are still fresh to the concept of a Canvas, why not test your knowledge. Try answering these questions below.

Question 9.1: What classes are used to draw in Java?
 * 1) Any class that is derived from the   class.
 * 2) Any class that is derived from the   class.
 * 3) None of the above.

2 A class derived from the  class is not viable as a visible component, whereas a class derived from a   class is a visible entity atop a   hence a likely candidate for displaying drawings.

Question 9.2: What is the method that needs to be overridden in order to enable drawing?
 * 1) The   method.
 * 2) The   method.
 * 3) The   method.
 * 4) None of the above.

3 As discussed earlier the  method is the correct option. The name says it all.