|
/** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright © 2005</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */ public class TicketPanel extends JPanel implements MouseInputListener { /** Ticket image (background). */ private BufferedImage ticketImage; /** Source winnings image. */ private BufferedImage winningsImage; /** Source scratch image (same size as winnings image). */ private BufferedImage scratchSourceImage; /** Scratch mask (same size as source scratch image). */ private BufferedImage scratchMaskImage; /** Current scratch image, contains scratched areas (same size as source scratch image). */ private BufferedImage scratchImage; /** Active scratch area (same size as source scratch image). */ private Rectangle scratchArea; /** Creates a new instance. * @exception IOException In case of I/O error. */ public TicketPanel() throws IOException { super(); ticketImage = ImageIO.read(new File("Ticket.png")); setPreferredSize(new Dimension(ticketImage.getWidth(), ticketImage.getHeight())); winningsImage = ImageIO.read(new File("Winnings.png")); scratchSourceImage = ImageIO.read(new File("Scratch.png")); scratchMaskImage = new BufferedImage(scratchSourceImage.getWidth(), scratchSourceImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY); // Initial mask is completly white (opaque). { Graphics2D graphics = scratchMaskImage.createGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, scratchMaskImage.getWidth(), scratchMaskImage.getHeight()); graphics.dispose(); } scratchImage = new BufferedImage(scratchSourceImage.getWidth(), scratchSourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB); // Initially the ticket is not scratched. { Graphics2D graphics = scratchImage.createGraphics(); graphics.drawImage(scratchSourceImage, 0, 0, null); graphics.dispose(); } // Active area (mouse clicks and drags valid within this rectangle. scratchArea = new Rectangle(10, 170, scratchSourceImage.getWidth(), scratchSourceImage.getHeight()); addMouseListener(this); addMouseMotionListener(this); } /** Update the current scratch. * @param x Current X location (screen/ticket coordinates). * @param y Current Y location (screen/ticket coordinates). */ private void maybeUpdateScratch(int x, int y) { if (scratchArea.contains(x, y)) { updateScratch(x - scratchArea.x, y - scratchArea.y); repaint(); } } /** Update the current scratch. * @param x Current X location (scratch coordinates). * @param y Current Y location (scratch coordinates). */ private void updateScratch(int x, int y) { // Update mask. Graphics2D graphics = scratchMaskImage.createGraphics(); graphics.setColor(Color.BLACK); graphics.fillOval(x - 15, y - 15, 30, 30); graphics.dispose(); // Update scratch image pixels. int width = scratchArea.width; int height = scratchArea.height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { // We take RBG from the source scratch image. int srcRGB = scratchSourceImage.getRGB(i, j); // But we take alpha from the scratch mask. int maskRGB = scratchMaskImage.getRGB(i, j); // We combine both values to make the current scratch. int dstRGB = ((srcRGB & 0xFFFFFF) | ((maskRGB & 0xFF) << 24)); scratchImage.setRGB(i, j, dstRGB); } } } /** {@inheritDoc} */ @Override protected void paintComponent(Graphics graphics) { super.paintComponent(graphics); graphics.drawImage(ticketImage, 0, 0, null); graphics.drawImage(winningsImage, scratchArea.x, scratchArea.y, null); graphics.drawImage(scratchImage, scratchArea.x, scratchArea.y, null); } /** Self-test main. * @param args Arguments from the command line. */ public static void main(String ...args) { try { TicketPanel ticket = new TicketPanel(); JFrame frame = new JFrame("Scatch !!!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(ticket, BorderLayout.CENTER); frame.pack(); frame.setResizable(false); frame.setVisible(true); } catch (Throwable t) { t.printStackTrace(); } } /////////////////////// // Event management. // /////////////////////// /** {@inheritDoc} */ public void mouseClicked(MouseEvent event) { maybeUpdateScratch(event.getX(), event.getY()); } /** {@inheritDoc} */ public void mousePressed(MouseEvent event) { } /** {@inheritDoc} */ public void mouseReleased(MouseEvent event) { } /** {@inheritDoc} */ public void mouseEntered(MouseEvent event) { } /** {@inheritDoc} */ public void mouseExited(MouseEvent event) { } /** {@inheritDoc} */ public void mouseDragged(MouseEvent event) { maybeUpdateScratch(event.getX(), event.getY()); } /** {@inheritDoc} */ public void mouseMoved(MouseEvent event) { }
|