Your SlideShare is downloading. ×
Graphics programming in Java
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Graphics programming in Java

8,611
views

Published on

The seminar for students given at Government Polytechnic, Nashik. …

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


2 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,611
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
355
Comments
2
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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