|
import java.awt.*; |
|
import java.awt.Canvas; |
|
import java.awt.Container; |
|
import java.awt.Graphics; |
|
import java.awt.Rectangle; |
|
import java.awt.event.*; |
|
import java.io.*; |
|
import javax.sound.sampled.*; |
|
import javax.swing.JButton; |
|
import javax.swing.JFrame; |
|
|
|
public class HiddenMickey extends Canvas { |
|
private static boolean drawMickey = false; |
|
|
|
public static void main(String[] args) { |
|
JFrame frame = new JFrame("Hidden Mickey"); |
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit the application when the frame is closed (the default behaviour hides the frame and keeps the app running :/ ) |
|
Canvas canvas = new HiddenMickey(); |
|
canvas.setSize(400, 400); |
|
frame.add(canvas); |
|
JButton mainButton = new JButton("Draw Mickey"); |
|
mainButton.addActionListener(new ActionListener() |
|
{ |
|
public void actionPerformed(ActionEvent e) |
|
{ |
|
drawMickey = true; |
|
try{ |
|
AudioInputStream playit = AudioSystem.getAudioInputStream(new File("your_sound_byte_here.aiff")); |
|
Clip laugh = AudioSystem.getClip(); |
|
laugh.open(playit); |
|
laugh.start(); |
|
}catch(Exception ex){ |
|
ex.printStackTrace(); |
|
} |
|
canvas.repaint(); |
|
} |
|
}); |
|
frame.add(mainButton); |
|
frame.setPreferredSize(new Dimension(400, 500)); |
|
frame.pack(); |
|
frame.setLayout(new FlowLayout()); |
|
frame.setVisible(true); |
|
} |
|
|
|
public void boxOval(Graphics g, Rectangle bb) { |
|
g.fillOval(bb.x, bb.y, bb.width, bb.height); |
|
} |
|
|
|
public void mickey(Graphics g, Rectangle bb) { |
|
boxOval(g, bb); |
|
|
|
int dx = bb.width / 2; |
|
int dy = bb.height / 2; |
|
Rectangle half = new Rectangle(bb.x, bb.y, dx, dy); |
|
|
|
half.translate(-dx / 2, -dy / 2); |
|
boxOval(g, half); |
|
|
|
half.translate(dx * 2, 0); |
|
boxOval(g, half); |
|
} |
|
|
|
public void paint(Graphics g) { |
|
if (drawMickey) { |
|
Rectangle box = new Rectangle(100, 100, 200, 200); |
|
mickey(g, box); |
|
} |
|
} |
|
} |