35 5 JApplet toy example 5.1 2 extends class Point { // public int x; public int y; Point x y, 5.2 Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2
36 5 Point p = new Point(); p.x = 1; p.y = 2; System.out.println("(" + p.x + ", " + p.y + ")");. 5.3 C, class Point { // ( public int x; public int y; // public void move(int dx, int dy) { x += dx; y += dy; public void print() { System.out.print("(" + x + ", " + y + ")"); // public Point(int x0, int y0) { x = x0; y = y0; move print x, y. Point int 2 Point p = new Point(1, 2); x 1 y 2 PointTest Point main
5.4. 37 public class PointTest { public static void main(string args[]) { Point p = new Point(10, 20); p.move(1, -1); p.print(); System.out.println("<br>"); static main static C C++ PointTest.java Point public public Point PointTest Point public javac PointTest.java java PointTest 5.4 Point ColorPoint Point Point Point ColorPoint ColorPoint Point extends class ColorPoint extends Point { public String color; public ColorPoint(int x, int y, String c) { super(x, y); /* 1 */ color = c; public void print() { System.out.print("<font color= "+color+ >"); // System.out.print("(" + x + ", " + y + ")"); // super.print(); System.out.print("</font>"); // ColorPoint color print() Applet super(x, y) /* 1 */ Point super
38 5 print() HTML HTML ColorPoint print() 2 /* 2 */ Point print() super.print(); Point x y move /* 2 */ public static void main(string args[]) { ColorPoint cp = new ColorPoint(10, 20, "green"); cp.move(1, -1); cp.print(); System.out.println("<br>"); (11, 19) 5.5 color "red", "green" 2 setcolor getcolor ColorPoint "black", "red", "green", "yellow","blue", "magenta", "cyan", "white" color int 1 class ColorPoint extends Point { public String[] cs = {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", ""; public int color; // 0-1- 2-3- 4-5- 6-7- public void print() { System.out.print("<font color= "+getcolor()+" >"); // System.out.print("(" + x + ", " + y + ")"); super.print();// System.out.print("</font>"); public void setcolor(string c) { int i; for (i=0;!cs[i].equals(""); i++) { if (c.equals(cs[i])) { color = i; return; // public ColorPoint(int x, int y, String c) { super(x, y); setcolor(c); public String getcolor() { return cs[color]; // // 1 color int
5.5. 39 setcolor getcolor color 0 7 p.color = 100; public ColorPoint color private int color; // color cs p.color = 100; public 2 private, public, protected public ColorPoint color "black", "red" 5.5.1 ColorPoint color "black", "red" 5.5.2 DeepPoint Point int depth print depth 5 DeepPoint (((((11, 19))))) 5 1. DeepPoint 2. depth 1 10 setdepth getdepth 5.5.3 SecretPoint Point 2 int a, b print a x+b y = 1 (1, 2) (?,?) SecretPoint 2 protected
40 5 5.6 public static void main(string args[]) { Point p = new Point(1, 2); ColorPoint cp = new ColorPoint(3, 4, "green"); DeepPoint dp = new DeepPoint(5, 6, 5); Point, ColorPoint, DeepPoint 3 Point 3 Point[] pts = new Point[3]; pts[0] = p; pts[1] = cp; pts[2] = dp; ColorPoint DeepPoint Point, widening move int i; for (i=0; i<3; i++) { pts[i].move(10, 10); move print for (i=0; i<3; i++) { pts[i].print(); System.out.println("<br>"); print ColorPoint, DeepPoint? Java print (11, 12) (13, 14) (((((15, 16)))))
5.6. 41 C++ Java Point, ColorPoint, DeepPoint Point* pts[3]; Point* p = new Point(1, 2); ColorPoint* cp = new ColorPoint(3, 4, "green"); DeepPoint* dp = new DeepPoint(5, 6, 5); pts[0] = p; pts[1] = cp; pts[2] = dp; for (i=0; i<3; i++) { pts[i]->print(); cout << "<br>y=n"; Point print (11, 12) (13, 14) (15, 16) C++ Java print pts[i] p, cp, dp Java C++ virtual class Point { // : C++ public: int x, y; void move(int dx, int dy); virtual void print(void); ; C++ static void moveinsquare(point p) { p.move(1, 0); p.print(); System.out.print(" "); p.move(0, 1); p.print(); System.out.print(" "); p.move(-1, 0); p.print(); System.out.print(" "); p.move(0, -1); p.print(); System.out.println("<br>"); moveinsquare ColorPoint DeepPoint print 3
42 5 3 Poly 4 Morph 1 (GUI) Java / Button, Label, TextField, TextArea GUI Component java.awt.component Component setvisible, setenabled, setlocation 5.6.1 HideShow.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class HideShow extends JApplet implements ActionListener { JTextField input; JLabel l1; JButton b1, b2; public void init() { l1 = new JLabel("label"); input= new JTextField("text", 5); b1 = new JButton("Hide"); b1.addactionlistener(this); b2 = new JButton("Show"); b2.addactionlistener(this); getcontentpane().setlayout(new FlowLayout()); getcontentpane().add(l1); getcontentpane().add(input); getcontentpane().add(b1); getcontentpane().add(b2); public void actionperformed(actionevent e) { if (e.getsource()==b1) { l1.setvisible(false); input.setvisible(false); b1.setvisible(false); else if (e.getsource()==b2) { l1.setvisible(true); input.setvisible(true); b1.setvisible(true); repaint(); 3 4 =
5.6. 43 Hide setvisible Component Component add = setvisible,,,