// Tegn M-figur. import java.applet.Applet; import java.awt.*; public class Polygon2 extends Applet { public void paint(Graphics g) { drawM(g); drawM(g, 100, 50); drawM(g, 200, 50, 3.0); drawM(g, 50, 200, 2.0, Color.green); drawM(g, 300, 200, 4.0, Color.red); } static int[] xs = { 0, 20, 40, 40, 0 }; static int[] ys = { 0, 20, 0, 40, 40 }; static void drawM(Graphics g) { g.fillPolygon(xs, ys, 5); } // Følgende (overlæssede!) metoder er løsninger til opgaver fra // forelæsningen: static void drawM(Graphics g, int x0, int y0) { int[] xsny = new int[5]; int[] ysny = new int[5]; for (int i=0; i<5; i=i+1) { xsny[i] = xs[i] + x0; ysny[i] = ys[i] + y0; } g.fillPolygon(xsny, ysny, 5); } static void drawM(Graphics g, int x0, int y0, double scale) { int[] xsny = new int[5]; int[] ysny = new int[5]; for (int i=0; i<5; i=i+1) { xsny[i] = (int)(scale * xs[i] + x0); ysny[i] = (int)(scale * ys[i] + y0); } g.fillPolygon(xsny, ysny, 5); } static void drawM(Graphics g, int x0, int y0, double scale, Color c) { int[] xsny = new int[5]; int[] ysny = new int[5]; for (int i=0; i<5; i=i+1) { xsny[i] = (int)(scale * xs[i] + x0); ysny[i] = (int)(scale * ys[i] + y0); } g.setColor(c); g.fillPolygon(xsny, ysny, 5); } }