Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Graphics Programming With Applets Feb 23, 2000. Applets There are three different types of executable java code. –Standalone application, which has main()"— Presentation transcript:

1 Graphics Programming With Applets Feb 23, 2000

2 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.

3 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.

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

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

6 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).

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

8 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().

9 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); }

10 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); }

11 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); }

12 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!!!

13 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); }

14 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().

15


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

Similar presentations


Ads by Google