Made with love, by Zachary Langley Applets The Graphics Presentation.

Slides:



Advertisements
Similar presentations
Custom Painting Gestione della Grafica customizzata Vedi anche:
Advertisements

1 Applets Programming Enabling Application Delivery Via the Web.
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Web Design & Development Lecture 19. Java Graphics 2.
Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Polymorphism Method overriding Method overloading Dynamic binding 1.
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Developing User Interfaces (DUI) Chris North cs3724: HCI.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Applets.
JAPPLET.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Creating a Java Application and Applet
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Java Applets.
Applets.
Lecture 09 Applets.
Java Applets.
Java Applets.
Java Applets.
Java applets 1/3/2019.
Applet in Java.
Java Programming COMP-417 Applet
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

Made with love, by Zachary Langley Applets The Graphics Presentation

What is an Applet? An applet is a Java program that a web browser enabled with Java technology can download and run Applets are a subclass of java.awt.Applet, which provides the interface between the browser and the applet

How do Applets Differ From Applications? Applets do not have a main method ( public static void main(String[] args) ) Instead, an applet has a series of methods that are called by an HTML enabled browser Applets are subclasses of JApplet or Applet (they “extend from” these classes)

Writing Your First Applet Open up NetBeans Create a new project called “HelloWorldApplet” Delete the everything in the class’s body (the SPVM and the constructor) Make your class “extend” from J Applet : public class HelloWorldApplet extends JApplet Fix your imports by typing alt+shift+F

Displaying “Hello World!” Add the method in bold to your class: public class HelloWorldApplet extends JApplet { public void paint(Graphics g) { g.drawString(“Hello World!”, 50, 75); } } Run the applet by typing shift+F6

How it Works The drawString method accepts three arguments: the String to draw, the x location, and the y location In Java, the origin (0, 0) is in the top-left corner origin (50, 75) g.drawString(“Hello World!”, 50, 75);

The paint(Graphics) Method The paint method is automatically called by an applet when the applet needs repainting If you want to force the applet to call paint, call repaint() repaint() will call paint(Graphics) for you, but will pass in the appropriate Graphics object

Drawing a Rectangle Let’s draw a rectangle as a border around our text: public void paint(Graphics g) { g.drawRect(45, 60, 85, 20); g.drawString(“Hello World!”, 50, 75); } Run the applet

How it Works The drawRect method accepts four arguments: the x location, the y location, the width, and the height (45, 60) g.drawRect(45, 60, 85, 20);

Filling the Rectangle and Adding Some Color Now we’re going to fill that rectangle in blue and draw “Hello World!” in green: public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(45, 60, 85, 20); g.setColor(Color.green); g.drawString(“Hello World!”, 50, 75); } Notice the change from “drawRect” to “fillRect”

How it Works Color.blue and Color.green are public static variables in the Color class (and there are many others, check the Color documentation) By default, the Graphic ’s color is black Tip: you can create your own Color object with its constructor: Color(int r, int g, int b)

Adding Animation Let’s make our rectangle bounce off the walls of the applet. The first thing we should do is store the x and width as variables public class HelloWorldApplet extends JApplet { private int x = 45, width = 85; public void paint(Graphics g) { /* snip */ } }

Adding Animation II Now we need to use these variables in our paint method public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(x, 60, width, 20); g.setColor(Color.green); g.drawString(“Hello World!”, x + 5, 75); } Run the applet, it should look the same

Threads All animation needs to be done in a separate thread A thread is a thread of execution in a program

Adding Animation III: Creating a Thread Now let’s create our own thread: private int x = 45, width = 85; private class MyThread extends Thread { public void run() { /* code for the thread to execute */ } } Note that this class is inside HelloWorldApplet(we call this an inner-class)

Adding Behavior to MyThread We want to make the text bounce, but first let’s just make it move: private int speed = 1; public void run() { while (true) { x += speed; repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { } } } }

Starting the Thread The program still shouldn’t act differently; we haven’t started the thread Add the following method before the paint method: public void init() { MyThread myThread = new MyThread(); myThread.start(); }

The init() Method Like the paint method, the init method is called automatically However, the init method is called once and only once: as soon as the applet launches Only initialization code should go in the init method

Animation IV If you run your applet now, you’ll notice that the text moves, but it moves right off the window We can easily fix this by checking if our rectangle is going to move out of view, and if so, reverse the speed: while (true) { if (x + width + speed > getWidth() || x + speed < 0) speed = -speed; x += speed; /* snip */ }

Mouse Listeners To demonstrate MouseListeners, create another project in NetBeans called “MouseListenerApplet” Follow the same steps you did before to make it run as an applet (remove the SPVM, the constructor, extend JApplet, fix imports)

Adding a MouseListener Adding a MouseListener is much like creating a thread public class MouseListenerApplet extends JApplet { private class MyMouseListener implements MouseListener { } public void init() { addMouseListener(new MyMouseListener()); } } At this point you may need to fix your imports

Implementing the Required Methods The code will not compile yet; the MouseListener interface requires you to implement certain methods private class MyMouseListener implements MouseListener { public void mouseClicked(MouseEvent e) { System.out.println(“mouseClicked”); } public void mouseEntered(MouseEvent e) { System.out.println(“mouseEntered”); } public void mouseExited(MouseEvent e) { System.out.println(“mouseExited”); } public void mousePressed(MouseEvent e) { System.out.println(“mousePressed”); } public void mouseReleased(MouseEvent e) { System.out.println(“mouseReleased”); } } After adding these methods, run the applet. Click on the applet and watch what is printed to the console

Methods in the Applet’s Life Cycle There are four methods in an applet’s life cycle: i nit(), s tart(), stop(), and d estroy() init() and d estroy() are called once throughout the applet’s life, while s tart() and s top() are called at least once start() is called immediately after the init method, and also when a user returns to the page containing the applet, after viewing other methods stop() is called whenever the user moves away from the page containing the applet destroy() is called when the browser shuts down normally

Questions?