import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.sun.xml.internal.ws.api.server.Container;
public class Tossakan extends JFrame implements ActionListener{
int index = 0;
ImageIcon pic1 = new ImageIcon("beber.jpg");
ImageIcon pic2 = new ImageIcon("kaka.jpg");
ImageIcon q[] = {pic1,pic2};
String answer[] = {"Beber","Gaga"};
JTextField box = new JTextField(20);
java.awt.Container con = getContentPane();
JLabel showPic;
Tossakan(){
setLayout(null);
setBounds(0, 0, 600, 500);
setVisible(true);
Random ran = new Random();
index = ran.nextInt(2);
showPic = new JLabel(q[index]);
con.add(showPic);
con.add(box);
showPic.setBounds(10,100,256,256);
box.setBounds(300,100,300,250);
box.addActionListener(this);
}
public static void main(String[] args) {
new Tossakan();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==box){
if(box.getText().toLowerCase().equals(answer[index].toLowerCase())){
JOptionPane.showMessageDialog(null, "ถูกต้องนะคร๊าาาบบบบ");
con.remove(showPic);
repaint();
Random ran = new Random();
index = ran.nextInt(2);
showPic = new JLabel(q[index]);
con.add(showPic);
showPic.setBounds(10,100,256,256);
}
else{
box.setText("");
}
}
}
}
วันพฤหัสบดีที่ 1 กันยายน พ.ศ. 2554
จับเวลา
import javax.swing.JOptionPane;
public class Th extends Thread implements Runnable{
Th() throws InterruptedException{
for(int i = 1;i <= 200;i++)
{
System.out.println(i);
sleep(1000);
if(i==8){
JOptionPane.showMessageDialog(null, "Hurry");
}
if(i==15){
JOptionPane.showMessageDialog(null, "End");
System.exit(0);
}
}
}
public static void main(String[] args) throws InterruptedException {
new Th();
}
}
public class Th extends Thread implements Runnable{
Th() throws InterruptedException{
for(int i = 1;i <= 200;i++)
{
System.out.println(i);
sleep(1000);
if(i==8){
JOptionPane.showMessageDialog(null, "Hurry");
}
if(i==15){
JOptionPane.showMessageDialog(null, "End");
System.exit(0);
}
}
}
public static void main(String[] args) throws InterruptedException {
new Th();
}
}
SLOT MACHINE
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Game extends JFrame implements ActionListener{
Container con = getContentPane();
ImageIcon begin = new ImageIcon("wall.png");
ImageIcon p1 = new ImageIcon("slot1.jpg");
ImageIcon p2 = new ImageIcon("slot2.png");
ImageIcon p3 = new ImageIcon("slot3.png");
int score =50000;
JButton slot1 = new JButton("",begin);
JButton slot2 = new JButton("",begin);
JButton slot3= new JButton("",begin);
JButton spin= new JButton("spin");
Game(){
con.setBackground(Color.black);
setLayout(null);
setBounds(200, 10, 200, 400);
setVisible(true);
setTitle("Slot Machine");
setResizable(false);
con.add(slot1);
con.add(slot2);
con.add(slot3);
con.add(spin);
slot1.setBounds(10,50, 50, 50);
slot2.setBounds(60,50, 50, 50);
slot3.setBounds(110,50, 50, 50);
spin.setBounds(50,150, 60, 50);
spin.addActionListener(this);
}
public static void main(String[] args) {
new Game();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==spin){
Random ran = new Random();
int Rslot1 = ran.nextInt(3);
int Rslot2 = ran.nextInt(3);
int Rslot3 = ran.nextInt(3);
ImageIcon pp1 = null;
ImageIcon pp2 = null;
ImageIcon pp3 = null;
if(Rslot1==1){
pp1 = p1;
}
if(Rslot1==2){
pp1 = p2;
}if(Rslot1==3){
pp1 = p3;
}
if(Rslot2==1){
pp2 = p1;
}
if(Rslot2==2){
pp2 = p2;
}if(Rslot2==3){
pp2 = p3;
}
if(Rslot3==1){
pp3 = p1;
}
if(Rslot3==2){
pp3 = p2;
}if(Rslot3==3){
pp3 = p3;
}
con.remove(slot1);
con.remove(slot2);
con.remove(slot3);
repaint();
slot1 = new JButton("",pp1);
slot2 = new JButton("",pp2);
slot3= new JButton("",pp3);
con.add(slot1);
con.add(slot2);
con.add(slot3);
slot1.setBounds(10,50, 50, 50);
slot2.setBounds(60,50, 50, 50);
slot3.setBounds(110,50, 50, 50);
if(score<=0){
JOptionPane.showMessageDialog(null, "หมดตูด");
System.exit(0);
}
if(Rslot1 == Rslot2 && Rslot2 == Rslot3){
score = score+10000;
JOptionPane.showMessageDialog(null, "Jacpot"+score);
}
else{
score = score-10000;
JOptionPane.showMessageDialog(null, "สมน้ำหน้า"+score);
}
}
}
}
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Game extends JFrame implements ActionListener{
Container con = getContentPane();
ImageIcon begin = new ImageIcon("wall.png");
ImageIcon p1 = new ImageIcon("slot1.jpg");
ImageIcon p2 = new ImageIcon("slot2.png");
ImageIcon p3 = new ImageIcon("slot3.png");
int score =50000;
JButton slot1 = new JButton("",begin);
JButton slot2 = new JButton("",begin);
JButton slot3= new JButton("",begin);
JButton spin= new JButton("spin");
Game(){
con.setBackground(Color.black);
setLayout(null);
setBounds(200, 10, 200, 400);
setVisible(true);
setTitle("Slot Machine");
setResizable(false);
con.add(slot1);
con.add(slot2);
con.add(slot3);
con.add(spin);
slot1.setBounds(10,50, 50, 50);
slot2.setBounds(60,50, 50, 50);
slot3.setBounds(110,50, 50, 50);
spin.setBounds(50,150, 60, 50);
spin.addActionListener(this);
}
public static void main(String[] args) {
new Game();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==spin){
Random ran = new Random();
int Rslot1 = ran.nextInt(3);
int Rslot2 = ran.nextInt(3);
int Rslot3 = ran.nextInt(3);
ImageIcon pp1 = null;
ImageIcon pp2 = null;
ImageIcon pp3 = null;
if(Rslot1==1){
pp1 = p1;
}
if(Rslot1==2){
pp1 = p2;
}if(Rslot1==3){
pp1 = p3;
}
if(Rslot2==1){
pp2 = p1;
}
if(Rslot2==2){
pp2 = p2;
}if(Rslot2==3){
pp2 = p3;
}
if(Rslot3==1){
pp3 = p1;
}
if(Rslot3==2){
pp3 = p2;
}if(Rslot3==3){
pp3 = p3;
}
con.remove(slot1);
con.remove(slot2);
con.remove(slot3);
repaint();
slot1 = new JButton("",pp1);
slot2 = new JButton("",pp2);
slot3= new JButton("",pp3);
con.add(slot1);
con.add(slot2);
con.add(slot3);
slot1.setBounds(10,50, 50, 50);
slot2.setBounds(60,50, 50, 50);
slot3.setBounds(110,50, 50, 50);
if(score<=0){
JOptionPane.showMessageDialog(null, "หมดตูด");
System.exit(0);
}
if(Rslot1 == Rslot2 && Rslot2 == Rslot3){
score = score+10000;
JOptionPane.showMessageDialog(null, "Jacpot"+score);
}
else{
score = score-10000;
JOptionPane.showMessageDialog(null, "สมน้ำหน้า"+score);
}
}
}
}
วันพุธที่ 31 สิงหาคม พ.ศ. 2554
ตัวอย่าง เกม JAVA ง่ายๆ เกมทายเลข ลองเอาไปเป็น Ex ครับ
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class JJJ extends JFrame implements ActionListener{
JButton bt = new JButton("ok");
int r = -1;
JTextField tx = new JTextField(10000);
JJJ(){
Random t = new Random();
r = t.nextInt(1000);
System.out.print(r);
setLayout(null);
setVisible(true);
setBounds(10,10,500,500);
add(bt);add(tx);
tx.setText("0");
bt.addActionListener(this);
bt.setBounds(0, 0, 200, 30);
tx.setBounds(0, 50, 200, 30);
}
public static void main(String[] args) {
new JJJ();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bt){
int num = Integer.parseInt(tx.getText());
if(r>num){
JOptionPane.showMessageDialog(null, "low");
}
else if(r<num){
JOptionPane.showMessageDialog(null, "much");
}
else if(r==num){
JOptionPane.showMessageDialog(null, "win");
System.exit(0);
}
}
}
}
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class JJJ extends JFrame implements ActionListener{
JButton bt = new JButton("ok");
int r = -1;
JTextField tx = new JTextField(10000);
JJJ(){
Random t = new Random();
r = t.nextInt(1000);
System.out.print(r);
setLayout(null);
setVisible(true);
setBounds(10,10,500,500);
add(bt);add(tx);
tx.setText("0");
bt.addActionListener(this);
bt.setBounds(0, 0, 200, 30);
tx.setBounds(0, 50, 200, 30);
}
public static void main(String[] args) {
new JJJ();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bt){
int num = Integer.parseInt(tx.getText());
if(r>num){
JOptionPane.showMessageDialog(null, "low");
}
else if(r<num){
JOptionPane.showMessageDialog(null, "much");
}
else if(r==num){
JOptionPane.showMessageDialog(null, "win");
System.exit(0);
}
}
}
}
สมัครสมาชิก:
บทความ (Atom)