Document Sample
DrawingText Powered By Docstoc
					Drawing Text
                         A First Applet

      MyJApplet                    public class MyJApplet extends JApplet {
                                       public void paint (Graphics g) {
                                           g.drawString(“hello, world!”, 20, 100);
                       y = 100
       hello, world!
                                 A Graphics object is passed as a parameter,
                                   MyJApplet inherits method paint(
                                  MyJApplet overridesfrom JApplet )
                                 and the Graphics method drawString is

    x = 20                       The string “hello, world!” is drawn in a
                                 “box” whose upper left-hand corner is at
                                 coordinates (20, 100)
                        Class JApplet
 JApplet is a “heavyweight” class in javax.swing that inherits
 from its awt precursor (Applet). To create your own Applet
 you will first extend JApplet.

Class JApplet has a number of predefined methods that are
“inherited” by the class derived from the base class, JApplet.
Some of these methods are empty and will need to be
overridden by the the author of the derived class when used.
Two of these methods are init( ) and paint( ).

Method init( ) is used to “register” components such as JButton
and JTextField that are placed on an applet.

Method paint( ) is inherited from Component, and gives
instructions for coloring and drawing objects on the Component.

Class MyJApplet that we described in the second slide does not contain
any instance variables that have to be initialized nor any Component
objects that have to be registered. There is no need to override method
init( ).

Method paint( ) is called by the browser in which the applet is running
and causes the string “hello, world!” to be drawn on the screen.
                     The Anatomy of a Program
1. Browser runs applet -- executes method paint( )

 import java.awt.*;        //To include class Graphics into the program

 public class MyJApplet extends JApplet {
      public void paint (Graphics g) {
          g.drawString(“hello, world!”, 20, 100);             2. Call function drawString( )

                                                                 3. Fill in the arguments

  class Graphics {
      public void drawString(String s, int x, int y) {
            //code to draw the string at indicated location     4. Execute the method
      }      5. Return back to the calling location
                            Class Font

We can choose the name, size, and style of the font for the strings that
we paint on our applet. There are three basic fonts that should be
supported on any platform.

  Font Name                 Appearance                 Java Name
  Helvetica               Helvetica                   SansSerif

  TimesRoman               TimesRoman                 Serif
  Courier                  Courier                    Monospaced

There are three Font class constants that are used to specify the style of
the font.

Font.BOLD                 Font.ITALIC               Font.PLAIN
                               Class Font

You can create a new Font object using the Font constructor. (If you do
not specify a Font object, Java will choose a default font of its own.)

Font myFont = new Font(“Monospaced”, Font.BOLD, 20);

                             Font name         style       Size in pixel

g.drawString(“This string is in courier bold - 20 pixels”,10, 10);
                            Font Class

Alternatively one can construct a new Font object in the Graphics setFont( )

g.setFont(new Font(“SansSerif”,font.ITALIC, 18));

 You can combine styles by adding them.

g.setFont(new Font(“SansSerif”, font.BOLD + font.ITALIC, 12));
g.drawString(“I am Helvetica, Bold, and Italicized”,10, 20);
                             The Graphics Class
Class Graphics includes the following methods that are available for use
by an application programmer:
public void drawString (String s, int x, int y) { }
public void drawLine(int x1, int y1, int x2, int y2) { }

Produces a line that connects the two end-points
public void drawRect(int x, int y, int width, int height) { }
Parameters include the coordinates of the upper left-hand corner, width and length
public void fillRect (int x, int y, int width, int height) { }
Fills the rectangle with the current drawing color
public void setColor(Color c) { }

Sets the current drawing color as indicated

public void clearRect(int x, int y, int width, int height) { }

Clears area of rectangle by painting it the background color

          2 - dimensional shapes are drawn inside of a specified rectangular
          « bounds box ».

         drawOval (int x1, int y1, int width, int height) { }

     (x1, y1)         width


          2 - dimensional shapes are drawn inside of a specified rectangular
          « bounds box ».

         fillOval (int x1, int y1, int width, int height) { }

     (x1, y1)         width

       An arc is formed by drawing an oval between a start angle and a finish
       angle. The start angle is measured from the positive x-axis and is expressed
       in degrees. The end angle is expressed in degrees from the start angle.
       Angles extend from the center of the bounds box.

      drawArc(int x, int y, int width, int height, int startAngle,
          int endAngle) { }

        drawArc(20, 100, 200, 50, 210, 120);
     (20, 100)               200

                 210o                                120o

                               A smiley face

   Color is a class in the package java.awt. When drawing a picture we
   will need to create some Color objects.

 To draw an apple, we will need three colors: red for the fruit, brown for
 the stem, green for the leaf. We may create three Color objects by
 specifying the amount of each primary color (red, green, blue) we want to
 use – a n integer betwee 0 and 255.

 Color fruitColor = new Color(255,                 0,       0);

Type declaration                     amount of green and blue
                                 amount of red


          Memory for an object is dynamically allocated using the new operator
We may set the color in our Graphics object to that of our fruitColor:
      Color fruitColor = new Color(255, 0, 0);

Alternatively we can set the color in our graphics object using a color
constant provided by the class Color:


Other color constants include:
     black                                       cyan

       blue                                      magenta

       green                                     orange

       lightGray                                 pink

       gray                                      white

       yellow                                    darkGray
One may draw a blue rectangle using the following code in
method paint( ).

public class DrawRectangle extends JApplet {
    public void paint(Graphics g) {
        Color rectColor = new Color(0, 0, 255);
        g.fillRect(20, 20, 200, 100);

(20, 20)


Draw an Apple in your Applet
            Drawing an Apple in an Applet

In order to obtain the exact colors one wants for a particular
application, one should use Color constructors that take three integers
between 0 and 255 as parameters. These three integers represent the
amount of red, green, and blue components that are used in a color.

The color 0, 0, 0 is black,
The color 255, 255, 255 is white, and
The color 128, 128, 128 is medium gray.
                  Drawing an Apple in an Applet

To obtain the same colors as in our picture of the apple, we will need to
make the following declarations:

Color appleColor = new Color(238, 23, 30);
       The deep red color of the apple is 94% red, but contains some shadings of green
       and blue as well.

Color stemColor = new Color (92, 67, 35);
      A deep brown.

Color leafColor = new Color(23, 117, 37);

     A dark green.
Use the graphics methods we have previously listed to construct an
apple in an applet.
import javax.swing.*;
import java.awt.*;
public class AppleDrawing extends JApplet {
   public void paint(Graphics g) {
      //set the color of the outline to black, the color of the apple to red, and the stem to brown
      Color outline = new Color(0, 0, 0);
      Color appleColor = new Color(238,23,30); Color stemColor = new Color(92,67,35);
      Color leafColor = new Color(23, 117,37);
      g.setColor(appleColor); g.fillOval(75 ,30, 250, 350);
      g.setColor(stemColor); g.fillRect(195, 10, 10, 30);
      g.setColor(leafColor);       g.fillOval(205, 30, 50, 20);
       g.setColor(new Color(outline)); g.drawOval(75, 30, 250, 350);
       g.drawRect(195,10,10, 30); //stem
       g.drawOval(205, 30, 50, 20); //leaf
Use the graphics methods we have previously listed to construct a face
in an applet. Complete the following code.

import javax.swing.*;
import java.awt.*;
public class FaceDrawing extends JApplet {
    public void paint(Graphics g) {
        //add your own code here
        //construct a face as indicated on the next slide

Locate the eyes
relative to the
                                                                          The nose is formed by
bounds box for                                                             Bounds boxes for
                                                                          connecting three lines
the face.                                                                 Two bounds boxes to
                                                                           the the eyes --
                                                                          into atwo ears relative
                                                                          locate triangle. The
                                                                           draw a white oval
                                                                          to the the nose is in the
                                                                          top of face. The ears
                                                                           with of the face.
                                                                          middlebe blue that
                                                                          shoud a ovals or
                                                                          have the same color as
                                                                           brown circle
                                                                          the face and are drawn
                                                                          before the face.

                                                                          Draw an arc inside of a
                                                                          bounds box to form the
                                                                          mouth. You may make
                                                                          your face a smiley face
                                                                          or a sad face.

    You may add other facial features such as eyebrows, hair, a mustache, or goatee as you choose.

Shared By: