October 20th, 2008
Java funny with Robot Class !!!
No Comments », Java, Programming, Programming Tips, by exodusสวัสดีครับ วันนี้ผม exodus_T_T (ฮือๆ) จะมาพูดคุยกับเพื่อนๆ น้องๆ เกี่ยวกะ Robot class ใน java awt libary กัน ซึ่ง ประโยชน์ ของ Robot คือ การให้ java เข้ามา ควบคุม !!! keyboard หรือ mouse ก็ได้ ระหว่างที่ Run Program ที่เราเขียนด้วย Robot class ขึ้นมา ซึ่งประโยชน์ มากมายมหาศาล (รึเปล่า) ของมัน จะทำให้เราสนุก กับการคิดอะไรแปลกๆ และใช้เวลาว่างๆ ซึ่งพวกเราไม่ค่อยจะมี ให้กับการ practice java เท่าไหร่ (มึนมะ คนเขียนมึนล่ะ) อันนี้ก็ขึ้นอยู่กับ idea ของแต่ล่ะคนนะครับ ว่าคิดได้แค่ไหน มี idea อะไร ก็เอามาโชว์กันหน่อย
เอาล่ะพล่ามมากมาก เรามาดูกันดีกว่า program ที่ผมจะนำเสนอ มันเจ๋งขนาดไหน ฮ่าๆๆ …

/**
*
* @author exodus_tt
*/
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
public class RobotDemo extends JFrame implements ActionListener
{
//Store Keystrokes in an array
static int keyInput[] = {KeyEvent.VK_R,KeyEvent.VK_O,
KeyEvent.VK_B,KeyEvent.VK_O,KeyEvent.VK_T};
static JTextArea ta = new JTextArea();
static JButton bold = new JButton(“Bold”);
public RobotDemo()
{
getContentPane().add(ta,BorderLayout.CENTER);
JPanel p = new JPanel();
bold.addActionListener(this);
p.add(bold);
getContentPane().add(p,BorderLayout.SOUTH);
}
public static void main(String[] args) throws AWTException, IOException
{
RobotDemo rd = new RobotDemo();
rd.setLocation(100,100);
rd.setTitle(“Robot Demo”);
rd.setSize(200,200);
rd.setVisible(true);
Robot robot = new Robot();
//This types the word ‘robot’ in the Textarea
for (int i = 0; i < keyInput.length; i++)
{
if(i > 0)
{
robot.keyRelease(KeyEvent.VK_SHIFT);
}
robot.keyPress(keyInput[i]);
robot.delay(500);
}
//The following clicks the button ‘Bold’ to get the text bolder
robot.mouseMove(180,280);
robot.delay(2000);
robot.mousePress(InputEvent.BUTTON1_MASK);
//This delay keeps the button pressed for 2 seconds
robot.delay(2000);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(3000);
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
Font f = new Font(“Times New Roman”, Font.BOLD, 20);
ta.setFont(f);
}
}
ในส่วนของ code ก็ไม่ได้ยากมากมายที่จะทำความเข้าใจนะครับ
หากไม่เข้าใจ code ในส่วนของ class robot ตรงไหน ก็สามารถเข้าไปที่
http://www.javadoconline.com/search.jsp?from=main&class=robot&action=search
เพื่อดู java document ของ Robot class ที่ได้ทำการอธิบายไว้อย่างละเอียดรับ ^^ …
หากขัดข้องอันใด ก็สามารถ add msn หรือ e-mail มาได้ที่ exodus_tt@hotmail.com ครับ
ด้วยความสามารถอันน้อยนิดของผม จะพยายามหาสิ่งที่มีประโยชน์ และ ความรู้ มาลง blog ให้อีกใน คราวหน้านะครับ
หาก blog ง่ายไป หรือ ผิดพลาดประการใด ขออภัยมา ณ ที่นี้ด้วย ฮ่าๆๆ … ขอบคุณครับ
ที่มา http://www.devx.com/tips/Tip/28642
เพิ่มเติม robot capture หน้าจอ
http://www.java-tips.org/java-se-tips/java.awt/how-to-capture-screenshot.html
Last 5 posts by exodus
- hynix แจ้งเกิดชิปหน่วยความจำ"หนาแน่นที่สุดในโลก" - March 8th, 2009
- กูเกิลจ่ายโบนัส 4 ผู้บริหารไม่ต่ำกว่า 40 ล้านบาทต่อคน - March 8th, 2009
- Intel Havendel / Auburndel !!! - March 4th, 2009
- USB 3.0 , by Seagate - March 4th, 2009
- eSATA ใน flash drive ... !!! - March 3rd, 2009

