โปรแกรมบวกเลข 1 ถึง 9

java-add-number
โปรแกรมบวกเลข 1 ถึง 9 เขียนเล่น ๆ ขำ ๆ ไม่มีไรทำ เบื่อ ๆ เซ่ง ๆ พอดีไปเจอโจทย์ของรุ่นน้อง เขียนใน vb.net เขียนใน java มันกว่าเยอะเลย 555+ บวกได้อย่างเดียว – * / ไม่ได้ หุหุ
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Code15092550 extends JFrame implements ActionListener {
JPanel p1,p2,p3,p4;
JLabel label1,label2;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12;
boolean firstNumber = true;
int sumNumber = 0;

public static void main(String args[]){
new Code15092550();
}

public Code15092550(){
label1 = new JLabel(“0″,JLabel.RIGHT);
b1 = new JButton(“1″); b1.addActionListener(this);
b2 = new JButton(“2″); b2.addActionListener(this);
b3 = new JButton(“3″); b3.addActionListener(this);
b4 = new JButton(“4″); b4.addActionListener(this);
b5 = new JButton(“5″); b5.addActionListener(this);
b6 = new JButton(“6″); b6.addActionListener(this);
b7 = new JButton(“7″); b7.addActionListener(this);
b8 = new JButton(“8″); b8.addActionListener(this);
b9 = new JButton(“9″); b9.addActionListener(this);
b10 = new JButton(“0″); b10.addActionListener(this);
b11 = new JButton(“Clear”); b11.addActionListener(this);
b12 = new JButton(“=”); b12.addActionListener(this);

p1 = new JPanel(new GridLayout(0,3));
p1.add(b1); p1.add(b2); p1.add(b3);
p1.add(b4); p1.add(b5); p1.add(b6);
p1.add(b7); p1.add(b8); p1.add(b9);
p1.add(b10); p1.add(b11); p1.add(b12);

p2 = new JPanel(new BorderLayout());
p2.add(label1,BorderLayout.NORTH);
p2.add(p1,BorderLayout.CENTER);
getContentPane().add(p2);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(“โปรแกรมบวกเลข 1 ถึง 9″);
setResizable(false);
setLocation(50,50);
setSize(200,170);
setVisible(true);
show(true);
}

public void actionPerformed(ActionEvent ae){
if(ae.getSource() == b1){  inputNumber(1);
}else if(ae.getSource() == b2){ inputNumber(2);
}else if(ae.getSource() == b3){ inputNumber(3);
}else if(ae.getSource() == b4){ inputNumber(4);
}else if(ae.getSource() == b5){ inputNumber(5);
}else if(ae.getSource() == b6){ inputNumber(6);
}else if(ae.getSource() == b7){ inputNumber(7);
}else if(ae.getSource() == b8){ inputNumber(8);
}else if(ae.getSource() == b9){ inputNumber(9);
}else if(ae.getSource() == b10){inputNumber(0);
}else if(ae.getSource() == b11){
firstNumber=true; sumNumber=0; label1.setText(“0″);
}else if(ae.getSource() == b12){ label1.setText(String.valueOf(sumNumber));
}
}

public void inputNumber(int s){
if(firstNumber){
firstNumber = false;
label1.setText(String.valueOf(s));
}else{
label1.setText(label1.getText()+”+”+s);
}
sumNumber += s;
}
}

Java Slide Puzzle Game

เกมส์ Slide Puzzle Game คับ โค้ดต้นฉบับเป็น JavaScript คับ ผมเอาแก้เขียนใหม่เป็น Java Appication มีคนอยากได้ พอดีว่าว่างๆอยู่เลยทำให้ เห็นว่า algorithm ของเกมส์ น่าสนใจดีคับ ผมทำโค้ดต้นฉบับหายอ่ะคับ ไม่สามารถอ้างอิงได้ว่าโค้ดต้นฉบับมาจากไหน แต่ผมที่แน่ๆต้นฉบับโค้ดที่เป็น JavaScrpt ผมไม่ได้เขียนเอง อิอิ Copy And Develop คับ ผมเขียนทิ้งไว้นานแล้ว เอาขึ้นมาไว้ เผื่อว่ามีน้องๆ อยากได้ไปศึกษาคับ

โค้ดเหมือนกาน แต่เปลี่ยนแค่ที่รูปภาพคับ
1
2
ตอนยังไม่ได้เริ่มเกมส์ จะออกมาเป็นงี้คับ
3
พอเล่นจบชนะแล้ว จะออกมาเป็นงี้คับ
4

มาดู Source Code กันคับ

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class SlidePuzzleGame extends JFrame implements ActionListener{

Frame mainFrame;
JButton btn[][],btnStart,btnBlank,btnGo;
JPanel panel1,panel2,panel3;
JLabel label1;
Icon imgBlank;
String imgSrc[][];

boolean blnCongrat = true;
boolean blnNoStart = true;
boolean blnIsFirst = true;

public SlidePuzzleGame() {
panel1 = new JPanel(new GridLayout(0,5));

imgSrc = new String[5][5];
btn = new JButton[5][5];

for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
btn[i][j] = new JButton("",new ImageIcon("images/untitled(" +(i+1)+","+(j+1)+").png"));
imgSrc[i][j] = btn[i][j].getIcon().toString();
panel1.add(btn[i][j]);
btn[i][j].setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
btn[i][j].addActionListener(this);
}
}

btn[4][4].setIcon(new ImageIcon("images/blank.png"));
btnStart = new JButton("Start Game");
btnStart.addActionListener(this);

panel2 = new JPanel(new FlowLayout());
panel2.add(btnStart);

panel3 = new JPanel(new BorderLayout());
panel3.add(panel1, BorderLayout.CENTER);
panel3.add(panel2, BorderLayout.SOUTH);

this.getContentPane().add(panel3);
this.pack();
this.setLocation(50,50);
this.setResizable(false);
this.setTitle("Slide Puzzle Game");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void swapImg(JButton btn1,JButton btn2){
Icon tmp;
tmp = btn1.getIcon();
btn1.setIcon(btn2.getIcon());
btn2.setIcon(tmp);
}

public void slide(int row,int col){
if(blnNoStart){
JOptionPane.showMessageDialog(null,"กรุณากดปุ่ม Start เพื่อเริ่มเกมส์","Slide Puzzle Game",JOptionPane.OK_OPTION);
}else{

if(row>0){
if(btn[row-1][col].getIcon() == imgBlank){
swapImg(btn[row-1][col],btn[row][col]);
}
}

if(row<4){
if(btn[row+1][col].getIcon() == imgBlank){
swapImg(btn[row+1][col],btn[row][col]);
}
}

if(col>0){
if(btn[row][col-1].getIcon() == imgBlank){
swapImg(btn[row][col-1],btn[row][col]);
}
}

if(col<4){
if(btn[row][col+1].getIcon() == imgBlank){
swapImg(btn[row][col+1],btn[row][col]);
}
}

blnCongrat = true;
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
if(btn[i][j].getIcon().toString() != imgSrc[i][j]){
blnCongrat = false;
}
}
}
if(blnCongrat){
JOptionPane.showMessageDialog(null,"Congratulation !!!","Slide Puzzle Game",JOptionPane.OK_OPTION);
blnNoStart = true;
}
}
}

public void actionPerformed(ActionEvent ae){
if(ae.getSource()==btnStart){
blnNoStart = false;
if(blnIsFirst){
imgBlank = btn[4][4].getIcon();
for(int i=0;i<=4;i++){
for(int j=0;j<=4;j++){
imgSrc[i][j] = btn[i][j].getIcon().toString();
}
}
blnIsFirst = false;
}
for(int i=0;i<=9000;i++){
int pos1 = (int) Math.round(Math.random() * 4);
int pos2 = (int) Math.round(Math.random() * 4);
swapImg(btn[pos1][pos2],btn[pos2][pos2]);
}
}else if(ae.getSource() == btn[0][0]){ slide(0,0);
}else if(ae.getSource() == btn[0][1]){ slide(0,1);
}else if(ae.getSource() == btn[0][2]){ slide(0,2);
}else if(ae.getSource() == btn[0][3]){ slide(0,3);
}else if(ae.getSource() == btn[0][4]){ slide(0,4);

}else if(ae.getSource() == btn[1][0]){ slide(1,0);
}else if(ae.getSource() == btn[1][1]){ slide(1,1);
}else if(ae.getSource() == btn[1][2]){ slide(1,2);
}else if(ae.getSource() == btn[1][3]){ slide(1,3);
}else if(ae.getSource() == btn[1][4]){ slide(1,4);

}else if(ae.getSource() == btn[2][0]){ slide(2,0);
}else if(ae.getSource() == btn[2][1]){ slide(2,1);
}else if(ae.getSource() == btn[2][2]){ slide(2,2);
}else if(ae.getSource() == btn[2][3]){ slide(2,3);
}else if(ae.getSource() == btn[2][4]){ slide(2,4);

}else if(ae.getSource() == btn[3][0]){ slide(3,0);
}else if(ae.getSource() == btn[3][1]){ slide(3,1);
}else if(ae.getSource() == btn[3][2]){ slide(3,2);
}else if(ae.getSource() == btn[3][3]){ slide(3,3);
}else if(ae.getSource() == btn[3][4]){ slide(3,4);

}else if(ae.getSource() == btn[4][0]){ slide(4,0);
}else if(ae.getSource() == btn[4][1]){ slide(4,1);
}else if(ae.getSource() == btn[4][2]){ slide(4,2);
}else if(ae.getSource() == btn[4][3]){ slide(4,3);
}else if(ae.getSource() == btn[4][4]){ slide(4,4);
}
}

public static void main(String args[]) {
System.out.println("Starting Slide Puzzle Game...");
SlidePuzzleGame spg = new SlidePuzzleGame();
}
}
Get Adobe Flash playerPlugin by wpburn.com wordpress themes