第四章

小程式和圖學(Graphics)

Console Application

Graphical Application

4.1 小程式(Applets)

網路伺服器(Web Server)與網頁瀏覽器(Browser)

網頁瀏覽器(Browser)

4.2 HTML簡介

HTML (HyperText Markup Language, 超文字標示語言)文件, 經由 HTTP ( HyperText Transfer Protocol) 網路通訊協定, 便能夠在世界各地透過 WWW (World Wide Web) 流通。

文字標籤

影像, 連結和小程式標籤

參考資料:
  1. 清大網路程式設計
  2. Intro to HTML

4.3 簡單小程式

Viewing an Applet

用Applet Viewer 執行 RectangleApplet

用瀏覽器執行 RectangleApplet

小程式類別 Outline

class MyApplet extends Applet
{
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
// add drawing operations
. . .
}
}

File RectangleApplet.java

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

/**
   An applet that draws two rectangles.
*/
public class RectangleApplet extends Applet
{
   public void paint(Graphics g)
   {
      // recover Graphics2D

      Graphics2D g2 = (Graphics2D)g;

      // construct a rectangle and draw it

      Rectangle cerealBox = new Rectangle(5, 10, 20, 30);
      g2.draw(cerealBox);

      // move rectangle 15 units sideways and 25 units down

      cerealBox.translate(15, 25);

      // draw moved rectangle

      g2.draw(cerealBox);
   }
}
示範:

4.4 圖形形狀

橢圓

線和點Lines and Points

4.5 顏色

4.6 字集

常用字集

標楷體

細明體

隸書體

基點(Basepoint) 和基線(Baseline)

4.7 畫複雜圖形用方格紙

The Car Drawer Applet

File CarApplet.java

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

/**
   This applet draws two car shapes.
*/
public class CarApplet extends Applet
{
   public void paint(Graphics g)
   {
      Graphics2D g2 = (Graphics2D)g;

      Car car1 = new Car(100, 100);
      Car car2 = new Car(200, 200);

      car1.draw(g2);
      car2.draw(g2);
   }
}

File Car.java

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
   A car shape that can be positioned anywhere on the screen.
*/
public class Car
{
   /**
      Constructs a car with a given top left corner
      @param x the x coordinate of the top left corner
      @param y the y coordinate of the top left corner
   */
   public Car(double x, double y)
   {
      xLeft = x;
      yTop = y;
   }

   /**
      Draws the car
      @param g2 the graphics context
   */
   public void draw(Graphics2D g2)
   {
      Rectangle2D.Double body
         = new Rectangle2D.Double(xLeft, yTop + 10, 60, 10);
      Ellipse2D.Double frontTire
         = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
      Ellipse2D.Double rearTire
         = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);

      // the bottom of the front windshield
      Point2D.Double r1
         = new Point2D.Double(xLeft + 10, yTop + 10);
      // the front of the roof
      Point2D.Double r2
         = new Point2D.Double(xLeft + 20, yTop);
      // the rear of the roof
      Point2D.Double r3
         = new Point2D.Double(xLeft + 40, yTop);
      // the bottom of the rear windshield
      Point2D.Double r4
         = new Point2D.Double(xLeft + 50, yTop + 10);

      Line2D.Double frontWindshield
         = new Line2D.Double(r1, r2);
      Line2D.Double roofTop
         = new Line2D.Double(r2, r3);
      Line2D.Double rearWindshield
         = new Line2D.Double(r3, r4);

      g2.draw(body);
      g2.draw(frontTire);
      g2.draw(rearTire);
      g2.draw(frontWindshield);
      g2.draw(roofTop);
      g2.draw(rearWindshield);
   }

   private double xLeft;
   private double yTop;
}
示範:

4.8 讀取文句輸入

附警告標示的 Applet 輸入視窗

File ColorApplet.java

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JOptionPane;

/**
   An applet that lets a user choose a color by specifying
   the fractions of red, green, and blue.
*/
public class ColorApplet extends Applet
{
   public ColorApplet()
   {
      String input;

      // ask the user for red, green, blue values

      input = JOptionPane.showInputDialog("red:");
      float red = Float.parseFloat(input);

      input = JOptionPane.showInputDialog("green:");
      float green = Float.parseFloat(input);

      input = JOptionPane.showInputDialog("blue:");
      float blue = Float.parseFloat(input);

      fillColor = new Color(red, green, blue);
   }

   public void paint(Graphics g)
   {
      Graphics2D g2 = (Graphics2D)g;

      // select color into graphics context

      g2.setColor(fillColor);

      // construct and fill a square whose center is
      // the center of the window

      Rectangle square = new Rectangle(
         (getWidth() - SQUARE_LENGTH) / 2,
         (getHeight() - SQUARE_LENGTH) / 2,
         SQUARE_LENGTH,
         SQUARE_LENGTH);

      g2.fill(square);
   }

   private static final int SQUARE_LENGTH = 100;

   private Color fillColor;
}
示範:

4.9 圖示和數值資訊的比較

圓和直線的交點

File IntersectionApplet.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;

/**
   An applet that computes and draws the intersection points
   of a circle and a line.
*/
public class IntersectionApplet extends Applet
{
   public IntersectionApplet()
   {
      String input
         = JOptionPane.showInputDialog("x:");
      x = Integer.parseInt(input);
   }

   public void paint(Graphics g)
   {
      Graphics2D g2 = (Graphics2D)g;

      double r = 100; // the radius of the circle

      // draw the circle

      Ellipse2D.Double circle
         = new Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS);
      g2.draw(circle);

      // draw the vertical line

      Line2D.Double line
         = new Line2D.Double(x, 0, x, 2 * RADIUS);
      g2.draw(line);

      // compute the intersection points

      double a = RADIUS;
      double b = RADIUS;

      double root = Math.sqrt(RADIUS * RADIUS - (x - a) * (x - a));
      double y1 = b + root;
      double y2 = b - root;

      // draw the intersection points

      LabeledPoint p1 = new LabeledPoint(x, y1);
      LabeledPoint p2 = new LabeledPoint(x, y2);

      p1.draw(g2);
      p2.draw(g2);
   }

   private static final double RADIUS = 100;
   private double x;
}
示範:

4.10 坐標變換(Coordinate Transformations)

溫度圖

File ChartApplet.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

/**
   This applet draws a chart of the average monthly
   temperatures in Phoenix, AZ.
*/
public class ChartApplet extends Applet
{
   public void paint(Graphics g)
   {
      Graphics2D g2 = (Graphics2D)g;

      month = 1;

      drawBar(g2, JAN_TEMP);
      drawBar(g2, FEB_TEMP);
      drawBar(g2, MAR_TEMP);
      drawBar(g2, APR_TEMP);
      drawBar(g2, MAY_TEMP);
      drawBar(g2, JUN_TEMP);
      drawBar(g2, JUL_TEMP);
      drawBar(g2, AUG_TEMP);
      drawBar(g2, SEP_TEMP);
      drawBar(g2, OCT_TEMP);
      drawBar(g2, NOV_TEMP);
      drawBar(g2, DEC_TEMP);
   }

   /**
      Draws a bar for the current month and increments
      the month.
      @param g2 the graphics context
      @param temperature the temperature for the month
   */
   public void drawBar(Graphics2D g2, int temperature)
   {
      Line2D.Double bar
         = new Line2D.Double(xpixel(month), ypixel(0),
            xpixel(month), ypixel(temperature));

      g2.draw(bar);

      month++;
   }

   /**
      Converts from user coordinates to pixel coordinates
      @param xuser an x-value in user coordinates
      @return the corresponding value in pixel coordinates
   */
   public double xpixel(double xuser)
   {
      return (xuser - XMIN) * (getWidth() - 1) / (XMAX - XMIN);
   }

   /**
      Converts from user coordinates to pixel coordinates
      @param yuser a y-value in user coordinates
      @return the corresponding value in pixel coordinates
   */
   public double ypixel(double yuser)
   {
      return (yuser - YMAX) * (getHeight() - 1) / (YMIN - YMAX);
   }

   private static final int JAN_TEMP = 11;
   private static final int FEB_TEMP = 13;
   private static final int MAR_TEMP = 16;
   private static final int APR_TEMP = 20;
   private static final int MAY_TEMP = 25;
   private static final int JUN_TEMP = 31;
   private static final int JUL_TEMP = 33;
   private static final int AUG_TEMP = 32;
   private static final int SEP_TEMP = 29;
   private static final int OCT_TEMP = 23;
   private static final int NOV_TEMP = 16;
   private static final int DEC_TEMP = 12;

   private static final double XMIN = 1;
   private static final double XMAX = 12;
   private static final double YMIN = 0;
   private static final double YMAX = 40;

   private int month;
}

示範: