1 / 40

Java Applets

Java Applets. CS442- Advanced Programming using Java. x. ( x,y ). width. (x1,y1). drawRect. ( x,y ). drawOval. height. (x2,y2). fillOval. width. ( x,y ). ( x,y ). fillRect. height. y. import java.applet.Applet ; import java.awt.Graphics ;. import java.applet .*;

angelo
Télécharger la présentation

Java Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Applets CS442- Advanced Programming using Java Prepared By: TA.Eman AlMoaili

  2. Prepared By: TA.Eman AlMoaili

  3. x (x,y) width (x1,y1) drawRect (x,y) drawOval height (x2,y2) fillOval width (x,y) (x,y) fillRect height y Prepared By: TA.Eman AlMoaili

  4. import java.applet.Applet; import java.awt.Graphics; import java.applet.*; import java.awt.*; public class exampleextendsApplet { public void init() { ---------------- } public void paint(Graphics g) { -------------------- } } OR Prepared By: TA.Eman AlMoaili

  5. importjava.applet.Applet; • importjava.awt.Color; • importjava.awt.Graphics; • publicclassDemoTestextends Applet{ • publicvoid init() • { • this.setSize(200,240); • this.setBackground(Color.LIGHT_GRAY); • } • publicvoid paint(Graphics g) • { • g.setColor(Color.red); • g.drawString("HELLO", 30, 30); • g.setColor(Color.blue); • g.drawLine(100, 30, 150, 30); • g.setColor(Color.orange); • g.drawRect(30, 50, 50, 30); //position(30,50) width = 50 height = 30 • g.setColor(Color.green); • g.drawOval(30, 100, 50, 50); // position(30,100) width = 50 height = 50 • g.setColor(Color.yellow); • g.fillRect(30, 160, 50, 50); • g.setColor(Color.magenta); • g.fillOval(100, 160, 80, 50); • } • } import java.applet.*; import java.awt.*; OR Prepared By: TA.Eman AlMoaili Prepared By: TA.Eman AlMoaili

  6. Applet with awt (Abstract Windowing Toolkit) importjava.awt.Panel; importjava.awt.Button; importjava.awt.Label; importjava.awt.TextField; Label TextField Button Panel They are Classes ! Prepared By: TA.Eman AlMoaili

  7. nameLabel myPanel enteredName newGridLayout(3,2) 3 rows & 2 columns emptyLabel helloResult helloBtn Prepared By: TA.Eman AlMoaili

  8. Steps to create simple applet with awt • Create Panel • Set Layout for the Panel • Add components (Label, TextField, Button) to the Panel • Add the Panel to the applet Prepared By: TA.Eman AlMoaili

  9. importjava.applet.Applet; importjava.awt.Graphics; importjava.awt.Color; importjava.awt.Panel; importjava.awt.Button; importjava.awt.Label; importjava.awt.TextField; importjava.awt.GridLayout; importjava.awt.event.ActionListener; importjava.awt.event.ActionEvent; OR 1 2 • importjava.applet.*; • import java.awt.*; • importjava.awt.event.*; 3 4

  10. How to make the Button responds to the click event? Prepared By: TA.Eman AlMoaili

  11. Status Bar Prepared By: TA.Eman AlMoaili

  12. Prepared By: TA.EmanAlMoaili

  13. NOTE: When you read fromTextField, you read a String . When you write to TextField, you write a String. SO: When read a number from TextField, convert it from String to number. When write a number to TextField, convert it from number to String. From String to Double From String to Integer From Double to String int x = 10; Stirng s = Integer.toString(x); //from Integer to String Prepared By: TA.Eman AlMoaili

  14. From String to Number: String s = “200”; inti = Integer.parseInt(s); Double d = Double.parseDouble(s); To int To Double Between() From From Number to String: inti = 3 ; Double d = 2.5 ; String s = Integer.toString(i); String s = Double.toString(d) ; To String Prepared By: TA.Eman AlMoaili

  15. How to run applet using applet viewer? • Open the project & the file Select the file Prepared By: TA.Eman AlMoaili

  16. Prepared By: TA.Eman AlMoaili

  17. OR Prepared By: TA.Eman AlMoaili

  18. How to Working with APPLET Tag Prepared By: TA.Eman AlMoaili

  19. <APPLET CODE=“applet class file.class” WIDTH=“pixels” HEIGHT=“pixels” [CODEBASE=“directory to look for”] [ALT=“alternate text”] [NAME=“name of current applet instance”] [ALIGN=“applet alignment”] [VSPACE=“pixels”] [HSPACE=“pixels”]> [<PARAM NAME=“par.name” VALUE=“par.value”> ………. ………. <PARAM NAME=“par.name” VALUE=“par.value”>] [HTML statements] </APPLET> Prepared By: TA.Eman AlMoaili

  20. To deal with the APPLET tag in the previous slide, you have to add HTML file Prepared By: TA.Eman AlMoaili

  21. You need to show the Project Explorer ( NOT Package Explorer): Prepared By: TA.Eman AlMoaili

  22. Under the project folder, you can see the src folder only. BUT, How can you show bin folder? Prepared By: TA.Eman AlMoaili

  23. Remove the tick Now, it is shown Prepared By: TA.Eman AlMoaili

  24. HOW to add html file to your project ? You can add .html file to : 1- project folder. OR 2- bin folder. Prepared By: TA.Eman AlMoaili

  25. add html file to project folder Prepared By: TA.Eman AlMoaili

  26. add html file to project folder Prepared By: TA.Eman AlMoaili

  27. add html file to project folder Prepared By: TA.Eman AlMoaili

  28. add html file to project folder Prepared By: TA.Eman AlMoaili

  29. add html file to bin folder • Right click the bin folder, then apply the same steps before Prepared By: TA.Eman AlMoaili

  30. add html file to bin folder • Finally, you will get it here Prepared By: TA.Eman AlMoaili

  31. If you put the HTML file inside the project folder: <APPLET CODE BASE=“bin” CODE=“packageName/ClassName.class” WIDTH=…… > If you put the HTML file inside the bin folder : <APPLET CODE=“packageName/ClassName.class” WIDTH= ....…. > Prepared By: TA.Eman AlMoaili

  32. You should write the APPLET tag inside the <body> tag Prepared By: TA.Eman AlMoaili

  33. NOTE: If you start editing the html file, and you get this window, click Yes. Prepared By: TA.Eman AlMoaili

  34. How to run applet from html ? • Open the browser ( IE ) • Open the html file from the bin folder. NOTE: If you make any changes in the applet java file: • Save the applet java file(ctrl+s) • Clear the web browsing storage.Tools  Internet Options  Delete  • Close IE • Open IE • Open the html NOTE: If you make changes to html: • Save the html file(ctrl+s) • refresh html OR open it again. Prepared By: TA.Eman AlMoaili

  35. If you get this security warning, right click then Allow Blocked Content Prepared By: TA.Eman AlMoaili

  36. If you use PARAM in the APPLET tag, you can run the applet from html file not from applet viewer in Eclipse. Prepared By: TA.Eman AlMoaili

  37. EXAMPLE Prepared By: TA.Eman AlMoaili

  38. Prepared By: TA.Eman AlMoaili

  39. Prepared By: TA.Eman AlMoaili

  40. Prepared By: TA.EmanAlMoaili

More Related