Java Reference
In-Depth Information
usually make use of graphics and colours, the next example illustrates some of these
facilities (though still in a rather artifi cial way)
Example
This example uses a combination of font selection, line drawing, rectangle draw-
ing, text placement and colour changes to give the reader a brief fl avour of what can
be done in applets. As might be expected, methods also exist for drawing arcs, cir-
cles, bar charts and a range of other shapes, but the purpose of this text is not to
provide comprehensive coverage of possible applet content. Rather, the intention is
to concentrate upon the network aspects of applets (i.e., how they may be created
and made accessible across a network, especially across the Internet). The code is
mostly self-explanatory, so little commenting is required.
Here's the code…
import java.awt.*;
import javax.swing.*;
public class SimpleGraphics extends JApplet
{
public void init()
{
ImagePanel pane = new ImagePanel();
//Make above panel the current content pane…
setContentPane(pane);
}
class ImagePanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setFont(
new Font("TimesRoman",Font.BOLD,36));
g.setColor(Color.blue);
g.drawString("Simple Applet Graphics",50,80);
g.setColor(Color.red);
g.drawLine(50,85,410,85);
g.setFont(
new Font("TimesRoman",Font.PLAIN,24));
g.setColor(Color.magenta);
g.drawString(
"Here's my message in a box",110,150);
g.setColor(Color.green);
g.drawRect(100,120,280,50);
}
}
}
Search WWH ::




Custom Search