Use Graphics in a Java Applet

Revision as of 08:51, 21 March 2017 by Kipkis (Kipkis | contribs) (importing article from wikihow)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Steps

  1. 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

  1. 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

  1. 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

  1. To draw images, import the class Image. Type this at the top of your code (not in your class).
  2. 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.
  3. 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:

Generated images

To show your generated images like Mandelbrot set, use the BufferedImage class: 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

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.