) / Java ( )

Similar documents
1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

ISO-2022-JP (JIS ) 1 2. (Windows95/98 MacOS ) Java UNICODE UNICODE. Java. .java.java.txt native2ascii. .java

1 GUI GUI GUI GUI GUI. GUI(Graphical User Interface) JDK java.awt. ? Component

GUI. interface. CUI Character-Based User Interface Command-Based User Interface GUI. Graphical User Interface GUI. Frame Frame.

Chapter #1. Program to demonstrate applet life cycle

CS108, Stanford Handout #29. MVC Table

MVC Table / Delegation

Original GUIs. IntroGUI 1

/* Write a Program implementing GUI based Calculator using Swing */


Chapter 1 GUI Applications

Uppsala University. Assignment 3. Separation into Model-View TableModel ListModel ( multiple inheritance or adapter) Renderer (delegation)

H212 Introduction to Software Systems Honors

Question 1. Show the steps that are involved in sorting the string SORTME using the quicksort algorithm given below.

Swing JTable. JTable

EINFÜHRUNG IN DIE PROGRAMMIERUNG

II 12, JFileChooser. , 2. SolidEllipse ( 2), PolyLine.java ( 3). Draw.java

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*;

Midterm assessment - MAKEUP Fall 2010

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Programming Language Concepts: Lecture 8

CS193k, Stanford Handout #16

Swing Programming Example Number 2

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

MIT AITI Swing Event Model Lecture 17

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

Graphical Interfaces

JOURNAL OF OBJECT TECHNOLOGY

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

Graphical Interfaces

Example 3-1. Password Validation

Graphical User Interfaces (GUIs)

Laborator 2 Aplicatii Java

G51PRG: Introduction to Programming Second semester Applets and graphics

Chapter 13 Lab Advanced GUI Applications

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

FIFO PAGE REPLACEMENT : import java.io.*; public class FIFO {

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

A sample print out is: is is -11 key entered was: w

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Caesar Cipher program in java (SS)

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

Swing from A to Z Some Simple Components. Preface

User interfaces and Swing

RAIK 183H Examination 2 Solution. November 11, 2013

Week 9. Abstract Classes

JAVA NOTES GRAPHICAL USER INTERFACES

Points Missed on Page page 1 of 8

CSIS 10A Practice Final Exam Solutions

RAIK 183H Examination 2 Solution. November 10, 2014

Packages: Putting Classes Together

Apéndice A. Código fuente de aplicación desarrollada para probar. implementación de IPv6


Client-side GUI. A simple Swing-gui for searching for proudcts

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

AWT DIALOG CLASS. Dialog control represents a top-level window with a title and a border used to take some form of input from the user.

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

(Incomplete) History of GUIs

6O03 project report. Main points for the solution. Build a combination possibility tableau. Problem: Page 5, the prime number problem

Laborator 3 Aplicatii Java

Graphic User Interfaces. - GUI concepts - Swing - AWT

JAVA NOTES GRAPHICAL USER INTERFACES

Programming Languages and Techniques (CIS120)

Swing. Component overview. Java UI, summer semester 2017/2018 1

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

GlobalLogic Technical Question Paper

JAVA PROGRAM EXAMPLE WITH OUTPUT PDF

Programming Languages and Techniques (CIS120e)

javax.swing Swing Timer

University of Cape Town Department of Computer Science Computer Science CSC1017F

Window Interfaces Using Swing Objects

Vinayaka Missions University

MYcsvtu Notes. Java Programming Lab Manual

2.4 Input and Output. Input and Output. Standard Output. Standard Output Abstraction. Input devices. Output devices. Our approach.

Decisions: Logic Java Programming 2 Lesson 7

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Gracefulness of TP-Tree with Five Levels Obtained by Java Programming

RobotPlanning.java Page 1

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

7. Program Frameworks

III/IV B.Tech (Regular/Supplementary) DEGREE EXAMINATION. Electronics & Instrumentation Engineering Object Oriented Programming with JAVA

Practical No: 1 Aim: Demonstrate round - robin scheduling using threads.

protected void printserial() { System.out.println("> NO." + this.serialno); this.serialno++; }

B.Sc (Computer Science) Object Oriented Programming with Java and Data Structures Lab Programs

Lecture 3: Java Graphics & Events

AWT QUADCURVE2D CLASS

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Week -1. Try debug step by step with small program of about 10 to 15 lines which contains at least one if else condition and a for loop.

Lecture 5: Java Graphics

CS108, Stanford Handout #22. Thread 3 GUI

How-To Guide. SigPlus Demo LCD 4x3 Java. Copyright Topaz Systems Inc. All rights reserved.

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Java - Applets. public class Buttons extends Applet implements ActionListener

Chapter 1 Introduction to Java

Basics of programming 3. Java GUI and SWING

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS

Transcription:

2002 3 2003.1.29 1 3 ( ) ( ) / Java ( ) 1

( )? ( ) 2 (3 ) 3 Java Java Java (dynamic dispatch) ( ) import java.awt.*; import java.awt.event.*; public class Sample30 extends Frame { boolean go; double time; Animation[] a = new Animation[20]; int count = 0; public Sample30() { a[count++] = new FlyingCircle(Color.red, 100, 100, 40, 30, -40); a[count++] = new FlyingCircle(Color.blue, 100, 100, 30, 40, 50); a[count++] = new RandomSquare(Color.green, 100, 100, 20, 10); public void paint(graphics g) { for(int i = 0; i < count; ++i) a[i].draw(g); public void start() { go = true; (new Thread(new MyRun())).start(); public void stop() { go = false; class MyRun implements Runnable { public void run() { time = 0.001 * System.currentTimeMillis(); while(go) { try { Thread.sleep(100); catch(exception e) { double dt = 0.001 * System.currentTimeMillis() - time; for(int i = 0; i < count; ++i) a[i].addtime(dt); time += dt; repaint(); 2

public static void main(string[] args) { Sample30 app = new Sample30(); app.addwindowlistener(new WindowAdapter() { public void windowclosing(windowevent evt) { System.exit(0); ); app.setsize(300, 300); app.setvisible(true); app.start(); interface Animation { public void addtime(double dt); public void draw(graphics g); static class FlyingCircle implements Animation { Color col; double gx, gy, rad, vx, vy; public FlyingCircle(Color c, double x, double y, double r, double vx1, double vy1) { col = c; gx = x; gy = y; rad = r; vx = vx1; vy = vy1; public void addtime(double dt) { gx += vx*dt; gy += vy*dt; if(gx < 0 && vx < 0 gx > 300 && vx > 0) vx = -vx; if(gy < 0 && vy < 0 gy > 300 && vy > 0) vy = -vy; public void draw(graphics g) { g.setcolor(col); g.filloval((int)(gx-rad), (int)(gy-rad), (int)rad*2, (int)rad*2); static class RandomSquare implements Animation { Color col; double gx, gy, len, vel; public RandomSquare(Color c, double x, double y, double l, double v) { col = c; gx = x; gy = y; len = l; vel = v; public void addtime(double dt) { gx += vel*(math.random()-0.5); gy += vel*(math.random()-0.5); public void draw(graphics g) { g.setcolor(col); g.fillrect((int)(gx-len/2), (int)(gy-len/2), (int)len, (int)len); 1 /u1a/kuno/work/sample30.java 3

4 MVC import java.awt.*; import java.awt.event.*; public class Sample31 extends Frame { MyApp app; Label l0 = new Label(); Button b0 = new Button(); TextField t0 = new TextField(); public Sample31(MyApp a) { app = a; l0.settext(app.getmessage()); b0.setlabel(app.getname()); setlayout(null); setsize(200, 250); add(l0); l0.setbounds(10, 40, 180, 40); add(t0); t0.setbounds(10, 100, 180, 40); add(b0); b0.setbounds(10, 160, 90, 40); b0.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { app.setdata(t0.gettext()); t0.settext(""); l0.settext(app.getmessage()); ); public static void main(string[] args) throws Exception { // Frame f = new Sample31(new App1()); // Frame f = new Sample31(new App2()); Frame f = new Sample31((MyApp) (Class.forName(args[0])).newInstance()); f.setvisible(true); f.addwindowlistener(new WindowAdapter() { public void windowclosing(windowevent e) { System.exit(0); ); interface MyApp { public String getname(); public String getmessage(); public void setdata(string s); 4

class App1 implements MyApp { String mesg = "F to C converter"; public String getname() { return "F to C"; public String getmessage() { return mesg; public void setdata(string s) { try { float x = (new Float(s)).floatValue(); float y = (9f/5f)*(x - 32f); mesg = "Temp in C: " + y; catch(exception e) { mesg = e.tostring(); class App2 implements MyApp { double sum = 0.0; String mesg = "Calculate Sum"; public String getname() { return "Sum"; public String getmessage() { return mesg; public void setdata(string s) { try { sum += (new Double(s)).doubleValue(); mesg = "Sum is: " + sum; catch(exception e) { mesg = e.tostring(); 2 MyApp 2 /u1a/kuno/work/sample31.java import java.io.*; public class Sample32 { public static void main(string[] args) throws Exception { // MyApp app = new App1()); // MyApp app = new App2()); MyApp app = (MyApp) (Class.forName(args[0])).newInstance(); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); while(true) { System.out.println(app.getMessage()); System.out.print("> "); 5

String line = in.readline(); if(line.equals("")) break; app.setdata(line); interface MyApp { public String getname(); public String getmessage(); public void setdata(string s); class App1 implements MyApp { String mesg = "F to C converter"; public String getname() { return "F to C"; public String getmessage() { return mesg; public void setdata(string s) { try { float x = (new Float(s)).floatValue(); float y = (9f/5f)*(x - 32f); mesg = "Temp in C: " + y; catch(exception e) { mesg = e.tostring(); class App2 implements MyApp { double sum = 0.0; String mesg = "Calculate Sum"; public String getname() { return "Sum"; public String getmessage() { return mesg; public void setdata(string s) { try { sum += (new Double(s)).doubleValue(); mesg = "Sum is: " + sum; catch(exception e) { mesg = e.tostring(); 3 /u1a/kuno/work/sample32.java 2 5 Swing Java API UI Swing JTree JTable ( 1) 6

JTable TableModel 1: TableModel int getrowcount() int getcolumncount() String getcolumname(i) i boolean iscelleditable(r,c) r c getvalueat(r,c) r c setvalueat(object,r,c) r c MouseAdapter AbstractTableModel (getrowcount() getcolumncount() getvalueat() 3 ) AbstractTableModel JTable import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; public class R12Sample2 extends JFrame { public R12Sample2() { getcontentpane().add(new JScrollPane(new JTable(new MyTableModel()))); public static void main(string[] args) { R12Sample2 app = new R12Sample2(); app.setdefaultcloseoperation(jframe.exit_on_close); app.setsize(300, 300); app.setvisible(true); class MyTableModel extends AbstractTableModel { double start = 1.0, step = 0.1; public int getrowcount() { return 25; public int getcolumncount() { return 3; 7

public boolean iscelleditable(int r, int c) { return r == 1 && c == 1; public Object getvalueat(int r, int c) { if(c == 0) { return new Integer(r); else if(c == 1) { double x = start; for(int i = 0; i < r; ++i) x = x + step; return new Double(x); else { return "?"; public void setvalueat(object o, int r, int c) { if(r == 1 && c == 1) { step = (new Double(o.toString())).doubleValue() - 1.0; firetabledatachanged(); (JScrollPane) MyTableModel JTable 1 MyTableModel step double 1 25 3 1 1 ( 0 0 ) 0 1 i 1 step i 2? 1 1 double 1 step AbstractTableModel firetabledatachanged() ( ) ( 1/4 ) 1 ( ) Java implements implements 4 /u1a/kuno/work/sample34.java 1 JScrollPane 8

(a) 1.0 (0 1 ) (b) 1 1 ( ) (c) 2 0 1 a 1 ar 2 2 1 2 ( ) (d) 2 3 prime 4 not prime 10000 (e) (f) 9