http://www.thaiall.com/class
Page:
1
การโปรแกรมเชิงวัตถุด
้
วยภาษา
JAVA
บุรินทร
์
รุจจนพันธุ
์
.
ปรับปรุง
10
มกราคม
255
1
แอพเพล
็
ตเบื้องต
้
น
(Applet)
มหาวิทยาลัยเนชั่น
http://
www.
nation.
ac.th
http://www.thaiall.com/class
Page:
2
กราฟฟิกในเว็บเพจแบบอิน
เทอร
์
แอ็กทีฟ
x.htm
<applet code="x.class"
width=200 height=50>
</applet>
DOS>
appletviewer x.htm
http://www.thaiall.com/class
Page:
3
เขียนตัวอักษร
(
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
)
;
}
}
test
20
10
Y
X
http://www.thaiall.com/class
Page:
4
เขียนตัวอักษร
(
2
/
4
)
RGB (Red Green Blue)
<font face=#
00
ff
00
size=
0
>
Color Sample
Hexadecimal:
000000
Decimal :
0
,
0
,
0
Hexadecimal: ffdddd
Decimal :
255
,
238
,
238
Hexadecimal:
00
ff
00
Decimal :
0
,
255
,
0
Hexadecimal: ddddff
Decimal :
238
,
238
,
255
http://www.thaiall.com/class
Page:
5
เขียนตัวอักษร
(
3
/
4
)
test
20
10
Y
X
g.drawString("test",
10
,
20
)
;
http://www.yonok.ac.th/pmy/j
2
sdk
-
1
_
4
_
2
-
doc.zip
http://www.thaiall.com/class
Page:
6
เขียนตัวอักษร
(
4
/
4
)
1
2
3
4
5
6
7
8
9
10
70
72
Y
X
import java.applet.*;
import java.awt.*;
public class j
1102
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
}
}
}
10
x
http://www.thaiall.com/class
Page:
7
วาดเส
้
นตรง
20
10
Y
X
void drawLine(int x
1
, int y
1
, int x
2
,int y
2
)
Draws a line, using the current color, between the points
(x
1
, y
1
) and (x
2
, y
2
) in this graphics context's coordinate
system.
Parameters:
x
1
-
the first point's x coordinate.
y
1
-
the first point's y coordinate.
x
2
-
the second point's x coordinate.
y
2
-
the second point's y coordinate.
http://yn
1
.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html
g.drawLine(x
1
,y
1
,x
2
,y
2
)
;
g.drawLine(
10
,
20
,
30
,
20
)
;
30
http://www.thaiall.com/class
Page:
8
วาดสี่เหลี่ยม
5
10
Y
X
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://yn
1
.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html
g.draw
Rect
(x
1
,y
1
,width,height);
g.drawRect(
10
,
5
,
20
,
15
)
;
30
15
http://www.thaiall.com/class
Page:
9
วาดวงกลม
5
10
Y
X
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://yn
1
.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html
g.draw
Oval
(x
1
,y
1
,width,height);
g.drawOval(
10
,
5
,
20
,
15
)
;
30
15
http://www.thaiall.com/class
Page:
10
วาดเส
้
นรอบวง
5
10
Y
X
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://yn
1
.yonok.ac.th/burin/javadocs/api/java/awt/Graphics.html
g.drawArc(
10
,
5
,
20
,
15
,
0
,
180
)
;
30
15
http://www.thaiall.com/class
Page:
11
วาดหลายเหลี่ยม
10
10
Y
X
v
oid 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://yn
1
.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
)
;
100
100
50
50
http://www.thaiall.com/class
Page:
12
ปุ
่
ม และซ
่
อนทีละปุ
่
ม
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class x extends Applet implements ActionListener {
Button b
1
= new Button("exit
1
")
;
Button b
2
= new Button("exit
2
")
;
public void init() {
add(b
1
)
;
add(b
2
)
;
b
1
.addActionListener(this);
b
2
.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("exit
1
")) b
1
.setVisible(false);
if (e.getActionCommand().equals("exit
2
")) b
2
.setVisible(false);
}
}
http://www.thaiall.com/class
Page:
13
เพิ่มค
่
าให
้
กับ
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 n
1
= Integer.parseInt(e.getActionCommand());
String n
2
= Integer.toString(n
1
+
1
)
;
t.setText(n
2
)
;
}
}
http://www.thaiall.com/class
Page:
14
วาดโดราเอมอนด
้
วย
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
http://www.thaiall.com/class
Page:
15
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
)
;
}
}
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Comments 0
Log in to post a comment