Java - Dates and Threads

Similar documents
Java Applet Basics. Life cycle of an applet:

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

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

Decisions: Logic Java Programming 2 Lesson 7

9. APPLETS AND APPLICATIONS

G51PRG: Introduction to Programming Second semester Applets and graphics

TWO-DIMENSIONAL FIGURES

Java - Applets. public class Buttons extends Applet implements ActionListener

Date & Time Handling In JAVA

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,

Programmierpraktikum


Chapter 7 Applets. Answers

Introduction to the Java T M Language

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

9Introducing. Concurrency with Threads

EE219 - Semester /2009 Solutions Page 1 of 10

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

Using the API: Introductory Graphics Java Programming 1 Lesson 8

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

COURSE: Introduction to JAVA Programming GRADE(S): UNIT 1: Introduction to Applets NATIONAL STANDARDS: ALL STUDENTS

Week 9. Abstract Classes

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

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

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

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

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

IT101. Graphical User Interface

Essential Series. Springer-Verlag London Ltd.

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

Java Programming Lecture 6

Lab 3: Work with data (IV)

AP CS Unit 12: Drawing and Mouse Events

Solution Notes. COMP 151: Terms Test

John Cowell. Essential Java Fast. How to write object oriented software for the Internet. with 64 figures. Jp Springer

Chapter 3: Inheritance and Polymorphism

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

Day 5: Inscribing and Circumscribing Getting Closer to π: Inscribing and Circumscribing Polygons - Archimedes Method. Goals:

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

CSC 551: Web Programming. Fall 2001

Computer Science II - Test 1

Interfaces. Interfaces. Implementing Interfaces. Interfaces : : : :

(2½ hours) Total Marks: 75

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username

Chapter 1 GUI Applications

Introduction to Programming Using Java (98-388)

CHAPTER-3 STUDY OF GRAMMAR TREE USING L-SYSTEM. The central concept of L-systems is that of rewriting. In general, rewriting is a

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

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

EE219 Object Oriented Programming I (2006/2007) SEMESTER 1 SOLUTIONS

PESIT Bangalore South Campus

15CS45 : OBJECT ORIENTED CONCEPTS

COMPUTER SCIENCE TRIPOS

MaSH Environment graphics

Animation Part 2: MoveableShape interface & Multithreading

CSC Java Programming, Fall Java Data Types and Control Constructs

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

User interfaces and Swing

CS108, Stanford Handout #22. Thread 3 GUI

MODULE 8p - Applets - Trials B

EE219 Object Oriented Programming I (2007/2008) REPEAT SOLUTIONS


Data Representation and Applets

Dr. Hikmat A. M. AbdelJaber

Graphics Applets. By Mr. Dave Clausen

CHAPTER 2. Java Overview

UML and OOD. Some terms related to OOD. Dependency. Coupling. Interfaces. Multiple Interfaces

CHAPTER 3, FORM E TRIGONOMETRY Choose the best answer. NAME DATE. Do not use a calculator for problems 1-11.

l = Length of arc AB

CSD Univ. of Crete Fall Java Applets

Object Oriented Programming 2012/13. Final Exam June 27th, 2013

DUKE UNIVERSITY Department of Computer Science. Midterm Solutions

Applets and the Graphics class

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

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

To use CodeWarrior to compile and run prewritten Java programs

Exercise NMCGJ: Animated Graphics

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

GCSE LINKED PAIR PILOT 4364/02 METHODS IN MATHEMATICS UNIT 2: Methods (Calculator) HIGHER TIER

UCLA PIC 20A Java Programming

CSIS 10A Assignment 14 SOLUTIONS

Get Unique study materials from

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

Building a Java First-Person Shooter

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

CSE 212 : JAVA PROGRAMMING LAB. IV Sem BE (CS&E) (2013) DEPT OF COMPUTER SCIENCE & ENGG. M. I. T., MANIPAL. Prepared By : Approved by :

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Assignment 2. Application Development

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Practice Midterm 1. Problem Points Score TOTAL 50

Data Representation and Applets

More about GUIs GEEN163

Data Representation and Applets

Control Statements: Part Pearson Education, Inc. All rights reserved.

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

Design and Communication Graphics

Appendix D Main Applet for Web-Based 3D Experiment

Transcription:

Java - Dates and Threads Thread example Java uses threads to allow 1 event to break into another. If an action is to occur a number of times with a loop or to occur until some condition is satisfied then that action will take over the computer. Any other planned events will have to wait until the loop is finished. A thread can break into such an action with another section of code. In this example a circle is redrawn to give the effect of a ball bouncing in a box. Without the use of a thread there would be no way to interrupt the bouncing. Any controls placed on the applet would be disabled. The following code should run as typed. import java.awt.*; import java.applet.applet; import java.awt.event.*; public class Bounce extends Applet implements ActionListener //this class is used to create new Ball objects and display them private Button start; private Ball ball public void init() start = new Button("Start"); add(start); start.addactionlistener(this); public void actionperformed(actionevent event) Graphics g = getgraphics(); if(event.getsource()==start) ball = new Ball(g); ball.start(); //not run see the Ball Class class Ball extends Thread //extend Thread to make use of Thread methods private Graphics g; private boolean moving; private int x=7, xchange =7; private int y=0, ychange =2; private int diameter =10; private int rectleftx =0, rectrightx=100; 1

private int recttopy=0, rectbottomy=100; public Ball(Graphics graphics) //constructor g=graphics; moving=true; public void run() //the Thread equivalent of init() //draw rectangle so ball edge just touches g.drawrect(rectleftx,recttopy,rectrightxrectleftx+diameter,rectbottomy-recttopy+diameter); //all on 1 line while(moving) //paint over last ball g.setcolor(color.lightgray); g.filloval(x,y,diameter,diameter); //code for hitting edges if(x+xchange<=rectleftx) xchange = -xchange; if(x+xchange>=rectrightx) xchange = -xchange; if(y+ychange<=recttopy) ychange = -ychange; if(y+ychange>=rectbottomy) ychange = -ychange; //move ball x=x+xchange; y=y+ychange; //repaint ball g.setcolor(color.blue); g.filloval(x,y,diameter,diameter); try //allow other actions this is the gap for other code to cut in Thread.sleep(50); catch(interruptedexception e) //have to catch this exception g.drawstring("sleep exception",100,100); The above code will keep creating new blue balls as long as the Button start is clicked. The key method is run(), this goes on and on while moving is true but allows other methods to cut in during the Thread.Sleep() call. As run() is a method that goes on and on it is not possible to pass parameters from outside while the Thread runs. The Thread could be killed and another object created from that Class with new parameters. 2

In the Ball example the oval is filled in blue. That colour cannot be changed while the Thread runs. By adjusting the constructor to accept a Color value a new Thread object could be created with a different fill. Modifications Add a Button to stop the Balls moving. It is only necessary to stop 1 Ball if there are multiple Balls in the rectangle. Create a 2 nd Ball that bounces within another rectangle when start is pressed. This could be achieved by extending the original Ball Class or overloading the constructor of the Ball Class. Time in Java Threads will be used to display the system time as a set of numbers and moving hands on a clock. Before that some explanation of how to get the system date is required. The easy way is to import java.util.date; and make use of a Date object. There are a number of handy Date methods that will supply the hours and so forth as useful integers. import java.util.date; //for Date private Date now; now = new Date(); int min=now.getminutes(); Unfortunately these methods are depreciated because the Java authors decided that they could not cover all the World s possible ways of displaying dates. There are 2 ways around the problem. The Calendar Class appears to have attributes similar to Date but on closer observation most of the contents of this Class are constants used to set dates from user input rather than to get the system date. 3

private Calendar rightnow; rightnow = Calendar.getInstance(); //create Calendar object String time = String.valueOf(rightNow.getTime()) The above code will get the current system time and put it into a String for displaying on an Applet or in a TextField. Note that the following enticing code lines do not work: String hours = String.valueOf(rightNow.HOUR_OF_DAY); String mins = String.valueOf(rightNow.MINUTE); String secs = String.valueOf(rightNow.SECOND); Another approach uses the SimpleDateFormat Class to get the time from a Date object. import java.text.*; //for SimpleDateFormat SimpleDateFormat formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy z"); //all on one line // the full format with day, month, date, hour, minute, second, and time zone. //the number of characters affects how items are displayed such as Mon or Monday String time = formatter.format(now); //display the Date object now in the above format Appropriate formats for minutes and seconds can be assigned to Strings such as: SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault()); //note s is not a variable like secs String secs = formatter.format(now); The above code may cause a problem with BST times as the additional hour may not be factored in. This does not occur with NT at College but may do under Windows 95+. One solution is to set up a method to scan through the String created by SimpleDateFormat.formatter() and add an hour if the + character is found. Set up a pair of Classes. One to host the business end and create instances of other Classes, this will need Buttons to start and stop the time Thread. Another that extends Thread causing the time displayed to be updated every second - Thread.sleep(1000); the time should be displayed as the complete time and as separate Strings for hours, minutes and seconds. When the time is correctly displayed extend the time Thread to display the time as an analogue clock. The hours, minutes and seconds Strings will come in handy here. Adjust the Buttons so either clock can be started or stopped independently. The analogue clock will need a centre to draw the hands from and a radius for each hand. The angle to move each hand can be found from the time and the position using Sin and Cos, unfortunately Java angles are measured in radians. 4

The circle that forms the clock can be created with drawoval for the outside and filloval for a filled centre. The fill will have to be a little smaller that the rim or it will overdraw it. g.drawoval(topx,topy,radius*2,radius*2);//outside of clock circle g.setcolor(color.lightgray);//inside of clock g.filloval(topx+2,topy+2,(radius*2)-2,(radius*2)-2);//slightly smaller The initial string values for hours, minutes and seconds will need to be converted to double or float values for plotting. When drawing the hands there are 3 points to take care of: There are 60 minutes or seconds in an hour but 360 o in a full circle. dangle = dangle*6; //6*60=360 - for secs and mins Converting from degrees to radians: dangle = dangle * 3.14/180; Angles are drawn from the 3.00 position, 12.00 would be a better start line for a clock: dangle= dangle - (3.14/2); Plotting the lines can be handled by Java.Math; int ypos = (int)(math.sin(dangle)*60); int ypos = (int)(math.sin(dangle)*60); The hour hand will be more complex as there are 12 hours to divide in to 360 o and the effect of the minutes on the hour hand will have to be taken care of. 5