Java 2003.11.4 Web Web Web Applet java.applet.applet Applet public void init () start public void start () public void stop () public void destroy () stop Applet Java.awt.Component Component public void paint () EventListener implements EventListener Web HTML APPLET <APPLET CODE = appletfile WIDTH = pixels HEIGHT = pixels> </APPLET> CODE WIDTH HEIGHT ARCHIVE ALIGN VSPACE, HSPACE ALT APPLET APPLET ALIGN IMG 20
Java 2003.11.4 GUI java.awt GUI Canvas MyCanvas Jikken.java import java.applet.applet; import java.awt.*; import java.awt.event.*; import javax.imageio.*; import java.net.*; /** */ public class JikkenApplet extends Applet implements ActionListener, ItemListener, AdjustmentListener, MouseListener, MouseMotionListener, WindowListener, KeyListener { Button button, button1, button2; // Checkbox check[]; // CheckboxGroup group; // Choice choice; // Dialog dialog; // Frame frame; Image image; Label label; // // // List list1, list2; // Scrollbar scroll; // TextArea textarea; // TextField textfield1, textfield2; // Panel panel1, panel2, panel3; // MyCanvas canvas; public void init() { GridBagLayout gridbag = new GridBagLayout(); setlayout(gridbag); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridheight = 2; makepanel1(); gridbag.setconstraints(panel1, c); add(panel1); c.weightx = 1.0; canvas = new MyCanvas(); gridbag.setconstraints(canvas, c); canvas.addmouselistener(this); canvas.addmousemotionlistener(this); add(canvas); canvas.addkeylistener(this); 21
c.weightx = 0.0; c.gridheight = 1; c.gridwidth = GridBagConstraints.REMAINDER; makepanel2(); gridbag.setconstraints(panel2, c); add(panel2); makepanel3(); gridbag.setconstraints(panel3, c); add(panel3); c.weightx = 1.0; c.weighty = 1.0; c.gridheight = GridBagConstraints.REMAINDER; c.gridwidth = GridBagConstraints.REMAINDER; textarea = new TextArea("TextArea"); gridbag.setconstraints(textarea, c); add(textarea); try { image = getimage(new URL(getCodeBase(), "2euro.gif")); catch (MalformedURLException ex) { ex.printstacktrace(); canvas.setimage(image); canvas.setpoint(30, 30); canvas.repaint(); makeframe(); public void makepanel1() { panel1 = new Panel(); panel1.setbackground(color.yellow); panel1.setlayout(new GridLayout(8, 1)); button = new Button("Button"); button.addactionlistener(this); panel1.add(button); check = new Checkbox[6]; group = new CheckboxGroup(); for(int i = 0; i < 3; i++) { check[i] = new Checkbox("check " + i); check[i].additemlistener(this); panel1.add(check[i]); for(int i = 0; i < 3; i++) { check[i + 3] = new Checkbox("radio " + i, false, group); check[i + 3].addItemListener(this); panel1.add(check[i + 3]); 22
choice = new Choice(); for(int i = 0; i < 3; i++) { choice.additem("choice " + (i + 1)); choice.additemlistener(this); panel1.add(choice); public void makepanel2() { panel2 = new Panel(); panel2.setbackground(new Color(255, 192, 192)); list1 = new List(4, false); list2 = new List(4, true); for(int i = 0; i < 6; i++) { list1.add("list " + (i + 1)); list2.add("list " + (i + 1)); list1.additemlistener(this); list2.additemlistener(this); panel2.add(list1); panel2.add(list2); public void makepanel3() { panel3 = new Panel(); panel3.setbackground(color.white); panel3.setlayout(null); label = new Label("Label"); label.setbounds(10, 10, 80, 25); panel3.add(label); button1 = new Button("Frame"); button1.addactionlistener(this); button1.setbounds(10, 40, 80, 30); panel3.add(button1); textfield1 = new TextField("TextField"); textfield1.setbounds(100, 10, 120, 25); textfield1.addactionlistener(this); panel3.add(textfield1); textfield2 = new TextField("50"); textfield2.seteditable(false); textfield2.setbounds(100, 45, 120, 25); panel3.add(textfield2); scroll = new Scrollbar(Scrollbar.HORIZONTAL, 50, 5, 0, 105); scroll.setbounds(10, 80, 210, 25); scroll.addadjustmentlistener(this); panel3.add(scroll); public void makeframe() { frame = new Frame("Frame"); frame.setbounds(100, 100, 200, 200); frame.add(new Label("Dialog "), BorderLayout.CENTER); 23
button2 = new Button("Dialog"); button2.addactionlistener(this); frame.addwindowlistener(this); frame.add(button2, BorderLayout.SOUTH); dialog = new Dialog(frame, "Dialog"); dialog.setbounds(500, 100, 200, 200); dialog.add(new Label(" ")); dialog.addwindowlistener(this); public void actionperformed(actionevent e) { Object obj = e.getsource(); if(obj == button) { textarea.append(" nbutton "); else if(obj == button1) { frame.setvisible(true); else if(obj == button2) { dialog.setvisible(true); else { textarea.append(" ntextfield " + textfield1.gettext() + " "); public void itemstatechanged(itemevent e) { Object obj = e.getsource(); boolean c = false; for(int i = 0; (i < 3) && (c == false); i++) { if(obj == check[i]) { textarea.append(" n" + check[i].getlabel() + " " + check[i].getstate() + " "); c = true; if(i == 0) { boolean b = check[0].getstate(); canvas.setdoublebuffer(b); if (b) { /* */ textarea.append(" n else { if(c == false) { if(obj == choice) { "); textarea.append(" n "); textarea.append(" n" + choice.getselectedindex() + " " + choice.getselecteditem() + " "); else if(obj == list1) { textarea.append(" n " + list1.getselectedindex() + 24
Java 2003.11.4 " " + list1.getselecteditem() + " "); else if(obj == list2) { int[] x = list2.getselectedindexes(); String[] s = list2.getselecteditems(); int ii = x.length; if(ii == 0) { textarea.append(" n "); else { String str =" n "; str = str + x[0] + " " + s[0] + " "; for(int i = 1; i < ii; i++) { str = str + " " + x[i] + " " + s[i] + " "; textarea.append(str + " "); else { Checkbox cb = group.getselectedcheckbox(); textarea.append(" n" + cb.getlabel() + " "); String str; if (cb == check[3]) { str = "2euro.gif"; else if(cb == check[4]) { str = "norokko.png"; else { str = "grasshopper.jpg"; try { image = getimage(new URL(getCodeBase(), str)); catch (MalformedURLException ex) { canvas.setimage(image); public void adjustmentvaluechanged(adjustmentevent e) { textfield2.settext(integer.tostring(scroll.getvalue())); public void mouseclicked(mouseevent e) { textarea.append(" n ( " + e.getx() + ", " + e.gety() + canvas.setpoint(e.getx(), e.gety()); ") "); public void mousepressed(mouseevent e) { textarea.append(" n ( " + e.getx() + ", " + e.gety() + ") "); public void mousereleased(mouseevent e) { textarea.append(" n ( " + e.getx() + ", " + e.gety() + ") "); 25
public void mouseentered(mouseevent e) { textarea.append(" n ( " + e.getx() + ", " + e.gety() + ") "); public void mouseexited(mouseevent e) { textarea.append(" n ( " + e.getx() + ", " + e.gety() + ") "); public void mousedragged(mouseevent e) { canvas.setpoint(e.getx(), e.gety()); public void mousemoved(mouseevent e) { label.settext("( " + e.getx() + ", " + e.gety() + ")"); public String which(object obj) { if(obj == frame) { return "Frame"; else { return "Dialog"; public void windowopened(windowevent e){ textarea.append(" n" + which(e.getsource()) + " "); public void windowclosing(windowevent e){ ((Window)e.getSource()).setVisible(false); String str; if(e.getsource() == frame) { str = "Frame"; else { str = "Dialog"; textarea.append(" n" + which(e.getsource()) + " "); public void windowclosed(windowevent e) { public void windowiconified(windowevent e) { public void windowdeiconified(windowevent e) { public void windowactivated(windowevent e) { public void windowdeactivated(windowevent e) { public void keytyped(keyevent e) { textarea.append(" n" + e.getkeychar() + " "); public void keypressed(keyevent e) { public void keyreleased(keyevent e) { 26
MyCanvas.java import java.awt.*; import java.util.*; public class MyCanvas extends Canvas implements Runnable { int drawx, drawy; Image image, buffer; Thread thread; Font font; boolean dbuffer; public MyCanvas() { thread = null; font = null; buffer = null; dbuffer = false; this.init(); public void init() { image = null; drawx = -1; drawy = -1; if(font == null) { font = new Font("Sans-serif", 0, 50); if(thread == null) { thread = new Thread(this); thread.start(); public void setdoublebuffer(boolean b) { dbuffer = b; public boolean getdoublebuffer() { return dbuffer; public void setimage(image i) { image = i; public Image getimage() { return image; public void setpoint(int x, int y) { drawx = x; drawy = y; 27
public int getx() { return drawx; public int gety() { return drawy; public String time() { GregorianCalendar cal = new GregorianCalendar(); String str = Integer.toString(cal.get(Calendar.HOUR_OF_DAY)) + ":"; int x = cal.get(calendar.minute); if(x < 10) { str += "0"; str = str + x + ":"; x = cal.get(calendar.second); if(x < 10) { str += "0"; return (str + x); public void paint(graphics g) { Graphics gr; if(dbuffer) { int w = getwidth(); int h = getheight(); if (buffer == null) { buffer = createimage(w, h); gr = buffer.getgraphics(); gr.setcolor(getbackground()); gr.fillrect(0, 0, w - 1, w - 1); gr.setcolor(color.black); else { gr = g; if((image!= null) && (drawx >=0) && (drawy >= 0)) { int w = image.getwidth(this); int h = image.getheight(this); gr.drawimage(image, drawx - w / 2, drawy - h / 2, this); gr.setfont(font); gr.drawstring(time(), 5, 70); if(dbuffer) { g.drawimage(buffer, 0, 0, this); public void update(graphics g) { if(dbuffer) { paint(g); else { super.update(g); 28
public void run() { while(true) { repaint(); try { thread.sleep(100); catch(interruptedexception e) { jikken.html <!DOCUMENT HTML PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/tr/html4/loose.dtd"> <HTML> <BODY> <HEAD> <META http-equiv="content-type" content="text/html; charset=shift_jis"> <TITLE> </TITLE> </HEAD> <P> <H2> </H2> </P> </P> <BODY BGCOLOR="#ffffff"> <P> <APPLET CODE="JikkenApplet.class" WIDTH=600 HEIGHT=400> </APPLET><BR> </P> </BODY> </HTML> 2euro.gif norokko.png grasshopper.jpg 29