Programmierpraktikum

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

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

G51PRG: Introduction to Programming Second semester Applets and graphics

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

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

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

CSIS 10A Assignment 14 SOLUTIONS

PESIT Bangalore South Campus

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

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.

AP Computer Science Unit 13. Still More Graphics and Animation.

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

Advanced Java Unit 6: Review of Graphics and Events

User interfaces and Swing

Come & Join Us at VUSTUDENTS.net

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

Event Driven Programming

Which of the following syntax used to attach an input stream to console?

AP CS Unit 12: Drawing and Mouse Events

Programmierpraktikum

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

RobotPlanning.java Page 1

Chapter 1 GUI Applications

The AWT Event Model 9

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

CSD Univ. of Crete Fall Java Applets

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

Lecture 5: Java Graphics

Chapter 7 Applets. Answers

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

CSC 551: Web Programming. Fall 2001

Lecture 3: Java Graphics & Events

Lab 10: Inheritance (I)

CS2110. GUIS: Listening to Events

CS 106A, Lecture 14 Events and Instance Variables

Advanced Internet Programming CSY3020

Java - Applets. public class Buttons extends Applet implements ActionListener

CS 106A, Lecture 14 Events and Instance Variables

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

Java - Dates and Threads

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,

2IS45 Programming

Java. GUI building with the AWT

Computer Science 210: Data Structures. Intro to Java Graphics

GUI in Java TalentHome Solutions

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

Advanced Java Programming

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Computer Science II - Test 2

Introduction to Java Applets 12

Graphical User Interfaces 2

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

Introduction to the Java T M Language

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Introduction. Introduction

Chapter 14: Applets and More

NITI NITI I PRIORITET

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

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

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

Windows and Events. created originally by Brian Bailey

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

CSE 331 Software Design & Implementation

Java Applet Basics. Life cycle of an applet:

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

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

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

CSC System Development with Java Introduction to Java Applets Budditha Hettige

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

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

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CHAPTER 2. Java Overview

Unit 7: Event driven programming

9. APPLETS AND APPLICATIONS

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

Using the API: Introductory Graphics Java Programming 1 Lesson 8

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

Java Coordinate System

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

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Chapter 12 Advanced GUIs and Graphics

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

IT101. Graphical User Interface

Twin A Design Pattern for Modeling Multiple Inheritance

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

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

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

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

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

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

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

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

Lecture 19 GUI Events

Java Applets / Flash

Packages: Putting Classes Together

Transcription:

Programmierpraktikum Claudius Gros, SS2012 Institut für theoretische Physik Goethe-University Frankfurt a.m. 1 of 18 17/01/13 11:46

Java Applets 2 of 18 17/01/13 11:46

Java applets embedding Java applications into webpages simulations, tutorials, web interfaces applet example copy Java code to file AppletDrawingLines.java run the Java compiler javac AppletDrawingLines copy html code to file anything.html open the html file with a browser firefox anything.html, sometimes you need a reload 1. 2. 3. import javax.swing.*; // for JApplet,.. import java.awt.*; // for Color import java.util.*; // for sleeping 4. 5. /** Java applet example based on Japplet. 3 of 18 17/01/13 11:46

6. 7. 8. * Has no main() function, init() executed before paint(). public class AppletDrawingLines extends JApplet { 9. 10. 11. 12. 13. 14. 15. 16. 17. int width, height; public void init() { width = getsize().width; height = getsize().height; setbackground(color.black); // size of the html applet box // executed when the applet starts // getsize() is a member function of JApplet // returning the size of the html applet box 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. /** The.pain() function is executed whenever the applet is * asked to redraw itself. public void paint(graphics g) { g.setcolor(color.green); for (int i=0; i<10; i++) { g.drawline(width, height, i * width / 10, 0); // drawline(x-start,y-start,x-end,y-end) try { 28. Thread.sleep(700); // sleep for a short time 4 of 18 17/01/13 11:46

29. 30. 31. 32. 33. 34. catch (InterruptedException e) { System.out.println(e.toString()); // end of loop over drawing lines // end of AppletDrawingLines.paint() // end of AppletDrawingLines html code for embedding applet html tags are like brackets: <center>...</center> <html> <head> <meta charset="utf8" /> <title>java applets</title> </head> <!-- this is a comment: a html page has a header and a body --> <body> <center> <h1>embedded Java applet</hr> <br><br> <applet width=400 height=400 code="appletdrawinglines.class"> </applet> </center> </body> </html> 5 of 18 17/01/13 11:46

Mouse events here Applet instead of JApplet and java.awt instead of javax.swing the interfaces MouseListener and MouseMotionListener are implemented, all their member functions need to be explicitly declared, even when void 1. 2. 3. import java.applet.*; // this example with Applet instead of JApplet import java.awt.*; import java.awt.event.*; 4. 5. 6. public class AppletMouseFollow extends Applet implements MouseListener, MouseMotionListener { 7. 8. 9. 10. int width, height; // size of html applet box int mx, my; // the mouse coordinates boolean isbuttonpressed = false; 11. 12. public void init() { 6 of 18 17/01/13 11:46

13. 14. 15. width = getsize().width; height = getsize().height; setbackground(color.black); 16. 17. 18. mx = width/2; // starting position of my = height/2; // square 19. 20. 21. 22. addmouselistener( this ); // adding event listeners addmousemotionlistener( this ); 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. /** Called when the pointer enters the applet's box. * All functions of the implemented interfaces * MouseListener and MouseMotionListener need to be implemented. public void mouseentered( MouseEvent e ) { setbackground(color.blue); public void mouseexited( MouseEvent e ) { setbackground(color.orange); /** Called after a press and release of a mouse button * with no motion in between. 7 of 18 17/01/13 11:46

36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. * If the user presses, drags, and then releases, * there will be no click event generated. public void mouseclicked( MouseEvent e ) { setbackground(color.cyan); /** Called when mouse down but not released. public void mousepressed( MouseEvent e ) { isbuttonpressed = true; setbackground(color.gray); repaint(); // re // "Consume" the event so it won't be processed in the // default manner by the source which generated it. e.consume(); /** Called when released after drag. public void mousereleased( MouseEvent e ) { isbuttonpressed = false; setbackground(color.black); repaint(); e.consume(); 8 of 18 17/01/13 11:46

59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. /** Called during motion when no buttons are down. public void mousemoved( MouseEvent e ) { mx = e.getx(); // the mouse events provides the current my = e.gety(); // mouse position showstatus( "Mouse at (" + mx + "," + my + ")" ); // browser status line repaint(); // calls the paint() method e.consume(); /** Called during motion with buttons down. public void mousedragged( MouseEvent e ) { mx = e.getx(); my = e.gety(); showstatus( "Mouse at (" + mx + "," + my + ")" ); repaint(); e.consume(); 78. 79. 80. 81. public void paint( Graphics g ) { if (isbuttonpressed) g.setcolor(color.black); // black rectangle if moused down 9 of 18 17/01/13 11:46

82. 83. else g.setcolor(color.gray); // gray otherwise 84. // 85. g.fillrect(mx-20, my-20, 40, 40); // filled and centered rectangle 86. 87. // end of AppletMouseFollow 10 of 18 17/01/13 11:46

Bouncing with and without threads try to to change speed while bouncing double buffering for professional applications, avoiding flickering: paint first everything offscreen and then upload full graphics (not done here) 1. 2. 3. import java.awt.*; import java.awt.event.*; import java.applet.applet; 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. /** A class can extend only a single parent class * but it can implement any number of interfaces. public class AppletBounce extends Applet implements ActionListener, AdjustmentListener, Runnable { private int width, height; // width, height of applet box private int tick = 100; // length of pausini, speed control private int initialy = 70; // bouncing between these 11 of 18 17/01/13 11:46

14. private int lowesty; // two values 15. 16. private Button gothreaded = new Button("go threaded"); 17. 18. 19. private Button gonotthreaded = new Button("go not threaded"); private Scrollbar speed = new Scrollbar(Scrollbar.HORIZONTAL,tick,30,20,400); 20. 21. 22. 23. 24. /** Called when applet is loaded. public void init() { setbackground(new Color(230,230,230)); 25. width = getsize().width; 26. 27. 28. 29. height = getsize().height; lowesty = (int)(height*0.8); // --- select layout manager // use relative units 30. setlayout(new BorderLayout()); // a layout manger 31. 32. Panel buttons = new Panel(); // a seperate panel for the buttons 33. 34. 35. 36. // --- button for starting animation with current thread gonotthreaded.setbackground(color.lightgray); gonotthreaded.addactionlistener(this); buttons.add(gonotthreaded); 12 of 18 17/01/13 11:46

37. 38. 39. 40. 41. 42. // --- button for starting animation in an additional thread gothreaded.setbackground(color.lightgray); gothreaded.addactionlistener(this); buttons.add(gothreaded); add("north", buttons); // add both bottoms to the top 43. 44. 45. 46. 47. 48. 49. 50. 51. // --- the Scrollbar 'speed' is inside the Panel 'speedcontrol' Panel speedcontrol = new Panel(); speedcontrol.setbackground(color.lightgray); speed.setpreferredsize(new Dimension((int)(width*0.6),20)); speedcontrol.add(new Label("faster",Label.RIGHT)); speedcontrol.add(speed); speed.addadjustmentlistener(this); speedcontrol.add(new Label("slower")); 52. 53. add("south", speedcontrol); // add the speed-control bar to the bottom 54. // end of AppletBounce.init() 55. 56. 57. 58. 59. /** Catch scrollbar event. public void adjustmentvaluechanged(adjustmentevent e) { tick = speed.getvalue(); 13 of 18 17/01/13 11:46

60. 61. 62. 63. 64. 65. 66. /** Catch button events. public void actionperformed(actionevent e) { if (e.getsource() == gothreaded) // the new Thread calls run() new Thread(this).start(); // running the object 'this' 67. if (e.getsource() == gonotthreaded) // calling directly 68. doanimation(); // doanimation() 69. 70. 71. 72. 73. 74. 75. 76. /** AppletBounce is implementing runnable and run() is * called when a new thread is started. public void run() { doanimation(); 77. 78. 79. 80. 81. 82. /** Let a ball at random position x bounce downwards * and upwards, with increasing speed (step). public void doanimation() { // --- setup of bounds 14 of 18 17/01/13 11:46

83. 84. 85. 86. 87. int minx = (int)(width*0.1); // allways use int maxx = (int)(width*0.9); // relative units int x = (int)(minx+math.random()*(maxx-minx)); // fixed random x for bouncing int y = initialy; // variable y of bouncing int step = 1; // (in)decrement for y 88. 89. 90. 91. 92. 93. 94. // --- setup of graphics Graphics g = getgraphics(); Color background = getbackground(); // needed for repainting g.setcolor(color.black); g.drawline(minx, lowesty+20, // bottom line maxx, lowesty+20); 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. // --- bouncing downwards while (y <= lowesty) { g.filloval(x, y, 20, 20); // paint ball pause(tick); // pause animation method g.setcolor(background); g.filloval(x, y, 20, 20); // turn ball to background g.setcolor(color.black); y = y+step; step++; 15 of 18 17/01/13 11:46

106. 107. 108. 109. 110. 111. 112. 113. 114. // --- hitting bottom g.setcolor(new Color(204,51,51)); g.filloval(x, lowesty, 20, 15); // extra image for lowest point g.drawstring("boing!",x-10,lowesty+35); // its 'boing' pause(tick*3); // pause for reading g.setcolor(background); g.drawstring("boing!",x-10,lowesty+35); g.filloval(x, lowesty, 20, 15); // erase the extra squeeze ball 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. // --- bouncing upwards g.setcolor(color.black); while (step>0) { step--; y = y-step; g.filloval(x, y, 20, 20); pause(tick); g.setcolor(background); g.filloval(x, y, 20, 20); g.setcolor(color.black); // end of AppletBounce.doAnimation() 128. 16 of 18 17/01/13 11:46

129. /** Just a wrapper for the exception capture. 130. 131. 132. 133. 134. private void pause(int pauselength) { try { Thread.sleep(pauseLength); catch (InterruptedException e) { // end of AppletBounce 17 of 18 17/01/13 11:46

What to do with applets? physics / science simulations - visualization 1D quantum wave packet scattering one / two player collision game Conway's Game of Life... interaction web-based GUI (graphical user interface) web-based user-user interactions (maps, chat, mail,...) browser games 18 of 18 17/01/13 11:46