Skip to content

Instantly share code, notes, and snippets.

/Coin

Created May 21, 2014 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3e7731a0cb4c96647484 to your computer and use it in GitHub Desktop.
Save anonymous/3e7731a0cb4c96647484 to your computer and use it in GitHub Desktop.
problem
package twelve;
import java.awt.Image;
import java.awt.Graphics;
public class Coin {
private Image heads;
private Image tails;
private char side;
public Coin(Image h, Image t){
this.heads = h;
this.tails = t;
side = 'h';
}
public void flip(){
if (side == 'h'){
side = 'j';
}
else side = 'h';
}
public void draw(Graphics g, int x, int y){
if (side == 'h'){
g.drawImage(heads, x, y, null);
}
else g.drawImage(tails, tails.getHeight(null), tails.getWidth(null), null);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CoinTest extends JPanel
implements ActionListener
{
private Coin coin;
public CoinTest ()
{
Image heads = (new ImageIcon("heads.gif")).getImage();
Image tails = (new ImageIcon("tails.gif")).getImage();
coin = new Coin(heads, tails);
Timer clock = new Timer(2000, this);
clock.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x = getWidth() / 2;
int y = getHeight() / 2;
coin.draw(g, x, y);
}
public void actionPerformed(ActionEvent e)
{
coin.flip();
repaint();
}
public static void main(String[] args)
{
JFrame w = new JFrame("Flipping coin");
w.setSize(300, 300);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CoinTest panel = new CoinTest();
panel.setBackground(Color.WHITE);
Container c = w.getContentPane();
c.add(panel);
w.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment