您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 7

Example of changing the colour of the current graphics context

Extending the previous example, we can create an applet to draw blue text in its window to produce the following output:

by simply adding a statement to change the colour of the graphics context before drawing the string.

So we must send a setColor() message to the Graphics object before sending the drawString() message:

import java.applet.Applet;

import java.awt.*;

public class DrawString2 extends Applet
{

  public void paint( Graphics g )
  {
    g.setColor( Color.blue );
    g.drawString( "text drawn with Graphics method 'drawString()'",
       20, 20 );
  } 

} // class 

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg