แอพเพล็ตเบื้องต้น (Applet) การโปรแกรมเชิงวัตถุด้วยภาษา JAVA แอพเพล็ตเบื้องต้น (Applet) มหาวิทยาลัยเนชั่น http://www.nation.ac.th บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 10 มกราคม 2551
กราฟฟิกในเว็บเพจแบบอินเทอร์แอ็กทีฟ x.htm <applet code="x.class" width=200 height=50> </applet> DOS>appletviewer x.htm
เขียนตัวอักษร (1/4) import java.applet.*; import java.awt.*; public class x extends Applet { public void paint(Graphics g) { g.setColor(new Color(240,0,0)); g.drawString("test",10,20); } 10 X 20 test Y
เขียนตัวอักษร (2/4) RGB (Red Green Blue) <font face=#00ff00 size=0> Color Sample Hexadecimal: 000000 Decimal : 0,0,0 Hexadecimal: ffdddd Decimal : 255,238,238 Hexadecimal: 00ff00 Decimal : 0,255,0 Hexadecimal: ddddff Decimal : 238,238,255
เขียนตัวอักษร (3/4) g.drawString("test",10,20); test http://www.yonok.ac.th/pmy/j2sdk-1_4_2-doc.zip 10 X 20 test Y
เขียนตัวอักษร (4/4) import java.applet.*; import java.awt.*; public class j1102 extends Applet { int i,j; String istr,p; public void init() { setBackground(Color.yellow); p = getParameter("x"); } public void paint(Graphics g) { g.setColor(Color.black); g.drawString(p,0,10); i = 1; while (i <= 10) { j = 10 * i; istr= Integer.toString(i); g.drawString(istr,72,j); i++; // column = 1 inch 72 X 10 x 1 2 3 4 5 6 7 8 9 10 70 Y
วาดเส้นตรง g.drawLine(x1,y1,x2,y2); g.drawLine(10,20,30,20); void drawLine(int x1, int y1, int x2,int y2) Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system. Parameters: x1 - the first point's x coordinate. y1 - the first point's y coordinate. x2 - the second point's x coordinate. y2 - the second point's y coordinate. http://yn1.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html g.drawLine(x1,y1,x2,y2); g.drawLine(10,20,30,20); 10 30 X 20 Y
วาดสี่เหลี่ยม g.drawRect(x1,y1,width,height); g.drawRect(10,5,20,15); void drawRect(int x, int y, int width,int height) Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color. Parameters: x - the x coordinate of the rectangle to be drawn. y - the y coordinate of the rectangle to be drawn. width - the width of the rectangle to be drawn. height - the height of the rectangle to be drawn. http://yn1.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html g.drawRect(x1,y1,width,height); g.drawRect(10,5,20,15); 10 30 X 5 15 Y
วาดวงกลม g.drawOval(x1,y1,width,height); g.drawOval(10,5,20,15); void drawOval(int x,int y, int width, int height) Draws the outline of an oval. The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments. Parameters: x - the x coordinate of the upper left corner of the oval. y - the y coordinate of the upper left corner of the oval. width - the width of the oval to be drawn. height - the height of the oval to be drawn. http://yn1.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html g.drawOval(x1,y1,width,height); g.drawOval(10,5,20,15); 10 30 X 5 15 Y
วาดเส้นรอบวง g.drawArc(10,5,20,15,0,180); void drawArc(int x,int y,int width,int height,int startAngle,int arcAngle) Draws the outline of a circular or elliptical arc covering the specified rectangle. http://yn1.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html g.drawArc(10,5,20,15,0,180); 10 30 X 5 15 Y
วาดหลายเหลี่ยม int[] x={10,50,100}; int[] y={10,100,50}; void drawPolygon(Polygon p) Draws the outline of a polygon defined by the specified Polygon object. Parameters: p - the polygon to draw. See Also: fillPolygon(int[], int[], int) drawPolyline(int[], int[], int) http://yn1.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html int[] x={10,50,100}; int[] y={10,100,50}; g.drawPolygon(x,y,3); 10 50 100 X 10 50 100 Y
ปุ่ม และซ่อนทีละปุ่ม import java.applet.*; import java.awt.*; import java.awt.event.*; public class x extends Applet implements ActionListener { Button b1 = new Button("exit1"); Button b2 = new Button("exit2"); public void init() { add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("exit1")) b1.setVisible(false); if (e.getActionCommand().equals("exit2")) b2.setVisible(false);
เพิ่มค่าให้กับ TextField import java.applet.*; import java.awt.*; import java.awt.event.*; public class x extends Applet implements ActionListener { TextField t = new TextField("1"); public void init() { add(t); t.addActionListener(this); } public void paint(Graphics g) { } public void actionPerformed(ActionEvent e){ int n1 = Integer.parseInt(e.getActionCommand()); String n2 = Integer.toString(n1 + 1); t.setText(n2);
วาดโดราเอมอนด้วย Applet import java.applet.*; import java.awt.*; import java.awt.event.*; public class x extends Applet { public void paint(Graphics g){ g.setColor(Color.black); g.drawRect(140,285,220,5); g.drawOval(240,180,20,20); g.drawOval(210,130,40,60); g.drawOval(250,130,40,60); g.drawOval(233,160,15,20); g.drawOval(240,290,20,20); g.drawLine(250,200,250,250); g.drawLine(240,230,180,240); g.drawLine(260,230,320,240); g.drawLine(240,220,180,220); g.drawLine(260,220,320,220); g.drawLine(240,210,180,200); g.drawLine(260,210,320,200); g.drawLine(245,300,255,300); g.drawLine(250,300,250,310); g.drawArc(255,165,10,10,30,120); g.drawArc(150,50,200,200,240,60); g.drawArc(150,160,200,250,113,67); g.drawArc(150,160,200,250,0,67); g.drawArc(135,125,230,240,340,220); } โดย สุริยา พงษ์ปัญญา 4708002
Awt ผ่าน Java (ไม่ใช่ Applet) import java.awt.*; import java.awt.event.*; public class x implements ActionListener { Frame s = new Frame("awt"); Button bclose = new Button("Exit"); public static void main(String[] args){ new x().init(); } public void init( ) { s.setSize(200,200); s.add(bclose); bclose.addActionListener(this); s.show(); public void actionPerformed(ActionEvent a) { if(a.getSource()==bclose) System.exit(0);