-
-
Save anonymous/3e7731a0cb4c96647484 to your computer and use it in GitHub Desktop.
problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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