Java - Applets. public class Buttons extends Applet implements ActionListener

Similar documents
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,

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

Java Programming Lecture 6

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

CHAPTER 2. Java Overview

Java. GUI building with the AWT

Java Applets / Flash

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

G51PRG: Introduction to Programming Second semester Applets and graphics

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

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

User interfaces and Swing

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

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!

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

To gain experience using GUI components and listeners.

Java Applet & its life Cycle. By Iqtidar Ali

Java Applet Basics. Life cycle of an applet:

Graphics Applets. By Mr. Dave Clausen

Chapter 7 Applets. Answers

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Week 9. Abstract Classes

Graphics and Painting

Using the API: Introductory Graphics Java Programming 1 Lesson 8

EVENTS, EVENT SOURCES AND LISTENERS

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

Introduction. Introduction

Programming Language Concepts: Lecture 8

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

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

Introduction to Computer Science I

Java TM Applets. Rex Jaeschke

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

MIT AITI Swing Event Model Lecture 17

Abstract Factory Pattern (Creational Pattern) Week 05 (2nd Half) Report 05 A. Firuza Aibara P11119

Decisions: Logic Java Programming 2 Lesson 7

Part 3: Graphical User Interface (GUI) & Java Applets

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Graphical User Interface (GUI)

Chapter 12 Advanced GUIs and Graphics

Advanced Internet Programming CSY3020

Graphics Applets. By Mr. Dave Clausen

Graphical User Interface (GUI)

Graphic User Interfaces. - GUI concepts - Swing - AWT

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Chapter 1 GUI Applications

Interfaces: Listeners and Adapters Java Programming 3 Lesson 6

Java Arrays and Collections

AP CS Unit 11: Graphics and Events

Dr. Hikmat A. M. AbdelJaber

DEMONSTRATION OF AWT CONTROLS

Class 16: The Swing Event Model

Unit 1- Java Applets. Applet Programming. Local Applet and Remote Applet ** Applet and Application

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

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

CS 170 Java Programming 1. Week 9: Learning about Loops

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Mathematics for Computer Graphics - Lecture 13

Repetition. We might start out with the following program that draws only the left most object.

MODULE 8p - Applets - Trials B

CSC 551: Web Programming. Fall 2001

Packages: Putting Classes Together

Graphical Applications

DUKE UNIVERSITY Department of Computer Science. Midterm Solutions


CSC 1214: Object-Oriented Programming

Text. Text metrics. There are some important metrics that we must consider when working with text. Figure 4-1 shows the basics.

Assignment 2. Application Development

Advanced Java 2014

Unit 7: Event driven programming

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education

Programming graphics

CS1004: Intro to CS in Java, Spring 2005

CISC 1600 Lecture 3.1 Introduction to Processing

H212 Introduction to Software Systems Honors

Java - Dates and Threads

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Adding Buttons to StyleOptions.java

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

JApplet. toy example extends. class Point { // public int x; public int y; } p Point 5.2. Point. Point p; p = new Point(); instance,

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Window Interfaces Using Swing Objects

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

Programmierpraktikum

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Building Java Programs

Lab 3: Work with data (IV)

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

Object-Oriented Programming EE219 Repeat 2004/2005 Page 1 of 8

GUI in Java TalentHome Solutions

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

2. Write a program to convert 69F into its equivalent centigrade temperature.

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Java Event Handling -- 1

Swing - JButton. Adding buttons to the main window

Example: Building a Java GUI

Abstract Factory Pattern

Transcription:

Java - Applets Java code here will not use swing but will support the 1.1 event model. Legacy code from the 1.0 event model will not be used. This code sets up a button to be pushed: import java.applet.*; import java.awt.*; import java.awt.event.*; public class Buttons extends Applet implements ActionListener private Button push; public void init() push = new Button("Push Me"); add(push); push.addactionlistener(this); public void actionperformed(actionevent event) if(event.getsource()==push) push.setbackground(color.blue); The Button turns blue when pushed. This is a lot of code for a result that could be done in about 3 lines with VB or C#. There is a lot of new code here, some key points are: 1. The import statement is used to reference parts of Java and their methods that are not included by default. Import is similar to #include<> in C. 2. public static void main (String args[]) has gone. With applets public void init() acts as the main start up method. 3. Button like String is a Class so merits a capital letter. 4. init() is used to create the controls and actionperformed is fired when the control is clicked. With many controls some sort of selection is required to tell what is going on. 1

Modifications 1. Add some more controls, they do not have to do anything. All controls must first be declared then added. Some will also deserve some sort of event listener. TextField allows data to be typed in, the number of characters in the TextField must be specified. mytextfield = new TextField(10); //holds 10 characters TextArea is the big brother it holds more than 1 row of data. mytextarea = new TextArea(10,5) //10 by 5 Label allows instructions to be shown. Data cannot be typed into a Label. mylabel = new Label("A Caption"); 2. Have pushing a Button put some text in a TextField. If the TextField was called output the key line would be: output.settext("he pushed me"); This is fired by the condition - event.getsource()= =push 3. Write code to input data from a TextArea and output it to another. If a String is declared called message the code to put the TextArea text into the String would be: message =input.gettext(); Swing Modifications Swing came out with Java 2. It included a lot of updates and improvements but the basics of Java remained the same. One of the obvious changes was the addition of another set of widget objects. Recreate the above Applet in Swing. Import javax.swing.* 2

Change the widget class names to start with J. For example Button becomes JButton. Drawing on the canvas Applets make a lot of use of drawing to place shapes lines and words onto the screen. This is handled by the paint(graphics g) method. Add this method the current program. public void paint(graphics g) g.drawstring("a String",10,70); Graphics is a Java library within java.awt, g.drawstring is the method to draw a String at the x, y co-ordinates 10,70. Add code to draw squares, filled squares, circles and filled circles within paint, draw String next to the shapes to say what they are. These are all methods of g, for example: g.drawoval(40,80,140,180); // the 4 co-ordinates represent a box within which the shape is drawn. To draw the circle I must specify the 4 corners of the square surrounding it So far all the shapes and text have been black. Colour can be changed by the Graphics method setcolor. When typing the dot operator after Color the drop down shows all the colours that are available within Java. Note there are not very many of them. g.setcolor(color.red); //this code will draw a red oval. g.drawoval(40,80,140,180); Adapt the shape code to draw each shape and its corresponding descriptive String in a different colour. Drawing with an event It is more useful if drawings are created by a user event such as clicking a Button or by some decision in code. This is achieved by calling the paint method from an actionperformed or other place in code through repaint(). This code enables a Button to change the drawing colour to blue. private Color drawcolour; 3

//declare a Button //add the Button in init() public void actionperformed(actionevent event) if(event.getsource()==push) drawcolour = Color.blue; //assign a different colour to the Color variable repaint(); //call painy public void paint(graphics g) g.setcolor(drawcolour); g.drawoval(40,80,140,180); Modifications 1. Set up 2 TextFields and 2 Buttons each Button draws the text from its corresponding TextField onto the screen. 2. Allow the user to choose from 3 colours to display the Strings. This can be done with Buttons a better approach is described below. Check box groups These are the same as option buttons in Microsoft land. They allow 1 choice from amongst the group. They act quite similarly to Buttons except rather than using ActionListener they respond to ItemListener so the Class has to be extended to include this. private Color drawcolour; private CheckboxGroup colours; //declare the group private Checkbox red, orange, green; //these are the Checkboxes within the group public void init() colours = new CheckboxGroup(); //new the CheckboxGroup red = new Checkbox("Red",colours, true); //red is default add(red); red.additemlistener(this); // note additemlistener not addactionlistener orange = new Checkbox("Orange",colours, false); // colours is the group add(orange); orange.additemlistener(this); green = new Checkbox("Green",colours, false); add(green); green.additemlistener(this); //add other controls 4

public void itemstatechanged(itemevent e) if(e.getsource()==red) drawcolour=color.red; if(e.getsource()==orange) drawcolour=color.orange; if(e.getsource()==green) drawcolour=color.green; Fonts All the previous code has used a default font style and size, g.setfont( )allows this to be changed. Not surprisingly Font is a Class and an object from that Class is created with new. Create a new Font object and stop when the '(' is typed. Note what is needed to specify a Font Declare this Font with the variables at the top of the Class. Font text = new Font(""SanSerif",Font.BOLD,18); Then set the Font in paint(graphics g). g.setfont(text); Some example font names are: SanSerif Serif Monospaced TimesRoman Modifications name is restricted to the few names of Fonts that Java supports. These must be typed correctly. style is normal (0), bold (1), or italic (2). Bold + italic is 3. size is the font size, any smallish integer will do. 1. Set up a CheckboxGroup to allow the user to select the font that text is displayed in. 2. The font style could be normal, italic and/or bold. A CheckboxGroup would not be appropriate here but individual CheckBoxes that are not part of a group can be selected in multiples. These can be used to set the value of style. Note that style is an integer so can be declared as such and altered by the itemstatechanged code 5

for each CheckBox. A possible solution is to have an integer for bold and italic and add them up when setting the font. The values of bold and italic are set by the itemstatechanged methods for the relevant CheckBoxes. To tell if they have been checked or not getstate is required. if(e.getsource()==bold) //reference the control boolean state = bold.getstate(); //is it checked or not if(state) //or if(state == true) ibold = 1; else ibold =0; Font text can then be declared in paint rather than at the top of the class, it will get its font style from the value of bold. Italic will work in the same fashion. Font text = new Font("SanSerif",iBold,18); 3. Add a TextField to allow the user to type in the font size. 4. All the code so far has been run in applet viewer. Applets will often be run on the web using a browser. The HTML code to run an applet is just another tag. <applet code ="Gui.class" height =400 width = 500 ></applet> The Class name must be that of the Class that is being run. Height and width correspond to how much of the HTML page space is allocated to the applet. VJ will allow a blank web page to be added to an existing solution. The page has to be viewed in source to edit the code. 6