Graphics programming in Java

10,437 views

Published on

The seminar for students given at Government Polytechnic, Nashik.
by Tushar B Kute.
Topic: Graphics Programming in Java.

2 Comments
4 Likes
Statistics
Notes
No Downloads
Views
Total views
10,437
On SlideShare
0
From Embeds
0
Number of Embeds
214
Actions
Shares
0
Downloads
453
Comments
2
Likes
4
Embeds 0
No embeds

No notes for slide

Graphics programming in Java

  1. 1. TUSHAR B KUTE, LECTURER, SANDIP INSTITUTE OF TECHNOLOGY AND RESEARCH CENTRE, NASHIK Graphics Programming in Java http://www.tusharkute.com
  2. 2. Applets <ul><li>Applet is a web based application to support the dynamic web pages on internet… </li></ul>
  3. 3.
  4. 4. Applet vs. Applications <ul><li>Applets can't read from or write to the reader's file system. </li></ul><ul><li>Applets can't communicate with any network server other than the one that had originally stored the applet. </li></ul><ul><li>Applets can't run any programs on the reader's system. </li></ul><ul><li>Applets can't load programs native to the local platform, including shared libraries such as DLLs (Dynamic Link Library). </li></ul><ul><li>All these rules are true for Java applets running Netscape Navigator or Microsoft Internet Explorer. </li></ul>
  5. 5. Applet Class Object Component Container Panel Applet JApplet Window Frame JFrame java.lang java.awt java.applet javax.swing
  6. 6. The Applet Skeleton <ul><li>import java.awt.*; </li></ul><ul><li>import java.applet.*; </li></ul><ul><li>public class MyApplet extends Applet </li></ul><ul><li>{ </li></ul><ul><li>public void init( ) </li></ul><ul><li>{ </li></ul><ul><li>// initializes applet </li></ul><ul><li>} </li></ul><ul><li>public void start( ) </li></ul><ul><li>{ </li></ul><ul><li>// start or resume execution </li></ul><ul><li>} </li></ul><ul><li>public void stop( ) </li></ul><ul><li>{ </li></ul><ul><li>// suspends execution </li></ul><ul><li>} </li></ul><ul><li>public void destroy( ) </li></ul><ul><li>{ </li></ul><ul><li>// perform shutdown activities </li></ul><ul><li>} </li></ul><ul><li>public void paint(Graphics g) </li></ul><ul><li>{ </li></ul><ul><li>// redisplay contents of window </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  7. 7. Applet Life Cycle Born Running Idle Dead Begin Initialization Display Stopped Destroyed End start() paint() stop() start() destroy()
  8. 8. Compiling the Applet <ul><li>/* </li></ul><ul><li><applet code=&quot;MyApplet&quot; width=200 height=100> </li></ul><ul><li></applet> </li></ul><ul><li>*/ </li></ul><ul><li>  </li></ul><ul><li>javac MyApplet.java </li></ul><ul><li>  </li></ul><ul><li>  MyApplet.class </li></ul><ul><li>  </li></ul><ul><li>  appletviewer MyApplet.java </li></ul>
  9. 9. The drawString() method <ul><li>//void drawString(String msg, int x, int y) </li></ul><ul><li>  </li></ul><ul><li>/* </li></ul><ul><li><applet code = &quot;HelloApplet&quot; width = 200 </li></ul><ul><li>height = 100> </applet> </li></ul><ul><li>*/ </li></ul><ul><li>public class HelloApplet extends Applet </li></ul><ul><li>{ </li></ul><ul><li>public void paint(Graphics g) </li></ul><ul><li>{ </li></ul><ul><li> g.drawString(&quot;Hello&quot;,100,50); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Program
  10. 10. Output window
  11. 11. Colors <ul><li>void setBackground(Color newCol) </li></ul><ul><li>void setForeground(Color newCol) </li></ul><ul><li>Example: </li></ul><ul><li>setBackground(Color.lightGray); </li></ul><ul><li>setForeground(Color.red); </li></ul><ul><li>  </li></ul><ul><li>Color getBackground( ) </li></ul><ul><li>Color getForeground( ) </li></ul><ul><li>  </li></ul><ul><li>Colors Available in Color class </li></ul><ul><li>Color.black Color.magenta </li></ul><ul><li>Color.blue Color.orange </li></ul><ul><li>Color.cyan Color.pink </li></ul><ul><li>Color.darkGray Color.red </li></ul><ul><li>Color.gray Color.white </li></ul><ul><li>Color.green Color.yellow </li></ul><ul><li>Color.lightGray </li></ul>Program
  12. 12. Building Applet to the HTML file <ul><li><APPLET </li></ul><ul><li>[CODEBASE = codebaseURL] </li></ul><ul><li>CODE = appletFile </li></ul><ul><li>[ALT = alternateText] </li></ul><ul><li>[NAME = appletInstanceName] </li></ul><ul><li>WIDTH = pixels HEIGHT = pixels </li></ul><ul><li>[ALIGN = alignment] </li></ul><ul><li>[VSPACE = pixels] [HSPACE = pixels] > </li></ul><ul><li>[< PARAM NAME = AttributeName VALUE = AttributeValue>] </li></ul><ul><li>[< PARAM NAME = AttributeName2 VALUE = AttributeValue>] </li></ul><ul><li>. . . </li></ul><ul><li>. . . </li></ul><ul><li></APPLET> </li></ul>
  13. 13. ALIGN tags <ul><li>ALIGN = TEXTTOP </li></ul><ul><li>ALIGN = TOP </li></ul><ul><li>ALIGN = ABSMIDDLE </li></ul><ul><li>ALIGN = MIDDLE </li></ul><ul><li>ALIGN = BASELINE </li></ul><ul><li>ALIGN = BASELINE </li></ul><ul><li>ALIGN = BOTTOM </li></ul><ul><li>ALIGN = ABSBOTTOM </li></ul>
  14. 14. Building Applet on HTML Page <ul><li><HTML> </li></ul><ul><li><HEAD> </li></ul><ul><li><TITLE>THIS PAGE HAS AN APPLET ON IT</TITLE> </li></ul><ul><li></HEAD> </li></ul><ul><li><BODY> </li></ul><ul><li><P>MY JAVA APPLET SAYS: </li></ul><ul><li><BR> </li></ul><ul><li><APPLET CODE = &quot;FullApplet&quot; WIDTH = 400 </li></ul><ul><li>HEIGHT = 100 </li></ul><ul><li>HSPACE = 20 VSPACE = 20 ALIGN = CENTER> </li></ul><ul><li></APPLET> </li></ul><ul><li></BODY> </li></ul><ul><li></HTML> </li></ul>
  15. 15. Example: <ul><li>The Applet class </li></ul><ul><li>Applet HTML code </li></ul>
  16. 16. Passing parameters to the applet <ul><li>param tag </li></ul><ul><li>getParameter() method </li></ul><ul><ul><ul><li>String getParameter(parameter name) </li></ul></ul></ul><ul><li>Example: </li></ul><ul><ul><li>The program code </li></ul></ul>
  17. 17. Using the Status Window <ul><li>import java.awt.Graphics; </li></ul><ul><li>import java.applet.Applet; </li></ul><ul><li>import java.awt.Color; </li></ul><ul><li>/* </li></ul><ul><li><applet code=&quot;StatusWindow&quot; width=300 height=50> </li></ul><ul><li></applet> </li></ul><ul><li>*/ </li></ul><ul><li>public class StatusWindow extends Applet </li></ul><ul><li>{ </li></ul><ul><li>public void init( ) </li></ul><ul><li>{ </li></ul><ul><li>setBackground(Color.yellow); </li></ul><ul><li>} </li></ul><ul><li>public void paint(Graphics g) </li></ul><ul><li>{ </li></ul><ul><li>g.drawString(&quot;This is applet window.&quot;, 10, 20); </li></ul><ul><li>showStatus(&quot;This is status window.&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Program
  18. 18. Drawing lines <ul><li>void drawLine(int stx, int sty, int edx, int edy) </li></ul><ul><li>Example: </li></ul><ul><li>g.drawLine(120, 150, 400, 140); </li></ul><ul><li>g.drawLine(40, 25, 250, 180); </li></ul><ul><li>g.drawLine(75, 90, 400, 200); </li></ul>Program
  19. 19. Drawing Rectangles <ul><li>void drawRect(int top, int left, int width, int height) </li></ul><ul><li>void fillRect(int top, int left, int width, int height) </li></ul><ul><li>Example: </li></ul><ul><li>g.drawRect(20,20,160,160); </li></ul><ul><li>g.fillRect(120,20,60,60); </li></ul>Program
  20. 20. Drawing Rounded Rectangles <ul><li>void drawRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) </li></ul><ul><li>void fillRoundRect(int top, int left, int width, int height, int xDiam, int yDiam) </li></ul><ul><li>Examples: </li></ul><ul><li>g.drawRoundRect(20,30,160,160,30,30); </li></ul><ul><li>g.fillRoundRect(40,120,100,60,25,50); </li></ul>Program
  21. 21. Drawing Ellipse and Circle <ul><li>void drawOval(int top, int left, int width, int height) </li></ul><ul><li>void fillOval(int top, int left, int width, int height) </li></ul><ul><li>Examples: </li></ul><ul><li>g.drawOval(20, 20, 60, 60); </li></ul><ul><li>g.fillOval(100, 30, 75, 150); </li></ul>Program
  22. 22. Drawing Arcs <ul><li>void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle) </li></ul><ul><li>void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle) </li></ul><ul><li>Examples: </li></ul><ul><li>g.drawArc(20, 20, 60, 60, 90, 180); </li></ul><ul><li>g.fillArc(120, 20, 60, 60, 90, 180); </li></ul>Program
  23. 23. Drawing Polygons <ul><li>drawPolygon(x array, y array, coordinates); </li></ul><ul><li>fillPolygon(x array, y array, coordinates); </li></ul><ul><li>Examples: </li></ul>Program
  24. 24. Applet Using Control Loops <ul><li>/* </li></ul><ul><li><applet code = &quot;CtrlLoop&quot; width = 200 height = 200> </li></ul><ul><li></applet> </li></ul><ul><li>*/ </li></ul><ul><li>public class CtrlLoop extends Applet </li></ul><ul><li>{ </li></ul><ul><li>public void paint(Graphics g) </li></ul><ul><li>{ </li></ul><ul><li>int i = 70, j = 70; </li></ul><ul><li>while(i > 10) </li></ul><ul><li>{ </li></ul><ul><li>g.drawOval(i, i, j, j); </li></ul><ul><li>i –= 10 ; </li></ul><ul><li>j += 20; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Program
  25. 25. Drawing Line Graphs <ul><li>/* </li></ul><ul><li><applet code = &quot;LineGraph&quot; width = 250 height = 250> </li></ul><ul><li></applet> </li></ul><ul><li>*/ </li></ul><ul><li>public class LineGraph extends java.applet.Applet </li></ul><ul><li>{ </li></ul><ul><li>public void paint(java.awt.Graphics g) </li></ul><ul><li>{ </li></ul><ul><li>int i, j; </li></ul><ul><li>int x[] = {20, 60, 100, 140, 180, 220, 220}; </li></ul><ul><li>int y[] = {220, 96, 46, 70, 82, 124, 220}; </li></ul><ul><li>  </li></ul><ul><li>g.setColor(java.awt.Color.blue); </li></ul><ul><li>g.drawLine(20, 20, 20, 240); //y axis </li></ul><ul><li>g.drawLine(0, 220, 240, 220); //x axis </li></ul><ul><li>  </li></ul><ul><li>g.setColor(java.awt.Color.red); </li></ul><ul><li>g.drawPolygon(x, y, x.length); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Program
  26. 26. Drawing Bar Charts <ul><li>/* </li></ul><ul><li><applet code = &quot;BarChart&quot; width = 350 height = 250> </li></ul><ul><li></applet> </li></ul><ul><li>*/ </li></ul><ul><li>public class BarChart extends java.applet.Applet </li></ul><ul><li>{ </li></ul><ul><li>String sub[ ] = {&quot;SCI&quot;,&quot;HIN&quot;,&quot;MAR&quot;,&quot;SOC&quot;,&quot;MAT&quot;}; </li></ul><ul><li>int marks[ ] = {62, 87, 75, 69, 48}; </li></ul><ul><li>public void paint(java.awt.Graphics g) </li></ul><ul><li>{ </li></ul><ul><li>for(int i=0;i<5;i++) </li></ul><ul><li>{ </li></ul><ul><li>g.setColor(java.awt.Color.red); </li></ul><ul><li>g.drawString(sub[i], 20, i*50+30); </li></ul><ul><li>  </li></ul><ul><li>g.setColor(java.awt.Color.blue); </li></ul><ul><li>g.fillRect(50, i*50, marks[i]*3,40); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Program
  27. 27. Reference Books <ul><li>Java: The Complete Reference </li></ul><ul><ul><li>by Herbert Schildt </li></ul></ul><ul><li>Programming in Java </li></ul><ul><ul><li>by E Balagurusamy </li></ul></ul><ul><li>Core Java Programming: A Practical Approach </li></ul><ul><ul><li>by Tushar B Kute </li></ul></ul><ul><li>The Java Handbook </li></ul><ul><ul><li>by Patrick Naughton </li></ul></ul>
  28. 28. Thank You

×