2018 06 08 11:00 12:00 I. I III II. III. IV. ( a d) V. VI. 80 40 40 100 60 : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeylistener KE KeyEvent M MouseListener am addmouselistener ME MouseEvent pl System.out.println pf System.out.printf MouseTest.java, LeftRightButton3.java, Left- RightButton4.java, 1
I. (i) Foo public (A). javac Foo.java java Foo.class (C). javac Foo java Foo (ii)? (B). javac Foo.java java Foo (D). javac Foo java Foo.class (A). Java JavaScript (B). Java C (C). Java (D). Java II. java.awt.polygon java.awt. Graphics Java TM API 2
java.awt Polygon Polygon... public Polygon() public void addpoint(int x, int y) Polygon : x - X y - Y public boolean contains(int x, int y) Polygon : : x - X y - Y Polygon (x,y) true false java.awt Graphics... public void fillpolygon(polygon p) Polygon... : p - 3
: PolygonTest.java 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class PolygonTest extends JPanel (i) { 6 private Polygon polygon; 7 private Color color = Color.YELLOW; 8 9 public PolygonTest() { 10 setpreferredsize(new Dimension(120, 120)); 11 12 polygon = new Polygon(); 13 (ii-1) 14 (ii-2) 15 (ii-3) 16 (ii-4) 17 18 addmouselistener(this); 19 20 21 @Override 22 protected void paintcomponent(graphics g) { 23 super.paintcomponent(g); 24 g.setcolor(color); 25 g.fillpolygon(polygon); 26 27 28 public void mouseclicked(mouseevent e) { 29 int x = e.getx(), y = e.gety(); 30 31 if ( (iii) ) { 32 color = Color.YELLOW; 33 else { 34 color = Color.BLUE; 35 36 repaint(); 37 38 39 /* MouseListener main */ 40 (10, 10) (10, 110) (110, 110) (40, 80) 4 YELLOW BLUE 4
(i) (ii-1) (ii-4) polygon (10, 10) (10, 110) (110, 110) (40, 80) 4 4 (iii) (x, y) polygon 5
III. 2 (96, 128) @ x : x y : y Java x : 30 x : -30 y : -30 y : 30 y : -30 y : 30 x : 30 x : -30 : StringMover.java 1 import java.awt.*; 2 import java.awt.event.*; 3 4 import javax.swing.*; 5 6 public class StringMover extends JPanel implements ActionListener { 7 private JTextField xtf, ytf; 8 private int x = 96, y = 128; 9 10 public StringMover() { 11 setpreferredsize(new Dimension(192, 224)); 12 add(new JLabel("x : ")); 13 xtf = new JTextField("0", 3); 14 add(xtf); 15 add(new JLabel("y : ")); 16 ytf = new JTextField("0", 3); 17 add(ytf); 18 6
19 (i-1) 20 (i-2) 21 22 23 public void actionperformed(actionevent e) { 24 Object source = e.getsource(); 25 if (source == xtf) { 26 int dx = Integer.parseInt(xTF.getText()); 27 x += dx; 28 xtf.settext("0"); 29 else if (source == ytf) { 30 int dy = Integer.parseInt(yTF.getText()); 31 y += dy; 32 ytf.settext("0"); 33 34 repaint(); 35 36 37 @Override 38 protected void paintcomponent(graphics g) { 39 super.paintcomponent(g); 40 g.drawstring("@", x, y); 41 42 /* main */ 43 (i-1) (i-2) 2 StringMover.java StringMover2.java : StringMover2.java 1 import java.awt.*; 2 import java.awt.event.*; 3 4 import javax.swing.*; 5 6 public class StringMover2 (ii) { 7 /* StringMover.java 7 8 */ 8 public StringMover2() { 9 /* StringMover.java 11 17 */ 10 11 xtf.addactionlistener( 12 (iii) 13 ); 14 ytf.addactionlistener( 15 (iv) 16 ); 7
17 18 19 /* paintcomponent, main */ 20 (ii) (iii), (iv) StringMover2 (iii) (iv) /* StringMover.java */ 8
MouseTest.java, LeftRightButton3.java, LeftRightButton4.java, main MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MouseTest extends JPanel implements MouseListener { private int x = 50, y = 20; public MouseTest() { setpreferredsize(new Dimension(150, 150)); addmouselistener(this); public void mouseclicked(mouseevent e) { x = e.getx(); y = e.gety(); repaint(); public void mousepressed(mouseevent e) { public void mousereleased(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { @Override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring("hello WORLD!", x, y); /* main */ LeftRightButton3.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LeftRightButton3 extends JPanel { private int x = 20; public LeftRightButton3() { setpreferredsize(new Dimension(200, 70)); JButton lbtn = new JButton("Left"); JButton rbtn = new JButton("Right"); lbtn.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { x -= 10; repaint(); ); rbtn.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { x += 10; repaint(); ); setlayout(new FlowLayout()); add(lbtn); add(rbtn); 9
@Override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring("hello WORLD!", x, 55); /* main */ LeftRightButton4.java import javax.swing.*; import java.awt.*; public class LeftRightButton4 extends JPanel { private int x = 20; public LeftRightButton4() { setpreferredsize(new Dimension(200, 70)); JButton lbtn = new JButton("Left"); JButton rbtn = new JButton("Right"); lbtn.addactionlistener(e -> { x -= 10; repaint(); ); rbtn.addactionlistener(e -> { x += 10; repaint(); ); setlayout(new FlowLayout()); add(lbtn); add(rbtn); @Override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring("hello WORLD!", x, 55); /* main */ 10
2018 06 08 I. 3 2 (i). (ii). II. (i). (ii). 4, 6, 6 III. (iii). (i). 4, 4, 5, 5 (ii). (iii). /* */ xtf.addactionlistener( );
(iv). /* */ ytf.addactionlistener( );