// An applet that demonstrates most of the graphics primitives in java.awt.Graphics. import java.applet.*; import java.awt.*; import java.io.*; public class GraphicsExample extends Applet { Image image; // Initialize the applet public void init() { setBackground(Color.lightGray); try{ image = getImage(getDocumentBase(),"n_face.gif"); } catch (IOException e) { System.err.println("error"); System.exit(1); } } // Draw the applet whenever necessary public void paint(Graphics g) { Color fill = new Color(140, 140, 255); Color outline = Color.blue; int xString=60; int xfig=15; int xfig1=xfig+155; int xString1=xString+ 155; int xfig2=xfig1+160; int xString2=xString1+ 160; // Paint Rectangle with interpolated color Color C1 = new Color(250, 250, 120); Color C2 = new Color(120,120, 250); FillRect(xfig1,5,xfig1+155, 95, C1, C2, g); // Draw a line g.setColor(Color.black); g.drawLine(25, 10, 150, 80); g.drawLine(xfig1+20, 80, xfig1+140, 20); g.setColor(Color.red); g.fillOval(xfig1+20-3, 80-3, 6, 6); //dot for end point g.fillOval(xfig1+140-3, 20-3, 6, 6); // Draw a rectangle g.setColor(fill); g.fillRect(xfig, 110, 150, 80); g.setColor(outline); g.drawRect(xfig, 110, 150, 80); // Draw a rounded rectangle g.setColor(fill); g.fillRoundRect(xfig1, 110, 150, 80, 20, 20); g.setColor(outline); g.drawRoundRect(xfig1, 110, 150, 80, 20, 20); // Draw a 3D rectangle (clear an area for it first) g.setColor(Color.gray); g.clearRect(xfig2, 105, 160, 90); g.setColor(fill); g.draw3DRect(xfig2+2, 110, 150, 80, true); // g.setColor(Color.white); // g.fill3DRect(xfig2+8,114, 144, 72, true); // Draw an oval g.setColor(fill); g.fillOval(xfig, 210, 150, 80); g.setColor(outline); g.drawOval(xfig, 210, 150, 80); // Draw an arc g.setColor(fill); g.fillArc(xfig1, 210, 150, 80, 90, 135); g.setColor(outline); g.drawArc(xfig1, 210, 150, 80, 90, 135); // Draw a polygon int numpoints = 9; int[] xpoints = new int[numpoints+1]; int[] ypoints = new int[numpoints+1]; for(int i=0; i < numpoints; i++) { double angle = 2*Math.PI * i / numpoints; xpoints[i] = (int)(xfig2+80 + 50*Math.cos(angle)); ypoints[i] = (int)(250 - 40*Math.sin(angle)); } g.setColor(fill); g.fillPolygon(xpoints, ypoints, numpoints); g.setColor(outline); xpoints[numpoints] = xpoints[0]; ypoints[numpoints] = ypoints[0]; g.drawPolygon(xpoints, ypoints, numpoints+1); // Center and draw an image int w = image.getWidth(this); int h = image.getHeight(this); g.drawImage(image, xfig2+w/2, 10 + (80-h)/2, this); g.setColor(Color.black); g.drawString("fillRect()", xString, 140); g.drawString("drawRect()", xString, 150); g.drawString("fillRoundRect()", xString1, 140); g.drawString("drawRoundRect()", xString1, 150); g.drawString("draw3DRect()", xString2, 155); g.drawString("fillOval()", xString, 240); g.drawString("drawOval()", xString, 250); g.drawString("fillArc()", xString1, 240); g.drawString("drawArc()", xString1, 250); g.drawString("fillPolygon()", xString2, 245); g.drawString("drawPolygon()", xString2, 255); g.drawString("drawLine()", xString,60); g.drawString("drawImage()", xString2, 100); } // Utility method to tile an image protected void FillRect(int x1,int y1,int x2,int y2, Color C1,Color C2,Graphics g) { for(int y = y1; y < y2; y ++) { double t = (double)(y-y1)/(double)(y2-y1); int red = (int)((double)(C2.getRed()-C1.getRed())*t) + C1.getRed(); int grn = (int)((double)(C2.getGreen()-C1.getGreen())*t) + C1.getGreen(); int blu = (int)(((double)C2.getBlue()-C1.getBlue())*t) + C1.getBlue(); g.setColor(new Color(red, grn, blu)); for(int x = x1; x < x2; x ++) g.drawLine(x, y, x,y); } } }