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,

Similar documents
Java - Applets. public class Buttons extends Applet implements ActionListener

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 Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Java Applets / Flash

Introduction to Computer Science I

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

Java Applet Basics. Life cycle of an applet:

Java Programming Lecture 6

CHAPTER 2. Java Overview

Java. GUI building with the AWT

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

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

G51PRG: Introduction to Programming Second semester Applets and graphics

Java Applet & its life Cycle. By Iqtidar Ali

Graphics Applets. By Mr. Dave Clausen

Using the API: Introductory Graphics Java Programming 1 Lesson 8

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

Chapter 7 Applets. Answers

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!

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Graphics and Painting

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Java TM Applets. Rex Jaeschke

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

Week 9. Abstract Classes

Advanced Internet Programming CSY3020

To gain experience using GUI components and listeners.

Graphical User Interface (GUI)

Graphical User Interface (GUI)


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

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

User interfaces and Swing

DEMONSTRATION OF AWT CONTROLS

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

EVENTS, EVENT SOURCES AND LISTENERS

Programming Language Concepts: Lecture 8

Advanced Java 2014

GUI in Java TalentHome Solutions

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

Programming graphics

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

Chapter 1 GUI Applications

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

Java Arrays and Collections

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Decisions: Logic Java Programming 2 Lesson 7

Interfaces: Listeners and Adapters Java Programming 3 Lesson 6

Mathematics for Computer Graphics - Lecture 13

Graphics Applets. By Mr. Dave Clausen

Chapter 12 Advanced GUIs and Graphics

Introduction. Introduction

Java Event Handling -- 1

CSC 551: Web Programming. Fall 2001

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

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

CS1004: Intro to CS in Java, Spring 2005

Graphical Applications

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

Window Interfaces Using Swing Objects

CSC 1214: Object-Oriented Programming

Packages: Putting Classes Together

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

MODULE 8p - Applets - Trials B

Interactive Tourist Map

Lab 3: Work with data (IV)

PROGRAMMING LANGUAGE 2

Class 16: The Swing Event Model

MIT AITI Swing Event Model Lecture 17

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Assignment 2. Application Development

Sri Vidya College of Engineering & Technology

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 :) )

Window Interfaces Using Swing Objects

Unit 7: Event driven programming

CISC 1600 Lecture 3.1 Introduction to Processing

Java - Dates and Threads

DUKE UNIVERSITY Department of Computer Science. Midterm Solutions

1 Getting started with Processing

Abstract Factory Pattern

Building Java Programs

Programmierpraktikum

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

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 5: Java Graphics

The AWT Package, An Overview

Garfield AP CS. Graphics

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

IT101. Graphical User Interface

Dr. Hikmat A. M. AbdelJaber

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

Building Java Programs

Applet which displays a simulated trackball in the upper half of its window.

Using Microsoft Excel

Example: Building a Java GUI

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

TWO-DIMENSIONAL FIGURES

Transcription:

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, 5.3.2. Java is not confined to a DOS environment. It can run with buttons and boxes in a Windows like manner. Forte is written in Java and gives a good example of what Java can and cannot do with a GUI. When running graphically Java can run within an applet or as a stand-alone application. Here Java will be run in an applet that requires another program to host the applet. This will usually be the Java Virtual Machine in a web browser or the applet viewer that comes with the JDK. Java 2 has more sophisticated graphics than 1.1 and earlier through the use of swing. Some older browsers (IE 3 and possible IE 4) will not be able to handle swing although an up to date JDK will by use of the applet viewer. VJ will not create swing code but Forte will. 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. Forte allows Buttons to be dragged and dropped and has a go at creating the code. care should be taken here as it is not a good way to see what the code does and the event code generated is less than ideally concise. The buttons that can be drawn with VJ are Microsoft AWT controls and will not work with pure Java. There is a lot of new code here, some key points are: 1

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. 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(); 2

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; //declare a Button //add the Button in init() public void actionperformed(actionevent event) if(event.getsource()==push) 3

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 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; 4

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 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 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; 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. 5

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. You may have to change the solution properties to ensure that the web page loads and not the applet when running the solution. 6