import java.awt.*; import java.awt.event.*; import java.util.*; /** BigPixelCanvas implements a Canvas that allows you to draw with big fat pixels. It is meant to be used by applets that illustrate basic line drawing and polygon filling. */ public class BigPixelCanvas extends Canvas { static final int radius = 8; //radius of circles used to represent pixels static final int pixelSize = 20; // space between each pixel static final int offset = 20; // offset of big pixel (0,0) from origin int maxx; // max x coord int maxy; // max y coord /* Pixels that have been filled */ Vector filled = new Vector(); /* polygon to draw in this canvas */ Polygon p = new Polygon(); /** Set the polygon to be drawn in this canvas @param p - the polygon to be drawn */ public void setPolygon(Polygon p) { this.p = p; repaint(); } /** Size this canvas would like to be */ public Dimension getPreferredSize(){ return(new Dimension(500,350)); } /** Map from virtual pixels to real ones @param x an x coordinate in fat pixel space @return x coordinate in regular pixel space */ public int toScreenX(int x) { return offset + x * pixelSize; } /** Map from virtual pixels to real ones @param y a y coordinate in fat pixel space @return y coordinate in regular pixel space */ public int toScreenY(int y) { return offset + (maxy - y) * pixelSize; } /** Map from real pixels to virtual ones @param x a coordinate in regular pixel space @return coordinate in fat pixel space */ public int fromScreenX(int x) { return (x+pixelSize/2-offset) / pixelSize; } /** Map from real pixels to virtual ones @param x a coordinate in regular pixel space @return coordinate in fat pixel space */ public int fromScreenY(int y) { return maxy - (y+pixelSize/2-offset) / pixelSize; } /** Erase all fat pixels */ public void clear() { filled.removeAllElements(); p = new Polygon(); repaint(); } /** Draw a single fat pixel. @param x x coordinate in fat pixel space @param y y coordinate in fat pixel space */ public void drawPixel(int x, int y) { drawPixelInternal(x,y); filled.addElement(new Point(x,y)); } /* Draw a pixel without saving it in filled */ private void drawPixelInternal(int x, int y) { getGraphics().fillOval(toScreenX(x)-radius,toScreenY(y)-radius,2*radius,2*radius); } /** Fill a row of pixels. @param startx starting x coordinate of row @param endx ending x coordinate of row @param y y coordinate of row */ public void fillSpan(int startx, int endx, int y) { for (int x = startx; x < endx; x++) { drawPixel(x,y); } } /** Paints the canvas. @param g - the Graphics object to use for painting */ public void paint(Graphics g) { FontMetrics fm = g.getFontMetrics(); maxx = (getSize().width-offset)/pixelSize-1; maxy = (getSize().height-offset)/pixelSize-1; /* vertical lines */ for (int x = 0; x <= maxx; x++) { g.setColor(Color.lightGray); g.drawLine(toScreenX(x),toScreenY(0),toScreenX(x),toScreenY(maxy)); String s = Integer.toString(x); g.setColor(Color.black); g.drawString(s,toScreenX(x)-fm.stringWidth(s)/2,toScreenY(0)+fm.getAscent()+1); } /* horizontal lines */ for (int y = 0; y <= maxy; y++) { g.setColor(Color.lightGray); g.drawLine(toScreenX(0),toScreenY(y),toScreenX(maxx),toScreenY(y)); String s = Integer.toString(y); g.setColor(Color.black); g.drawString(s,toScreenX(0)-fm.stringWidth(s)-1,toScreenY(y)+fm.getAscent()/2); } /* draw specified polygon */ Polygon sp = new Polygon(); for (int i = 0; i < p.npoints; i++){ sp.addPoint(toScreenX(p.xpoints[i]),toScreenY(p.ypoints[i])); } g.drawPolygon(sp); /* fill all pixels stored in Vector filled */ for (Enumeration e = filled.elements() ; e.hasMoreElements() ;) { Point p = (Point) e.nextElement(); drawPixelInternal(p.x,p.y); } } }