Graphics Programming With Applets Feb 23, 2000. Applets There are three different types of executable java code. –Standalone application, which has main()

Slides:



Advertisements
Similar presentations
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.
Advertisements

Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Java Applets. 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 a browser.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
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.
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
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.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Applets. 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 a browser You.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
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.
A Simple Applet.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
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.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Applets.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Java Applets. 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 a browser.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
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,
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
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.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
Java Swing One of the most important features of Java is its ability to draw graphics.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Java Applets. Introduction to Java and Java Applets Java applications Run in stand-alone mode No additional software required (such as a Web browser)
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
Java Applets.
30 Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Java applets 1/3/2019.
Applet in Java.
Java Programming COMP-417 Applet
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
G6DICP - Lecture 27 Java Applets.
Presentation transcript:

Graphics Programming With Applets Feb 23, 2000

Applets There are three different types of executable java code. –Standalone application, which has main() method. –Servlet, which runs in the context of Web server. (Not covered) –Applets, which runs in the context of Web browser. Applets are embedded in a web page. In other words, it should be specified in a HTML page. With applets, you can give your web page dynamic behaviors.

How Applets Run? To run an applet, web browser should have Java Runtime, which is of course java virtual machine. (JVM) To ease the pain of upgrading browser for new version of java, JVM runs as a plug-in nowadays. Generally applets are small in size. However you can make really sophisticated applets if you want. Lots of restrictions are posed on applets due to the security issue. (This will be covered thoroughly later) Once again, applets do not have a main() method. Instead, it has strictly defined life cycle.

Life Cycle of an Applet public void init() {…} Called by JVM (of browser) when the applet is first loaded public void start() {…} Called each time the browser visits the page containing this applet. public void stop() {…} Called when the browser leaves the page containing this applet. (or when the browser is iconified) public void destroy() {…} Final clean up method which will be called when the applet is unloaded.

applet is a sub class of Panel Panel is a class in old java.awt package. Panel is a sub class of Component class. applet is a sub class of Panel. (GUI class) Therefore applet inherits all from Component and Panel. In Java 2, a sub class of applet is introduced, which is called JApplet Use JApplet when you plan to use Swing packages. However JVM or plug-ins for JApplet is not available for all browsers. (WILL BE in the near future though) You can use appletviewer for testing and viewing your applets. (both applet an JApplet)

Graphics Class A Graphics object is what you actually draw something on. It is also called graphics context in some windowing system. It bundles together all the necessary information about drawing environment. (area, font, color, clipping region,…) Graphics object always works with a component. Most of time, we don’t need to create an instance of Graphics class. (Actually you can’t create it, it’s abstract class).

Selected Methods from Graphics setFont() drawArc() drawString() drawImage() drawLine() drawRect() drawPolygon() drawRoundRect() drawOval() fillArc() fillRect() fillPolygon() fillRoundRect() fillOval()

Three Methods You Need To Know With Graphics void repaint() You may call this whenever you think the window needs to be refreshed. This will eventually calls update(); void update(Graphics g) Generally this method is called by the window system when the component needs to be refreshed. (redisplayed) This method will call paint(); void paint(Graphics g) --(In Swing, paintComponent() ) Most of time, you override this method to draw default graphics of your component. Any newly updated thing is better drawn in update().

import java.awt.*; import java.applet.*; public class Graf extends Applet { public void init() { setBackground(Color.lightGray); } public void paint(Graphics g) { g.setColor(Color.cyan); g.drawString("3d raised",25,25); g.draw3DRect(25,30,50,20,true); g.drawString("3d not raised",95,25); g.draw3DRect(95,30,50,20,false); }

import java.awt.*; import java.applet.*; public class Graf extends Applet { public void init() { setBackground(Color.lightGray); } public void paint(Graphics g) { g.drawRect(25,30,60,40); g.drawRect(125,30,100,100); g.setColor(Color.blue); g.drawOval(25,30,60,40); g.drawOval(125,30,100,100); }

import java.applet.*; import java.awt.event.*; import java.awt.*; public class AppletDemo extends Applet { String message = "Simple Applet Demo"; public void start() { addMouseListener( new MouseAdapter() { public void mouseEntered(MouseEvent evt){ message = "GREETINGS!!!” setBackground(Color.yellow); repaint(); } public void mouseExited(MouseEvent evt){ message = "SEE YEAH!!! setBackground(Color.blue); repaint(); } }); } public void paint(Graphics g) { g.setColor(Color.red); g.drawString(message,100,100); }

JApplet And Swing Subclass of Applet, designed for Swing component. Basically it is an applet with the same life cycle. One main difference is: JApplet has a contentPane. We know what to do with contentPane. Put in JComponent!!!

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class POP extends JApplet { public void start() { JButton jb = new JButton("click me"); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { JFrame frame = new JFrame("Suprise!!!"); frame.setSize(300,200); frame.setVisible(true); } }); getContentPane().add(jb); }

Graphics Programming With JApplet Even though JApplet extends from applet, DO NOT use paint() method. If you do, there is no reason to use JApplet. Use paintComponent() instead. However JApplet is not a JComponent. How can we use paintComponent()? Add JPanel into contentPane of your JApplet. Since JPanel is a JComponent, it has paintComponent() method. However, to do draw our own graphics objects, we need our own paintComponent() method. The solution is to extend JPanel and override paintComponent().