Graphics programming in Java

28
TUSHAR B KUTE, LECTURER, SANDIP INSTITUTE OF TECHNOLOGY AND RESEARCH CENTRE, NASHIK Graphics Programming in Java http://www.tusharkute.com 1

description

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

Transcript of Graphics programming in Java

Page 1: Graphics programming in Java

TUSHAR B KUTE,LECTURER,

SANDIP INSTITUTE OF TECHNOLOGY AND RESEARCH CENTRE, NASHIK

Graphics Programming in Java

http://www.tusharkute.com

1

Page 2: Graphics programming in Java

Applets

Applet is a web based application to support the dynamic web pages on internet…

2

Page 3: Graphics programming in Java

3

Page 4: Graphics programming in Java

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.

4

Page 5: Graphics programming in Java

Applet Class

Object

Component

Container

Panel

Applet

JApplet

Window

Frame

JFrame

java.lang

java.awt

java.applet

javax.swing

5

Page 6: Graphics programming in Java

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 } }

6

Page 7: Graphics programming in Java

Applet Life Cycle

Born

Running Idle

Dead

Begin

Initialization

Display

Stopped

Destroyed

End

start()

paint()

stop()

start()

destroy()

7

Page 8: Graphics programming in Java

Compiling the Applet

/*<applet code="MyApplet" width=200 height=100></applet>*/ javac MyApplet.java  MyApplet.class  appletviewer MyApplet.java

8

Page 9: Graphics programming in Java

The drawString() method

//void drawString(String msg, int x, int y)  /* <applet code = "HelloApplet" width = 200

height = 100> </applet> */ public class HelloApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello",100,50); } }

Program

9

Page 10: Graphics programming in Java

Output window10

Page 11: Graphics programming in Java

Colors

void setBackground(Color newCol)void setForeground(Color newCol)

Example:setBackground(Color.lightGray);setForeground(Color.red);

 Color getBackground( )Color getForeground( )

 Colors Available in Color classColor.black Color.magentaColor.blue Color.orangeColor.cyan Color.pinkColor.darkGray Color.redColor.gray Color.whiteColor.green Color.yellowColor.lightGray

Program

11

Page 12: Graphics programming in Java

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>

12

Page 13: Graphics programming in Java

ALIGN tags

ALIGN = TEXTTOPALIGN = TOPALIGN = ABSMIDDLEALIGN = MIDDLEALIGN = BASELINEALIGN = BASELINEALIGN = BOTTOM ALIGN = ABSBOTTOM

13

Page 14: Graphics programming in Java

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 = "FullApplet" WIDTH = 400

HEIGHT = 100 HSPACE = 20 VSPACE = 20 ALIGN = CENTER></APPLET></BODY></HTML>

14

Page 15: Graphics programming in Java

Example:

The Applet classApplet HTML code

15

Page 16: Graphics programming in Java

Passing parameters to the applet

param taggetParameter() method

String getParameter(parameter name)Example:

The program code

16

Page 17: Graphics programming in Java

Using the Status Window

import java.awt.Graphics; import java.applet.Applet; import java.awt.Color; /* <applet code="StatusWindow" width=300 height=50> </applet> */ public class StatusWindow extends Applet { public void init( ) { setBackground(Color.yellow); } public void paint(Graphics g) { g.drawString("This is applet window.", 10, 20); showStatus("This is status window."); } }

Program

17

Page 18: Graphics programming in Java

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

18

Page 19: Graphics programming in Java

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

19

Page 20: Graphics programming in Java

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

20

Page 21: Graphics programming in Java

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

21

Page 22: Graphics programming in Java

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

22

Page 23: Graphics programming in Java

Drawing Polygons

drawPolygon(x array, y array, coordinates);fillPolygon(x array, y array, coordinates);

Examples:

Program

23

Page 24: Graphics programming in Java

Applet Using Control Loops

/* <applet code = "CtrlLoop" 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

24

Page 25: Graphics programming in Java

Drawing Line Graphs

/*<applet code = "LineGraph" 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

25

Page 26: Graphics programming in Java

Drawing Bar Charts

/* <applet code = "BarChart" width = 350 height = 250> </applet> */ public class BarChart extends java.applet.Applet { String sub[ ] = {"SCI","HIN","MAR","SOC","MAT"}; 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

26

Page 27: Graphics programming in Java

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

27

Page 28: Graphics programming in Java

Thank You

28