cs Java: lecture #5

Similar documents
Graphics Applets. By Mr. Dave Clausen

Graphics Applets. By Mr. Dave Clausen

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

TWO-DIMENSIONAL FIGURES


G51PRG: Introduction to Programming Second semester Applets and graphics

AP CS Unit 12: Drawing and Mouse Events

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

cs Java: lecture #6

9. APPLETS AND APPLICATIONS

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Graphic User Interfaces. - GUI concepts - Swing - AWT

The AWT Package, An Overview

PIC 20A GUI with swing

Chapter 7 Applets. Answers

Unit 7: Event driven programming

Java Applet Basics. Life cycle of an applet:

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Graphics and Painting

Appendix F: Java Graphics

Appendix F: Java Graphics

PESIT Bangalore South Campus

CHAPTER 2. Java Overview

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Java - Applets. public class Buttons extends Applet implements ActionListener

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

MaSH Environment graphics

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Graphics -- To be discussed

1 Getting started with Processing

Java Programming Lecture 6

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Lecture 7 A First Graphic Program And Data Structures & Drawing

Lab 3: Work with data (IV)

Programming: You will have 6 files all need to be located in the dir. named PA4:

Chapter 14: Applets and More

Java. GUI building with the AWT

Chapter 12 Advanced GUIs and Graphics

Lecture 5: Java Graphics

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Chapter 3 - Introduction to Java Applets

Graphics and Java2D. Objectives

10/16/2008. CSG 170 Round 5. Prof. Timothy Bickmore. User Analysis Task Analysis (6) Problem Scenarios (3)

GUI in Java TalentHome Solutions

Introduction to the JAVA UI classes Advanced HCI IAT351

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Lecture 3: Java Graphics & Events

User interfaces and Swing

AplusBug dude = new AplusBug(); A+ Computer Science -

CSC207H: Software Design Lecture 11

Introduction to Java Applets 12

CSIS 10A Assignment 14 SOLUTIONS

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Java Coordinate System

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Some classes and interfaces used in this chapter from Java s original graphics capabilities and from the Java2D API.

BASICS OF GRAPHICAL APPS

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

2IS45 Programming

Event Driven Programming

Programming graphics

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Graphical User Interfaces 2

Graphical User Interface (GUI)

1 Getting started with Processing

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

SIMPLE APPLET PROGRAM

UNIT -1 JAVA APPLETS

Graphical User Interface (GUI)

Creating Rich GUIs in Java. Amy Fowler Staff Engineer JavaSoft

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

Graphics in Swing. Engineering 5895 Faculty of Engineering & Applied Science Memorial University of Newfoundland

CS1004: Intro to CS in Java, Spring 2005

Java TM Applets. Rex Jaeschke

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

The Java Programming Language Basics. Identifiers, Keywords, and Types. Expressions and Flow Control. Object-Oriented Programming. Objects and Classes

Building Java Programs

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Applets and the Graphics class

Lab 10: Inheritance (I)

Programmierpraktikum

Using Graphics. Building Java Programs Supplement 3G

Graphical User Interfaces (GUIs)

Here is a list of a few of the components located in the AWT and Swing packages:

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

(Refer Slide Time: 02:01)

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Sri Vidya College of Engineering & Technology

Transcription:

cs3101-003 Java: lecture #5 news: homework #4 due today homework #5 out today today s topics: applets, networks, html graphics, drawing, handling images graphical user interfaces (GUIs) event handling interfaces cs3101-003-java-spring2003-sklar-lect05 1

networks (1). two or more computers connected to each other networked computers can share information and resources, e.g.: printer file server example: CUNIX system connections: point-to-point computers are directly connected to each other message speed is fast adding computers is expensive single communication line message speed can be slow adding computers is cheap cs3101-003-java-spring2003-sklar-lect05 2

networks (2). network address uniquely identifies each computer on the network packet long messages are split into pieces each piece is a packet, sent individually along the network improves message speed local-area network (LAN) designed to span short distances wide-area network (WAN) designed to span longer distances connects multiple LANs cs3101-003-java-spring2003-sklar-lect05 3

networks (3). the Internet developed in the 1970s as ARPANET the ultimate WAN a network of networks protocol set of rules governing communication TCP/IP (transmission control protocol / internet protocol) IP address = network address on the Internet numeric, e.g., 204.192.116.2 also have text equivalents called Internet addresses, which are comprised of local computer names (i.e., name of computer on LAN) plus domain names (i.e., name of LAN on WAN) domain names are controlled by the Internet Naming Authority domain name system (DNS) translates between IP address and Internet address cs3101-003-java-spring2003-sklar-lect05 4

networks (4). the World Wide Web (WWW) provides standard method of interfacing to the Internet from the user level uses hypertext non-linear method of organizing information refers only to textual information hypermedia refers to non-textual information, such as sound, video and graphics browser user program that provides method of viewing WWW documents early browsers included: Archie, Gopher Mosiac first graphical WWW browser released in 1993 became Netscape cs3101-003-java-spring2003-sklar-lect05 5

networks (5). HyperText Markup Language (HTML) standard format for WWW documents Uniform Resource Locator (URL) unique document address on the WWW HypterText Transfer Protocol (http) protocol used for transfering HTML documents provides one-way transfer from server to client other protocols include: ftp, telnet these provide two-way transfer between server and client Java grew out of the above allows two-way transfer text, graphics, sound cs3101-003-java-spring2003-sklar-lect05 6

networks (6). client-server architecture comes from operating system design methodology by which tasks are divided onto different processors according to functionality programs can be divided into: computation portion drawing or output portion each portion can be executed on a different CPU X windows windowing system used under UNIX with X windows, the drawing is done on the client, although the execution may be happening on a different physical machine, the server cs3101-003-java-spring2003-sklar-lect05 7

applets (1). Java programs can run as applications or applets application: executed using the java command server and client can be the same machine or different machines client invokes JVM which interprets classes and runs them applet: must be executed using a browser, like Netscape, or the appletviewer command server sends applet to the client, in the form of class files; applet invokes JVM which interprets classes and runs them on the client there are two parts: an HTML file used to invoke the applet Java class file(s) that contain the applet code cs3101-003-java-spring2003-sklar-lect05 8

applets (2). file name = hi.html <html> <title> sample applet page </title> the applet will be shown below... <applet code="hi.class" width=400 height=400> </applet> </html> cs3101-003-java-spring2003-sklar-lect05 9

applets (3). file name = hi.java import java.awt.*; import java.applet.applet; public class hi extends Applet { public void paint( Graphics g ) { g.drawstring( "hi",10,10 ); } // end of paint() } // end of class hi cs3101-003-java-spring2003-sklar-lect05 10

applets (4). java.awt package Abstract Windowing Toolkit (AWT) classes that support graphical user interfaces (GUI) includes java.awt.component method: public void paint() java.applet.applet class public void init() public void start() public void stop() cs3101-003-java-spring2003-sklar-lect05 11

frames (1). used when you want to write an application that has graphics relationship between Frame and Applet: java.lang.object +--java.awt.component +--java.awt.container +--java.awt.panel +--java.applet.applet java.lang.object +--java.awt.component +--java.awt.container +--java.awt.window +--java.awt.frame cs3101-003-java-spring2003-sklar-lect05 12

frames (2). import java.awt.*; public class hiho extends Frame { mycanvas c; public hiho( String title ) { super( title ); } // end of hiho constructor public static void main( String[] args ) { hiho h = new hiho( "myframe!" ); h.c = new mycanvas(); h.add( h.c ); h.setsize( 100,100 ); h.setbackground( Color.red ); h.show(); } // end of main() } // end of class hiho cs3101-003-java-spring2003-sklar-lect05 13

frames (3). import java.awt.*; public class mycanvas extends Canvas { public Dimension getminimumsize() { return( new Dimension( 90,90 )); } // end of getminimumsize() public void paint( Graphics g ) { setbackground( Color.blue ); g.drawstring( "hiho",10,10 ); } // end of paint() } // end of class mycanvas cs3101-003-java-spring2003-sklar-lect05 14

graphics (1). java.awt.graphics class X-windows coordinate system drawing primitives: lines Strings rectangles ovals arcs color cs3101-003-java-spring2003-sklar-lect05 15

graphics (2). simple methods from the java.awt.graphics class void drawline( int x1, int y1, int x2, int y2 ); draws a line connecting (x1,y1) and (x2,y2 ); void drawstring( String str, int x, int y ); draws the text in str, with its lower left corner at (x,y) cs3101-003-java-spring2003-sklar-lect05 16

graphics (3). import java.awt.*; import java.applet.applet; public class hi2 extends Applet { public void paint ( Graphics g ) { g.drawstring( "hello world!",10,10 ); g.drawline( 0,400, 400,0 ); } // end of paint() } // end of class hi2() cs3101-003-java-spring2003-sklar-lect05 17

graphics (4). bounding rectangles coordinates of origin (upper left corner) extent (width and height) arcs measured in degrees starting from 0 (along positive X-axis) extent (total angle of arc) cs3101-003-java-spring2003-sklar-lect05 18

graphics (5). methods from the java.awt.graphics class for drawing outlines of shapes void drawrect( int x, int y, int width, int height ); draws a rectangle with its upper left corner at (x,y), extending the specified width and height void drawoval( int x, int y, int width, int height ); draws an oval circumscribed in the bounding rectangle with its upper left corner at (x,y), extending the specified width and height void drawarc( int x, int y, int width, int height, int startangle, int arcangle ); draws an arc whose oval is circumscribed in the bounding rectangle with its upper left corner at (x,y), extending the specified width and height, where the arc starts at the startangle, measured in degrees (where 0 ) is horizontal along the positive x-axis), extending for arcangle degrees cs3101-003-java-spring2003-sklar-lect05 19

graphics (6). import java.awt.*; import java.applet.applet; public class hi3 extends Applet { public void paint ( Graphics g ) { g.drawrect( 10,300,25,25 ); g.drawoval( 10,250,25,25 ); g.drawarc( 10,200,25,25,45,90 ); } // end of paint() } // end of class hi3() cs3101-003-java-spring2003-sklar-lect05 20

graphics (7). methods from the java.awt.graphics class for drawing filled shapes void fillrect( int x, int y, int width, int height ); draws a filled rectangle with its upper left corner at (x,y), extending the specified width and height void filloval( int x, int y, int width, int height ); draws a filled oval circumscribed in the bounding rectangle with its upper left corner at (x,y), extending the specified width and height void fillarc( int x, int y, int width, int height, int startangle, int arcangle ); draws a filled arc whose oval is circumscribed in the bounding rectangle with its upper left corner at (x,y), extending the specified width and height, where the arc starts at the startangle, measured in degrees (where 0 ) is horizontal along the positive x-axis), extending for arcangle degrees cs3101-003-java-spring2003-sklar-lect05 21

graphics (8). methods from the java.awt.graphics class for drawing polygons void drawpolygon(int[] xpoints, int[] ypoints, int npoints); draws a closed polygon defined by arrays of x and y coordinates void drawpolygon( Polygon p ); draws the outline of a polygon defined by the specified Polygon object void drawpolyline( int[] xpoints, int[] ypoints, int npoints ); draws a sequence of connected lines defined by arrays of x and y coordinates the first two have counterparts for drawing filled polygons: void fillpolygon(int[] xpoints, int[] ypoints, int npoints); void fillpolygon( Polygon p ); cs3101-003-java-spring2003-sklar-lect05 22

graphics (9). java.awt.color class color is defined using the RGB methodology Red, Green, Blue each is an integer between 0 and 255, where 0 means no color and 255 means maximum color so white is: red=255 green=255 blue=255 or the ordered triple (255,255,255) and black is: red=0 green=0 blue=0 and red is: red=255 green=0 blue=0 and green is: red=0 green=255 blue=0 and blue is: red=0 green=0 blue=255 make up your own colors... cs3101-003-java-spring2003-sklar-lect05 23

graphics (10). even more methods from the java.awt.graphics class void setcolor( Color color ); sets the foreground (pen) color to the specified color void fillrect( int x, int y, int width, int height ); draws a filled rectangle with its upper left corner at (x,y), extending the specified width and height void filloval( int x, int y, int width, int height ); draws a filled oval circumscribed in the bounding rectangle with its upper left corner at (x,y), extending the specified width and height void fillarc( int x, int y, int width, int height, int startangle, int arcangle ); draws a filled arc whose oval is circumscribed in the bounding rectangle with its upper left corner at (x,y), extending the specified width and height, where the arc starts at the startangle, measured in degrees (where 0 ) is horizontal along the positive x-axis), extending for arcangle degrees cs3101-003-java-spring2003-sklar-lect05 24

graphics examples. snowman.java bullseye.java snowman2.java, man.java (with helper class) snowman3.java (animated) snowman4.java (another way of doing animation) cs3101-003-java-spring2003-sklar-lect05 25

GUIs (1). GUI = Graphical User Interface java.awt classes: Component Container LayoutManager Event java.awt.event classes: ActionListener ItemListener KeyListener MouseListener MouseMotionListener cs3101-003-java-spring2003-sklar-lect05 26

GUIs (2). components a component is a building block of any GUI here are some examples: Label TextField, TextArea Button Checkbox, CheckboxGroup Choice List cs3101-003-java-spring2003-sklar-lect05 27

GUIs (3). containers a container is a special component that can hold other components here are some examples: Applet Frame Panel cs3101-003-java-spring2003-sklar-lect05 28

GUIs (4). layout managers a layout manager describes where the components are laid out within a given container you need to set the layout manager for each container you can nest containers (and their layour managers) BorderLayout simplest layout manager looks like this: north west center east south GridBagLayout more complex layout manager; but gives you the most control cs3101-003-java-spring2003-sklar-lect05 29

GUIs (5). listeners are interfaces you need to implement the appropriate listener(s), depending on what events you want to handle then you need to override each method in the interface e.g., for a KeyListener, you need: keypressed() keytyped() keyreleased() the body of a method can be empty, if you don t want to do anything when a given event occurs cs3101-003-java-spring2003-sklar-lect05 30

events (1). an event represents some action on the part of the user user-generated events are entered either through the mouse or the keyboard examples: mouse pressed mouse released mouse clicked mouse entered mouse exited mouse moved mouse dragged cs3101-003-java-spring2003-sklar-lect05 31

listeners (1). a listener is a part of a program that captures these events for processing in the program frequently, a listener interface is created for example, java.awt.event.mouselistener: void mousepressed( MouseEvent evt ); void mousereleased( MouseEvent evt ); void mouseclicked( MouseEvent evt ); void mouseentered( MouseEvent evt ); void mouseexited( MouseEvent evt ); what is a MouseEvent? Point getpoint(); int getx(); int gety(); int getclickcount(); cs3101-003-java-spring2003-sklar-lect05 32

listeners (2). another example, java.awt.event.keylistener: void keypressed( KeyEvent evt ); void keyreleased( KeyEvent evt ); void keytyped( KeyEvent evt ); what is a KeyEvent? char getkeycode(); cs3101-003-java-spring2003-sklar-lect05 33

GUI examples. MouseListener examples: Dots.java Dots2.java Dots3.java KeyListener examples: Dots4.java Dots5.java gui.java, snowflake.java cs3101-003-java-spring2003-sklar-lect05 34

interfaces (1). an interface is a group of abstract methods that are defined by all classes that implement the interface an abstract method is one that does not have an implementation, i.e., there is no body of code for the method polymorphism means having many forms lets us use different implementations of a single interface binding happens when a particular implementation is locked to an interface this can happen at compile time or at run time an example of a run-time or dynamic binding is: ((Philosopher)current).pontificate(); from the example to follow cs3101-003-java-spring2003-sklar-lect05 35

interfaces (2). public interface Speaker { public void speak(); public void announce( String str ); } // end of Speaker interface cs3101-003-java-spring2003-sklar-lect05 36

interfaces (3). public class Philosopher implements Speaker { private String philosophy; public Philosopher ( String thoughts ) { philosophy = thoughts; } // end of Philosopher constructor public void speak () { System.out.println( philosophy ); } // end of Philosopher method speak public void announce ( String announcement ) { System.out.println( announcement ); } // end of Philosopher method announce public void pontificate() { for ( int i=0; i<5; i++ ) System.out.println( philosophy ); } // end of Philosopher method pontificate } // end of Philosopher class cs3101-003-java-spring2003-sklar-lect05 37

interfaces (4). public class Dog implements Speaker { public void speak() { System.out.println( "woof" ); } // end of Dog method speak public void announce( String arf ) { System.out.println( "woof: " + arf ); } // end of Dog method announce } // end of class Dog cs3101-003-java-spring2003-sklar-lect05 38

interfaces (5). public class Talking { public static void main( String[] args ) { Speaker current; current = new Dog(); current.speak(); current = new Philosopher( "I think, therefore I am." ); current.speak(); ((Philosopher)current).pontificate(); } // end of main() } // end of Talking class cs3101-003-java-spring2003-sklar-lect05 39

exercise. look at the examples in the class web page: http://www.cs.columbia.edu/ cs3101/examples/gallery.html copy the code for one of the snowman examples copy the code for the gui example replace the snowflake in the gui with the snowman change the components of the gui to do something interesting with the snowman cs3101-003-java-spring2003-sklar-lect05 40

advanced graphics (1): fonts. fonts in Java are defined using the java.awt.font class you can see which fonts (by name) are available in your system by using the java.awt.graphicsenvironment.getallfonts() method you can get information about the point size of the font, whether it is italic, bold or plain you will most likely want to get the size of a string that might be drawn with the current font. first you need to create a FontMetrics object, then you can call the FontMetrics.stringWidth( String str ) method to find the width cs3101-003-java-spring2003-sklar-lect05 41

advanced graphics (2): fonts, continued. useful font properties available in FontMetrics: ascent the distance from the font s baseline to the top of an alphanumeric character use int FontMetrics.getMaxAscent() descent the distance from the font s baseline to the bottom of an alphanumeric character with descenders use int FontMetrics.getMaxDescent() height the distance between the baseline of adjacent lines of text; the sum of the leading + ascent + descent. leading aka interline spacing; the logical amount of space to be reserved between the descent of one line of text and the ascent of the next line advance the distance from the leftmost point to the rightmost point on the string s baseline use int FontMetrics.charWidth( char ch ) to get the advance of the char argument cs3101-003-java-spring2003-sklar-lect05 42

advanced graphics (3): images. you can load images from a URL and draw them load them using java.applet.getimage( URL url ) for an applet or java.awt.toolkit.getimage( URL url ) for an application draw them using Graphics.drawImage() this method there are a number of versions of note that an Image is a Java object unto itself, defined in the java.awt package also note that URL is a Java object that must be instantiated prior to using either of the getimage() methods cs3101-003-java-spring2003-sklar-lect05 43

advanced graphics (4): animation. computer animation is kind of like an old-fashioned flip book you need to draw the object(s) being animated repeatedly, in each new location each time, you calculate the new position of the object(s) and redraw you can either redraw the entire scene or you can only redraw the object(s) that are moving but in the second case, you need to erase the object first, then move it to its new location and redraw the erasing part can be tricky if the background is not solid cs3101-003-java-spring2003-sklar-lect05 44

advanced graphics (5): GridBagLayout. GridBagLayout you place components in the container in rows and columns you can specify the number of rows and columns you can specify the spacing between each row and/or column you can specify how a component is placed within its row/column, if it is smaller than the space allocated note that the height of an entire row is uniform, even if the components in each column are of different heights and the same for the width of a column all these are specified using a GridBagConstraints object cs3101-003-java-spring2003-sklar-lect05 45

advanced graphics (6): GridBagLayout, continued. GridBagConstraints( int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady ); gridx, gridy specify the location of the component, starting from (0,0) gridwidth, gridheight specify how many columns/rows the component occupies weightx, weighty specify how to distribute extra horizontal and vertical space anchor specifies where to place a component when it is smaller than its display area (e.g., CENTER, NORTH, NORTHEAST,...) fill specifies whether to resize a component if it is smaller than its display area (e.g., NONE, HORIZONTAL, VERTICAL, BOTH) insets specifies minimum amount of space between a component and the edges of its display area (external padding) ipadx, ipady specifies how much space to add to the minimum width and height of the component (internal padding) cs3101-003-java-spring2003-sklar-lect05 46