/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.pegadi.artis; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; public class EasterGlassPane extends JComponent implements ActionListener,MouseListener { javax.swing.Timer animator; ImageIcon icon[] = new ImageIcon[1]; Font font1 = new Font("Sans Serif", Font.PLAIN, 13); int tmpScale; final static int numImages = 1; double x[] = new double[numImages]; double y[] = new double[numImages]; int xh[] = new int[numImages]; int yh[] = new int[numImages]; String stuf[] = {"survival guide", "erotic novel", "love letter", "great article", "movie script", "biography", "fantasy novel", "thriller", "legislation"}; String stuffern; String stuffernArticle; double scale[] = new double[numImages]; EasterRandom rand = new EasterRandom(); long start; public EasterGlassPane() { } public void go() { start = System.currentTimeMillis(); addMouseListener(this); setVisible(true); icon[0] = new ImageIcon(getClass().getResource("/images/pegadi_assistant.png")); scale[0] = 1.0; y[0] = 300; stuffern = stuf[(int)(Math.random()*stuf.length)]; char acter = stuffern.charAt(0); stuffernArticle = (acter=='a'|| acter=='e'|| acter=='i'|| acter=='o'|| acter=='u'|| acter=='A'|| acter=='E'|| acter=='I'|| acter=='O'|| acter=='U') ? "an" : "a"; animator = new javax.swing.Timer(22 + 22 + 22, this); animator.start(); } public void stop() { animator.stop(); removeMouseListener(this); setVisible(false); } public void paint(Graphics g) { for(int i = 0; i < numImages; i++) { if(x[i] > 3*i) { x[i] += 0.025; y[i] += 0.1; yh[i] = (int) (Math.sin(y[i])*20)-icon[0].getIconHeight()+450; xh[i] = (int) (Math.sin(x[i])*20)-icon[0].getIconWidth()+500; if(isVisible()) { g.drawImage(icon[0].getImage(), xh[0], yh[0],icon[0].getIconWidth(), icon[0].getIconHeight(), this); g.setFont(font1); g.drawString(stuffernArticle + " " + stuffern + ".", xh[0]+6, yh[0]+49); g.drawString(stuffern, xh[0]+26, yh[0]+132); g.drawString(stuffern, xh[0]+28, yh[0]+166); } } else { x[i] += .05; y[i] += .05; } } } class EasterRandom extends Random { public int nextInt(int n) { if (n<=0) throw new IllegalArgumentException("n must be positive"); if ((n & -n) == n) // i.e., n is a power of 2 return (int)((n * (long)next(31)) >> 31); int bits, val; do { bits = next(31); val = bits % n; } while(bits - val + (n-1) < 0); return val; } } public void nudge(int i) { x[i] += (double) rand.nextInt(1000) / 8756; y[i] += (double) rand.nextInt(1000) / 5432; int tmpScale = (int) (Math.abs(Math.sin(x[i])) * 10); scale[i] = (double) tmpScale / 10; int nudgeX = (int) (((double) getWidth()/2) * .8); int nudgeY = (int) (((double) getHeight()/2) * .60); xh[i] = (int) (Math.sin(x[i])) * nudgeX + nudgeX; yh[i] = (int) (Math.sin(y[i])) * nudgeY + nudgeY; } public void squish(Graphics g, ImageIcon icon, int x, int y, double scale) { if(isVisible()) { g.drawImage(icon.getImage(), x, y, (int) (icon.getIconWidth()*scale), (int) (icon.getIconHeight()*scale), this); } } public void actionPerformed(ActionEvent e) { // Stop animation after 20 secs if(System.currentTimeMillis()-start > 20000) { stop(); return; } if(isVisible()) { repaint(); } else { animator.stop(); } } public void mouseClicked(MouseEvent e) { stop(); } public void mouseEntered(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }