Use Graphics in a Java Applet
Applets involving graphics and animations usually look more exciting than applets that don't. Here is a basic overview of how to implement graphics in an applet.
Contents
Steps
- In your paint method, have a parameter that needs a Graphics class. Using the graphics class, you should be able to draw things and images inside the paint method. The method signature should look like this:
Drawing Lines
- Use the drawLine method of the Graphics class. The arguments to the method should be, in order, the first x coordinate, the first y coordinate, the second x coordinate, and the second y coordinate. The line will be drawn from the first x and y to the second. Here is an example:
- The line will be drawn from (10,20) to (50,60).
Drawing Rectangles
- Invoke the drawRect method of the Graphics class. The arguments to the method should be, in order, the top left corner's x coordinate, the top left corner's y coordinate, the width, and the height. Here is a sample code snippet:
- So, the rectangle's top left corner's coordinates will be (10,15), and its width will be 50 pixels, and its height will be 30 pixels.
Drawing Images
- To draw images, import the class Image. Type this at the top of your code (not in your class).
- Now, create an Image object. Here is the code for making an Image object. Instead of typing "getCodeBase()", you can also replace it with a URL. If your picture file is inside a folder, you can also type that folder with the name.
- To draw the image, use the drawImage method of Graphics. The arguments to the method should be, in order, the image object name, the x coordinate, the y coordinate, the width, the height, and "this". Here is the code snippet:
- The image's top left corner will be at (300,200). Its width will be 60 pixels, and its height will be 120 pixels.
Graphics2D
In the majority of browsers the Graphics object that the paint method receives can be casted into Graphics2D that provides more advanced features: Template:CodeBox
Generated images
To show your generated images like Mandelbrot set, use the BufferedImage class: Template:CodeBox Painting the BufferedImage will likely to take much less time than preparing it, so think if it is not possible to remember the image, reusing the same one if nothing has changed since the paint method have been previously called.
Repainting
Your applet will not be more useful than a picture if you will not update it periodically or in response to some user action. To update in response to the user input, call the applet's repaint method from the event listener that you have registered for the button or some other control.
Tips
- There are many more methods in the Graphics class in the Java API, such as drawing ovals and drawing polygons. Take a look for yourself: The Java API - Graphics.
Warnings
- Check your applet with all browsers you wish to support.
- Do not use drawLine or fillRect to draw individual pixels. One common mistake is to call g.fillRect(x, y, 1, 1) but this is much slower than it can and should be. The proper way is to master the BufferedImage as described above.
Related Articles
- Program in Java
- Install Java on Linux
- Install Java
- Write Your First Program in Java
- Make a Java Applet Animation With CarpStudio
Sources and Citations
- The Java API - Contains documentation on all pre-defined classes in Java.
- The 2D Graphics Tutorial - The Sun Java website has a wide range of very helpful tutorials to learn from.