วันจันทร์ที่ 3 ตุลาคม พ.ศ. 2554

Show HigthScore โฉมใหม่ ล่าสุด 11/2/2011

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class High {
High() throws IOException{
  FileReader read = new FileReader("user.txt");
  BufferedReader file = new BufferedReader(read);
  String readFile = file.readLine();
  String real = "";
  while (readFile!=null) {
   real = real+readFile;
   readFile = file.readLine();
  }
  String hightscore[] = real.split(",");
  String name[] = new String[hightscore.length];
  String score[] = new String[hightscore.length];
  int scoreCheck[] = new int[hightscore.length];
  int scoreCheck2[] = new int[hightscore.length];
  int scoreCheck3[] = new int[hightscore.length];
  for(int i =0;i<hightscore.length;i++){
   String nameSp[] = hightscore[i].split(" ");
    name[i] = nameSp[0];
    score[i] = nameSp[1];
  
  }
 
 
  for(int i =0;i<scoreCheck.length;i++){
   scoreCheck[i] = Integer.parseInt(score[i]);
  
  }
 
 

  scoreCheck2 = scoreCheck;
  for(int i =0;i<scoreCheck2.length;i++){
   scoreCheck3[i] = scoreCheck2[i];
  
  }
  //JOptionPane.showMessageDialog(null,scoreCheck3[0]);

  Arrays.sort(scoreCheck);
 
  //JOptionPane.showMessageDialog(null,scoreCheck3[0]);
 
  String showHighScore[] =new String[scoreCheck.length];
  String showOnDilog ="";


  for(int i=0; i<scoreCheck.length;i++){
 
   for(int j=0;j<scoreCheck2.length;j++){
  
   
    if(scoreCheck[i]==scoreCheck3[j]){
     showHighScore[i] = name[j]+"                   "+scoreCheck3[j];
    
    }
   }
  }

  for(int i=showHighScore.length-1;i>=0;i--){
   showOnDilog = showOnDilog+showHighScore[i]+"\n";
  }
   JOptionPane.showMessageDialog(null,"              Hight Score\n"+showOnDilog ,"High Score",JOptionPane.INFORMATION_MESSAGE );
  }
}

วันพุธที่ 21 กันยายน พ.ศ. 2554

การ Random เลขที่ไม่ซ้ำกันเลย

import java.util.Collections;
import java.util.List;
import java.util.Vector;

public class Test {
 public Test() {
 
  int xxx[] = {1,2,3,4,5,6};
  List<Integer> lists = new Vector<Integer>();

  for(int i=0;i<xxx.length;i++){
  lists.add(xxx[i]);
 
  }
 
  Collections.shuffle(lists);
  int size = lists.size();
  for(int i=0;i<size;i++){
   System.out.println(lists.get(i));
  
  }

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

วันอาทิตย์ที่ 18 กันยายน พ.ศ. 2554

นัดติว JAVA ครั้งสุดท้าย IT

วันเสาร์ที่ 24.09.2011
ตึก 15 ชั้น 4

เวลา 19.00-20.00

วันเสาร์ที่ 17 กันยายน พ.ศ. 2554

กำหนดการสอบโปรเจค JAVA สาขา IT ปี 2 2554

เริ่มพรีเซนต์           16.00
หมดเวลาพรีเซนต์ 18.00

มารายตัวเพื่อส่งหัวข้อและรายชื่อก่อนเวลา 17.00  มิฉนั้นจะถูกหัก 2 คะแนน

ห้องพรีเซนต์ตั้งอยู่ที่ ชั้น3 อาคาร ศร 1. (ฝั่งขวา ประตูไม้ (ถ้ามองจากตึกสอง) สังเกตหน้าห้องจะมีที่วางรองเท้า)

กรุณามาให้เร็ว+ไว จะได้ ปิดคอร์ด อีกหนึ่งเรื่องครับ

วันพฤหัสบดีที่ 15 กันยายน พ.ศ. 2554

เกมจับคู่ภาพเหมือน ง่ายๆ เอาไปประยุกต์เอานะ





จับเวฃา แสดงบน frame

import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Th extends Thread implements Runnable{
 Th() throws InterruptedException{
 JLabel time = new JLabel("99");
 Font a=new Font("Tohama",Font.PLAIN,35);
   JFrame frame =new JFrame();
   frame.setLayout(null);
   frame.setSize(500,500);
   frame.setVisible(true);
   frame.add(time);
   time.setBounds(10, 0, 100, 50);
   time.setFont(a);
    for(int i = 99;i >0;i--)
      {
 
    sleep(1000);
    time.setText(""+i);
    if(i == 95){
     JOptionPane.showMessageDialog(null, "out of time");
     System.exit(0);
   
    }
  
    }
    }
  

 public static void main(String[] args) throws InterruptedException {
  new Th();
 }
}

วันจันทร์ที่ 12 กันยายน พ.ศ. 2554

ประกาศงดการเรียน JAVA ตั้งแต่วันที่ 13-16 กย 2554

ประกาศงดการเรียน JAVA ตั้งแต่วันที่ 13-16 กย 2554

เนื่อจากผมป่วยด้วยไข้หวัด ไม่สามารถสอนได้ จึงต้องขออภัยมา ณ ที่นี้ด้วย

ส่วนนิสิตที่มีปัญหาเรื่องโปรเจคแล้วอยากปรึกษา ผมได้ทำตารางไว้ให้ดังนี้

วันที่ 19 == > 8.00-10.00
วันที่ 20 == > 14.00-15.00
วันที่ 21 == > 13.00-15.00
วันที่ 22 == > 8.00-10.00
วันที่ 23 == > 13.00-15.00

สำหรับหรับเมล์ที่ส่งมาถามคำถาม บางเมลผมอาจไม่ได้ตอบนะครับ ด้วยป่วยมาก ไม่ไหวจริงๆ

เจอกันใหม่ วันที่ 19 ครับ

วันพุธที่ 7 กันยายน พ.ศ. 2554

วันอังคารที่ 6 กันยายน พ.ศ. 2554

ตัวอย่างเกม อัจฉริยะข้ามคืน

คำตอบอยู่ใน file ชื่อ jote.txt

เกมนี้คือเกมทายตัวเลข 4 หลัก ตามคำใบ้
ให้เอาไปทำเป็นตัวอย่างไม่ใช่ให้ลอกนะครับ แต่ถ้าจะลอกไปต้องทำให้ดีกว่านี้ มาก ถึงมากที่สุด
กดโหลดที่นี่

วันจันทร์ที่ 5 กันยายน พ.ศ. 2554

จับการทำงานของ keyboard

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class KeyTest {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Key Listener");
    Container contentPane = frame.getContentPane();
    KeyListener listener = new KeyListener() {
      public void keyPressed(KeyEvent e) {
        dumpInfo("Pressed", e);
      }
      public void keyReleased(KeyEvent e) {
        dumpInfo("Released", e);
      }
      public void keyTyped(KeyEvent e) {
        dumpInfo("Typed", e);
      }
      private void dumpInfo(String s, KeyEvent e) {
        System.out.println(s);
        int code = e.getKeyCode();
        System.out.println("\tCode: " + KeyEvent.getKeyText(code));
        System.out.println("\tChar: " + e.getKeyChar());
        int mods = e.getModifiersEx();
        System.out.println("\tMods: "
            + KeyEvent.getModifiersExText(mods));
        System.out.println("\tLocation: "
            + location(e.getKeyLocation()));
        System.out.println("\tAction? " + e.isActionKey());
      }
      private String location(int location) {
        switch (location) {
        case KeyEvent.KEY_LOCATION_LEFT:
          return "Left";
        case KeyEvent.KEY_LOCATION_RIGHT:
          return "Right";
        case KeyEvent.KEY_LOCATION_NUMPAD:
          return "NumPad";
        case KeyEvent.KEY_LOCATION_STANDARD:
          return "Standard";
        case KeyEvent.KEY_LOCATION_UNKNOWN:
        default:
          return "Unknown";
        }
      }
    };
    JTextField text = new JTextField();
    text.addKeyListener(listener);
    contentPane.add(text, BorderLayout.NORTH);
    frame.pack();
    frame.show();
  }
}

ดูว่า Click Mouse ที่ตำแหน่งไหน

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public MainClass() {
      addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent me) {
            System.out.println(me.getXOnScreen()+","+me.getYOnScreen());
          }
        });
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}

ดูว่า click Mouse ไปกี่ที

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public MainClass() {
      addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent me) {
            System.out.println(me.getClickCount());
          }
        });
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}

Hight Score from File on Dialog

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Read {
 Read() throws IOException{
  FileReader read = new FileReader("score.txt");
  BufferedReader file = new BufferedReader(read);
  String readFile = file.readLine();
  String real = "";
  while (readFile!=null) {
   real = real+readFile;
   readFile = file.readLine();
  
  
  
  }
  String hightscore[] = real.split(",");
  String name[] = new String[hightscore.length];
  String score[] = new String[hightscore.length];
  String scoreCheck[] = new String[hightscore.length];
  for(int i =0;i<hightscore.length;i++){
   String nameSp[] = hightscore[i].split(" ");
    name[i] = nameSp[0];
    score[i] = nameSp[1];
   
  
  }
  scoreCheck = score;
  Arrays.sort(score);
  String showHighScore[] =new String[score.length];
  String showOnDilog ="";
  for(int i=0; i<score.length;i++){
  
   for(int j=0;j<scoreCheck.length;j++){
   
    if(score[i].equals(scoreCheck[j])){
     showHighScore[i] = name[j]+"                   "+score[i];
    
    
    }
   }
  }
 
  for(int i=showHighScore.length-1;i>=0;i--){
   showOnDilog = showOnDilog+showHighScore[i]+"\n";
  }
 
   JOptionPane.showMessageDialog(null,"              Hight Score\n"+showOnDilog ,"High Score",JOptionPane.INFORMATION_MESSAGE );
 }
 public static void main(String[] args) throws IOException {
  new Read();
 }
}

JAVA Play Audio Sound (.wav)

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class audio extends Thread {

    private String filename;

    private Position curPosition;

    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

    enum Position {
        LEFT, RIGHT, NORMAL
    };

    public audio(String wavfile) {
        filename = wavfile;
        curPosition = Position.NORMAL;
    }

    public audio(String wavfile, Position p) {
        filename = wavfile;
        curPosition = p;
    }

    public void run() {

        File soundFile = new File(filename);
        if (!soundFile.exists()) {
            System.err.println("Wave file not found: " + filename);
            return;
        }

        AudioInputStream audioInputStream = null;
        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) {
            e1.printStackTrace();
            return;
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        if (auline.isControlSupported(FloatControl.Type.PAN)) {
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);
            if (curPosition == Position.RIGHT)
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT)
                pan.setValue(-1.0f);
        }

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0)
                    auline.write(abData, 0, nBytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            auline.drain();
            auline.close();
        }

    }
    public static void main(String[] args) {
  new audio("tada.wav").start();
 }
}

ส่งค่าจาก Textfield ไป หน้าอื่น


import javax.swing.JFrame;
import javax.swing.JLabel;
public class P2 extends JFrame{

 P2(String Gebtx1,String GebTx2,int sum){
 
  JLabel lb1 = new JLabel(Gebtx1);
  JLabel lb2 = new JLabel(GebTx2);
  JLabel lb3 = new JLabel(""+sum);
 
 
 
  setLayout(null);
  setBounds(50,100,300,500);
  setVisible(true);
 
 
  add(lb1);
  add(lb2);
  add(lb3);
 
  lb1.setBounds(10, 10, 100, 25);
  lb2.setBounds(10, 40, 100, 25);
  lb3.setBounds(10, 70, 100, 25);
 }
}

/////////////Class  ที่สอง


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Send extends JFrame implements ActionListener{
 JTextField tx1 = new JTextField(10);
 JTextField tx2 = new JTextField(10);
 JButton click = new JButton("pk");
 String GebTx1 ="";
 String GebTx2 ="";
 Send(){
  setLayout(null);
  setBounds(50,100,300,500);
  setVisible(true);
  add(tx1);
  add(tx2);
  add(click);
 
  tx1.setBounds(10, 10, 100, 25);
  tx2.setBounds(10, 40, 100, 25);
  click.setBounds(10, 70, 100, 25);
 
  click.addActionListener(this);
 }
 public static void main(String[] args) {
  new Send();
 }
 @Override
 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==click){
   GebTx1 = tx1.getText();
   GebTx2 = tx2.getText();
   int num1 = Integer.parseInt(GebTx1);
   int num2 = Integer.parseInt(GebTx2);
  
   JOptionPane.showMessageDialog(null, num1+num2)
;   new P2(GebTx1,GebTx2,num1+num2);
  }
 
 }
}


วันพฤหัสบดีที่ 1 กันยายน พ.ศ. 2554

Ex. เกมทศกัณฑ์ 2 ด่าน

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("");
   }
  }
 
 }
}

จับเวลา

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();
 }
}

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);

}

}

}

}

วันพุธที่ 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);
    }
   
   }
  
  }
  
 
 

}

วันจันทร์ที่ 6 มิถุนายน พ.ศ. 2554

JDK ใช้สำหรับรันโปรแกรม JAVA

โหลดได้จากลิงค์นี้นะครับ
http://www.mediafire.com/?524umj29l0g0ojx

ต้องลงโปรแกรมตัวนี้ก่อนนะครับ ไม่อย่างนั้น JLAB จะไม่สามารถทำงานได้ครับ
ตอนลงก็ next--> ไปเรื่อยเลยครับ

โปรแกรม JLAB ครับ

โหลดตามลิงค์ที่ให้มานี้ได้เลยครับ

http://www.cp.eng.chula.ac.th/~somchai/JLab/download/setupJLab91205.exe

โหลดแล้วก็ install สามารถใช้งานได้เลยครับ

แต่ก่อนใช้งานต้องลงโปรแกรม JDK 1.6 ก่อนนะครับ