Page 221 - Introduction to Programming with Java: A Problem Solving Approach
P. 221

                5.9 GUI Track: Drawing Images, Lines, Rectangles, and Ovals in Java Applets (Optional) 187
 /*********************************************************************
*
GraphicsDemo.java
Dean & Dean
This defines a Java applet that displays an image and graphics.
*********************************************************************/
*
*
*
import java.awt.*;
// for Graphics, Image, and Color classes
import java.applet.Applet;
  public class GraphicsDemo extends Applet
{
 }
// end GraphicsDemo class
public void paint (Graphics g)
  {
}
Image image =
this.getImage(getDocumentBase(),"hurricanes.jpg");
// display smaller complete image in upper left corner of window
g.drawImage(image, 0, 0, 427, 284,
0, 0, 640, 427, this);
// establish color of all lines to be drawn
g.setColor(Color.BLUE);
      Apago PDF Enhancer
// draw rectangle around region to be expanded
g.drawRect(200, 60, 120, 120);
// topL, width
& height
// draw lines between corners of rectangles
g.drawLine(200, 60, 240, 240);
g.drawLine(320, 60, 600, 240);
g.drawLine(200, 180, 240, 600);
g.drawLine(320, 180, 600, 600);
// upper left
// upper right
// lower left
// lower right
// display expanded part of original image
g.drawImage(image, 240, 240, 600, 600, // destination
300, 90, 480, 270, this);
topL, botR
// source topL, botR
// draw rectangle around expanded part of image
g.drawRect(240, 240, 360, 360);
// topL, width & height
// create BLUE colored oval and write name on it
g.fillOval(520, 380, 45, 30);
g.setColor(Color.WHITE);
g.drawString("MAX", 530, 400);
// end paint
// topL, width & height
// change color for text
// string & start position
The extends
class heading allows this class to “borrow” the getImage method from the already- defined Java API Applet class.
Applet appended to the
// destination topL, botR
// source topL, botR
Figure 5.17 GraphicsDemo Java applet that illustrates graphics methods listed in Figure 5.15 This applet produces the output shown in Figure 5.16.
⎧⎪
⎪ ⎪ ⎨ ⎪ ⎪ ⎩


































   219   220   221   222   223