//Java Scratch Off Applet // Written by Dr. Roger Webster import java.applet.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class scratchoff extends Applet implements ActionListener { Frame window; TextArea textarea = new TextArea(5, 80); checkers mycheckers; boolean mousedragging = false; Image img; Panel mypanel = new Panel(); Button bigger = new Button("make larger scratches"); Button smaller = new Button("make smaller scratches"); public void init() { img = getImage( getDocumentBase(), "ms.jpg" ); window = new Frame ("Webster's Java Scratch off Window"); window.setSize (500, 500); textarea.setFont(new Font("Helvetica", Font.PLAIN, 12)); textarea.setEditable(false); //mypanel.add(textarea); bigger.addActionListener(this); smaller.addActionListener(this); mypanel.add(bigger); mypanel.add(smaller); mypanel.setBackground(Color.red); window.add("North", mypanel); textarea.appendText("hello Java Scratch Off Players\n"); mycheckers = new checkers(img); window.add("Center", mycheckers); window.show(); } public void stop() { window.dispose(); } public void actionPerformed(ActionEvent event) { Component source=(Component)event.getSource(); if (source == bigger ) { textarea.appendText("size is now bigger !\n"); mycheckers.bigger(); } if (source == smaller ) { textarea.appendText("size is now smaller !\n"); mycheckers.smaller(); } } class checkers extends Canvas { int grid = 8; int currentx=0, currenty=0; Color grey = new Color(165,165,165);//Board colors Color tan = new Color(255,255,204); //Board colors int nextx=0; int nexty=0; Graphics og; Image offScrImg; int imgWidth=20; // set this for how big you want to scratch off int imgHeight=20; // set this for how big you want to scratch off int width; int height; Image myimage; // Initialization function checkers(Image img) { super(); this.myimage = img; textarea.appendText("start sratching !\n"); width = size().width/grid; height = size().height/grid; } void cliptoaffectedarea (Graphics g, int oldx, int oldy, int newx, int newy, int width, int height) { int x = Math.min(oldx, newx); int y = Math.min(oldy, newy); int w = ( Math.max(oldx, newx) + width) - x; int h = ( Math.max(oldy, newy) + height) - y; g.clipRect(x,y,w,h); } public void update( Graphics g ) { if (offScrImg == null) offScrImg = createImage(size().width, size().height); og = offScrImg.getGraphics(); int lastx = currentx; int lasty = currenty; currentx = nextx; currenty = nexty; cliptoaffectedarea (og, lastx, lasty, currentx, currenty, imgWidth, imgHeight); // cliptoaffectedarea (g, lastx, lasty, currentx, currenty, imgWidth, imgHeight); paint(og); g.drawImage(offScrImg, 0, 0, this); og.dispose(); } // display the image public void paint( Graphics g ) { boolean shade = false; int PieceType; width = size().width/grid; height = size().height/grid; for (int y=0;y<=grid;y++) for (int x=0; x <= grid; x++) { g.setColor ( (shade = !shade) ? tan :grey); g.fillRect ( x * width, y * height, width, height); } // draw the original image g.drawImage( myimage, 0, 0, this ); } //end of paint() public void bigger () { imgHeight = imgHeight + 10; imgWidth = imgWidth + 10; textarea.appendText("size is now " + imgHeight + " !\n"); } public void smaller () { imgHeight = imgHeight - 10; imgWidth = imgWidth - 10; textarea.appendText("size is now " + imgHeight + " !\n"); } public boolean mouseDown (Event e, int x, int y) { nextx = x; nexty = y; currentx = x; currenty = y; mousedragging = true; repaint(); return true; } public boolean mouseUp (Event e, int x, int y) { mousedragging = false; return true; } public boolean mouseDrag(Event e, int x, int y) { nextx = x; nexty = y; currentx = x; currenty = y; mousedragging = true; repaint(); return true; } } }