Home : Course Map : Chapter 6 : Java : Supplements :
Simple Drawing Techniques
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

This applet illustrates more of the drawing methods in the Graphics class:

import java.applet.Applet;
import java.awt.Graphics;

/** Demonstrate basic AWT drawing with Graphics. **/
public class SimpleDraw1Applet extends Applet
{
  public void paint (Graphics g) {
   // Get the drawing area
   int dY = getSize ().height;
   int dX = getSize ().width;
   int midY = dY/2;
   int midX =d X/2;

   // Set current drawing color
   g.setColor (Color.red);
   // Draw a rectangle centered at the mid-point
   g.drawRect (midX-22,midY-22,44,44);
   // Set a new drawing color
   g.setColor (Color.green);
   // Fill a rectangle centered at the mid-point
   // Put it within the previous rectangle so that
   // border shows.

   g.fillRect (midX-21,midY-21,42,42);
   // Set a new drawing color
   g.setColor (Color.blue);
   // Draw a circle around the mid-point
   g.drawOval (midX-20,midY-21,40,40);

  } // paint
} //
SimpleDraw1Applet

 

Note how we obtained the dimensions of the drawing area. The getSize() method returns an instance of the Dimension class, which provides direct access to its height and width variables. (As of Java 1.2, component includes the methods getHeight() and getWidth().)

 

Latest update: Oct. 27, 2004

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.