Java D - 1
Java D - 2
(QDSSOHW
$SSOHWVRFKJUDILN
•
•
$SSOHWV
– /LYVF\NHO
–
–
•
3DUDPHWUDU
import java.awt.*;
import java.applet.*;
public class Demo extends Applet
{
private int antalUpprepningar = 0;
public void init()
{
String antal = getParameter("upprepa");
antal = antal.trim();
antalUpprepningar = Integer.parseInt(antal);
setBackground(Color.white);
}
6lNHUKHW
*UDILN
– 5LWIXQNWLRQHU
–
–
public void paint(Graphics g)
{
for (int i=0; i <antalUpprepningar; i++)
g.drawString("Hej Hopp!", 50, 30 + 20*i);
}
)lUJ
}
7\SVQLWW
•
•
9LVDELOGHU
•
6SHODXSSOMXG
-DYDILOHQ'HPR-DYD
+70/ILOHQ'HPRKWPO
<HTML>
<APPLET CODE=Demo.class WIDTH=150 HEIGHT=100>
<PARAM NAME=upprepa VALUE=3>
</APPLET>
</HTML>
•
.RPSLOHUDRFKN|UDDSSOHWHQ
==> javac Demo.java
==> appletviewer Demo.html
Java D - 3
Java D - 4
.ODVVHQ$SSOHW
•
9DUMH DSSOHW lUYHU IUnQ NODVVHQ $SSOHW
(QDSSOHWVOLYVF\NHO
•
1lU HQ DSSOHW N|UV JnU GHQ LJHQRP HWW DQWDO
VWHJ IUnQ LQLWLHULQJ WLOO DYVOXWQLQJ
•
:HEEOlVDUHQ DQURSDU ROLND PHWRGHU L
.ODVVHQ $SSOHW ILQQV L SDNHWHW MDYDDSSOHW
DSSOHWHQ I|U YDUMH VWHJ
•
(Q DSSOHWV IXQNWLRQ EHVWlPV EO D JHQRP DWW
start()
RPGHILQLHUD HWW DQWDO PHWRGHU VRP ILQQV L
NODVVHQ $SSOHW
•
init()
1nJUD VnGDQD PHWRGHU lU
destroy()
stop()
•
init()
init()
$QURSDV HQ JnQJ QlU DSSOHWHQ VNDSDV
start()
PRWVYDUDU NRQVWUXNWRUQ I|U YDQOLJD REMHNW
stop()
•
$QURSDV YDUMH JnQJ DSSOHWHQ VND YLVDV .DQ
destroy()
paint()
start()
DQURSDV IOHUD JnQJHU
•
stop()
$QURSDV YDUMH JnQJ DSSOHWHQ LQWH VND YLVDV
destroy()
$QURSDV L VDPEDQG PHG DWW DSSOHWHQ XSSK|U
DWW H[LVWHUD
Java D - 5
Java D - 6
$FFHVVUlWWLJKHWHU
.ODVVHQ*UDSKLFV
6LWXDWLRQ
:,
:/
$9
-$
/lVD IUnQ HQ ORNDO ILO
2.
2.
6NULYD WLOO HQ ORNDO ILO
2.
2.
,QIRUPDWLRQ RP HQ ILO
2.
2.
5DGHUD HQ ILO
2.
.|UD DQGUD SURJUDP
2.
2.
.RPXQLFHUD PHG VHUYHUQ
2.
2.
2.
2.
.RP PHG DQQDQ GDWRU
2.
2.
2.
$QURSD H[LW
2.
2.
6NDSD HWW SRSI|QVWHU
PHG
2.
2.
2N
YDUQLQJ
:, $SSOHW |YHUI|UG YLD LQWHUQHW WLOO HQ ZHEOlVDUH
•
9LGDQURSHWWLOOSDLQWPHWRGHQ|YHUI|UV
HQUHIHUHQVWLOOHWWREMHNWDYW\SHQ
Graphics
•
$OOULWQLQJVNHUPHGKMlOSDYDQURSWLOO
PHWRGHULNODVVHQ*UDSKLFVWH[
g.drawString("Hej Hopp!", 100, 100);
•
.ODVVHQ*UDSKLFVLQQHKnOOHUPHWRGHUI|U
DWWEOD
– VNULYDWH[WVDPWULWDOLQMHURFK
ILJXUHU
•
:/ $SSOHW ODGGDG IUnQ GHQ ORNDOD PDVNLQHQ WLOO HQ ZHEOlVDUH
–
$9 $SSOHW ODGGDG IUnQ GHQ ORNDOD PDVNLQHQ WLOO HQ
-$ -DYD DSSOLNDWLRQ G Y V HWW IULVWnHQGH SURJUDP
IRUPDWHUDWH[WRFKILJXUHU
•
DSSOHWYLHZHU
–
GUDZ6WULQJGUDZ5HFWILOO5HFW
VHW)RQWVHW&RORU
YLVDELOGHU
•
GUDZ,PDJH
Java D - 7
Java D - 8
5LWDHQNODILJXUHU
•
drawLine(x1, y1, x2, y2)
5LWDU HQ OLQMH IUnQ SXQNWHQ [ \ WLOO [ \
•
drawRect(x, y, b, h)
fillRect(x, y, b, h)
5LWDU HQ RI\OOG UHVS I\OOG UHNWDQJHO PHG |YUH
YlQVWHU K|UQ L [ \ EUHGGHQ E RFK K|MGHQ K
•
(WWJUDILNH[HPSHO
import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(10, 10, 100, 100);
g.setColor(Color.yellow);
g.fillOval(100, 100, 50,50);
drawOval(x, y, b, h)
fillOval(x, y, b, h)
Polygon p = new Polygon();
p.addPoint(50, 50);
p.addPoint(150, 50);
p.addPoint(100, 150);
5LWDU HQ RI\OOG UHVS I\OOG HOOLSV VRP lU LQVNULYHQ L
HQ UHNWDQJHO PHG YlQVWHU |YUH YlQVWHU K|UQ L [
\ EUHGG E RFK K|MG K
•
g.setColor(Color.orange);
g.fillPolygon(p);
drawPolygon(xp, yp, n)
fillPolygon(xp, yp, n)
g.setColor(Color.blue);
g.setFont(new Font(
"SansSerif", Font.BOLD + Font.ITALIC, 25));
g.drawString("Några figurer", 20, 50);
5LWDU HQ RI\OOG PnQJK|UQLQJ UHVSHNWLYH HWW DQWDO
VDPPDQKlQJDQGH OLQMHU [S RFK \S lU IlOW PHG
SXQNWHUQDV [ RFK \NRRUGLQDWHU Q lU DQWDOHW
SXQNWHU
•
drawString(txt, x, y)
5LWDU WH[WVWUlQJHQ W[W PHG E|UMDQ L SXQNWHQ[ \
\NRRUGLQDWHQ DQJHU WH[WHQV EDVOLQMH
}
}
Java D - 9
Java D - 10
$SSOHWPHGELOG
/lVDLQRFKYLVDELOGHU
•
import java.awt.*;
import java.applet.*;
-DYD NDQ KDQWHUD IlUGLJD ELOGHU ODJUDGH L
IRUPDWHQ *,) RFK -3(* (Q ELOGILO NDQ OlVDV LQ
public class Bild extends Applet
{
private Image bilden = null;
PHG I|OMDQGH VDWV
Image bild = getImage(getCodeBase(),
”bild.gif”));
•
0HWRGHQ
getCodeBase
public void init()
{
bilden = getImage(getCodeBase(), "glac5.gif");
}
JHU DGUHVVHQ WLOO GHQ
NDWDORJ GlU DSSOHWHQ OLJJHU
•
'HW ILQQV RFNVn HQ PHWRG
getDocumentBase
public void paint(Graphics g)
{
if (g.drawImage(bilden, 0, 0, this))
showStatus("Laddar ... klart!");
else
showStatus("Laddar ... ");
}
VRP
JHU DGUHVVHQ WLOO GHQ NDWDORJ I|U GRNXPHQWHW VRP
DSSOHWHQ OLJJHU L
}
•
%nGD PHWRGHUQD UHWXUQHUDU HQ UHIHUHQV WLOO HWW
85/REMHW µ8QLIRUP 5HVRXUFH /RFDWRUµ VRP
UHSUHVHQWHUDU HQ XQLN DGUHVV Sn QlWHW
•
)|U DWW YLVD HQ LQOlVW ELOG DQURSDV PHWRGHQ
drawImage
drawImage(bild, x, y, observerare)
$SSOHWPHGOMXGRFK
ELOG
Java D - 11
--------------------------------------------------import java.awt.*;
import java.applet.*;
public class Ljud extends Applet
{
private Image bilden = null;
private AudioClip ljudet = null;
public void init()
{
bilden = getImage(getCodeBase(), "glac5.gif");
ljudet = getAudioClip(getCodeBase(), "yahoo.au");
}
public void paint(Graphics g)
{
if (g.drawImage(bilden, 0, 0, this))
{
showStatus("Laddar ... klart!");
ljudet.play();
}
else
showStatus("Laddar ... ");
}
}
--------------------------------------------------==> javac Ljud.java
==> jar cf Ljud.jar Ljud.class glac5.gif yahoo.au
==>
--------------------------------------------------<HTML>
<APPLET CODE="Ljud.class"
ARCHIVE="Ljud.jar" WIDTH=400 HEIGHT=300>
</APPLET>
</HTML>
---------------------------------------------------