62,614
社区成员
发帖
与我相关
我的任务
分享
/*
* FileName: MyFace.java
* Author: SageZk
* Date: 2008-06-08
*/
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class MyFace extends Canvas {
public static final int FACE_HAPPY = 1;
public static final int FACE_SAD = 2;
private int facestyle = FACE_HAPPY;
public MyFace(int face) {
if (face == FACE_HAPPY || face == FACE_SAD) this.facestyle = face;
}
public void paint(Graphics g) {
super.paint(g);
switch (this.facestyle) {
case FACE_HAPPY:
g.setColor(Color.black);
g.fillRect(0, 0, 400, 200);
g.setColor(Color.red);
g.fillOval(120, 10, 30, 30);
g.fillOval(230, 10, 30, 30);
g.drawArc(110, 100, 160, 100, 0, 180);
break;
case FACE_SAD:
g.setColor(Color.black);
g.fillRect(0, 0, 400, 200);
g.setColor(Color.red);
g.fillOval(120, 10, 30, 30);
g.fillOval(230, 10, 30, 30);
g.drawArc(110, 50, 160, 100, 180, 180);
break;
}
}
public void update(Graphics g) {
paint(g);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyApplets extends JApplet {
private JButton keyButton[] = new JButton[9];
int b = 0;
int c = 0;
public void init() {
String strLine = System.getProperty("line.separator");
JOptionPane.showMessageDialog(null, "Welcome to Mini-MineSweeper" + strLine
+ "The object of the game is click on all" + strLine
+ "the squares EXCEPT the one with the bomb." + strLine
+ "(There is only one" + " bomb).To choose a square" + strLine
+ "To display please simply click on the square.", "Message",
JOptionPane.INFORMATION_MESSAGE);
}
public MyApplets() {
Listener nListener = new Listener();
Listener2 bListener = new Listener2();
Container container = getContentPane();
container.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 3));
int k = (int)(Math.random() * 9);// set the Button and Bomb
for (int i = 0; i <= 8; i++) {
keyButton[i] = new JButton();
p1.add(keyButton[i]);
keyButton[i].setBackground(Color.gray);
if (i == k) {
keyButton[k].addActionListener(bListener);
} else {
keyButton[i].addActionListener(nListener);
}
}
container.add(p1, BorderLayout.CENTER);
}
public class Listener implements ActionListener { //the win listener
public void actionPerformed(ActionEvent e) {
JButton n = (JButton)e.getSource();
n.setBackground(Color.white);
c++;
if (c == 8) {
face2 a2 = new face2();// set the happy face
a2.setTitle("Boom!");
a2.setSize(400, 200);
a2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a2.setVisible(true);
Dimension screenSize2 = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth2 = screenSize2.width;
int screenHight2 = screenSize2.height;
int x2 = (screenWidth2 - a2.getWidth()) / 2;
int y2 = (screenHight2 - a2.getHeight()) / 2;
a2.setLocation(x2, y2);
setVisible(false);
int chose1 = JOptionPane.showConfirmDialog(null,
"Congratulations!You win!"
+ "Would you like to play again?",
"Message", JOptionPane.YES_NO_OPTION);// the yes
// or no
// choose
if (chose1 == JOptionPane.YES_OPTION) {
a2.setVisible(false);
c = 0;
JFrame MyApplet = new JFrame();// set the main frame
MyApplet.setTitle("Boom!");
MyApplet.setSize(400, 200);
MyApplet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyApplet.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit()
.getScreenSize(); // let the frame be
// center
int screenWidth = screenSize.width;
int screenHight = screenSize.height;
int x = (screenWidth - MyApplet.getWidth()) / 2;
int y = (screenHight - MyApplet.getHeight()) / 2;
MyApplet.setLocation(x, y);
} else System.exit(0);
}
}
}
public class Listener2 implements ActionListener { // the lose listener
public void actionPerformed(ActionEvent e) {
JButton n = (JButton)e.getSource();
n.setBackground(Color.black);
b++;
if (b == 1 && c != 8) {
face a1 = new face();// set the sad face
a1.setTitle("Boom!");
a1.setSize(400, 200);
a1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1.setVisible(true);
Dimension screenSize1 = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth1 = screenSize1.width;
int screenHight1 = screenSize1.height;
int x1 = (screenWidth1 - a1.getWidth()) / 2;
int y1 = (screenHight1 - a1.getHeight()) / 2;
a1.setLocation(x1, y1);
int chose = JOptionPane.showConfirmDialog(null, "BOOooM!!!"
+ "Would you like to play again?", "Message",
JOptionPane.YES_NO_OPTION);
setVisible(false);
if (chose == JOptionPane.YES_OPTION) {
a1.setVisible(false);
c = 0;
JFrame MyApplet = new JFrame();// set the main frame
MyApplet.setTitle("Boom!");
MyApplet.setSize(400, 200);
MyApplet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyApplet.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit()
.getScreenSize(); // let the frame be
// center
int screenWidth = screenSize.width;
int screenHight = screenSize.height;
int x = (screenWidth - MyApplet.getWidth()) / 2;
int y = (screenHight - MyApplet.getHeight()) / 2;
MyApplet.setLocation(x, y);
} else System.exit(0);
}
}
}
class face extends JFrame {// draw the happy face
public face() {
getContentPane().add(new Ovals());
}
class Ovals extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0, 0, 400, 200);
g.setColor(Color.red);
g.fillOval(120, 10, 30, 30);
g.fillOval(230, 10, 30, 30);
g.drawArc(110, 100, 160, 100, 0, 180);
}
}
}
class face2 extends JFrame { // draw the sad face
public face2() {
getContentPane().add(new Ovals2());
}
class Ovals2 extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0, 0, 400, 200);
g.setColor(Color.red);
g.fillOval(120, 10, 30, 30);
g.fillOval(230, 10, 30, 30);
g.drawArc(110, 50, 160, 100, 180, 180);
}
}
}
}