Title: Review of Applets
1Review of Applets Compute Graphics GUI
- Overview
- Introduction to Graphics. Applets a quick
review. - Some Classes for Graphics in Java.
- Drawing Geometric Figures
- Lines, Rectangles, Ovals, Arcs
- Coloring Graphics Shapes.
- Applications with Many Frames.
- Example Displaying Images.
- Coming Next GUI Programming (Part I).
2Writing Applets
- Always extends the JApplet class, which is a
subclass of Applet for Swing components. - Override init(), start(), stop(), and destroy()
if necessary. By default, these methods are
empty. - Add your own methods and data if necessary.
- Applets are always embedded in anHTML page.
3The ltappletgt HTML Tag
- ltapplet
- codeclassfilename.class
- widthapplet_viewing_width_in_pixels
- heightapplet_viewing_height_in_pixels
- archivearchivefile
- codebaseapplet_url
- vspacevertical_margin
- hspacehorizontal_margin
- alignapplet_alignment
- altalternative_text
- gt
- ltparam nameparam_name1 valueparam_value1gt
- lt/appletgt
4The Color Class
- Color c new Color(r, g, b)
- r, g, and b specify a color by its red, green,
and blue components. - Example
- Color c new Color(128, 100, 100)
- You can use the following methods to set the
components background and foreground colors - setBackground(Color c)
- setForeground(Color c)
- Example
- setBackground(Color.yellow)
- setForeground(Color.red)
5The Font Class
- Font myFont Font(name, style, size)
- Example
- Font myFont new Font("SansSerif ", Font.BOLD,
16) - Font myFont new Font("Serif",
Font.BOLDFont.ITALIC, 12)
- public void paint(Graphics g)
-
- Font myFont new Font("Times", Font.BOLD, 16)
- g.setFont(myFont)
- g.drawString("Welcome to Java", 20, 40)
- //set a new font
- g.setFont(new Font("Courier",
Font.BOLDFont.ITALIC, 12)) - g.drawString("Welcome to Java", 20, 70)
-
6Drawing Geometric Figures
Drawing Lines drawLine(x1, y1, x2, y2)
Drawing Rectangles drawRect(x, y, w, h) fillRect(x, y, w, h)
Rounded Rectangles drawRoundRect(x, y, w, h, aw, ah) fillRoundRect(x, y, w, h, aw, ah)
Drawing Arcs drawArc(x, y, w, h, angle1, angle2) fillArc(x, y, w, h, angle1, angle2)
7Java Support for Graphics Common Classes
- As shown in the previous applet example, Java AWT
(Abstract Windowing Toolkit) package provides a
collection of classes your programs can use to
perform graphics operations. Some of the common
classes are
contains methods and constants for obtaining
font information
contains methods for drawing strings, lines,
rectangles and other shapes
contains methods and constants for manipulating
colors
contains methods and constants for manipulating
fonts
contains methods for creating polygons
8Using Applet to draw a rectangle
import java.applet.Appletimport
java.awt.Graphicsimport java.awt.Graphics2Dimp
ort java.awt.Rectanglepublic class
RectangleApplet extends Applet public void
paint(Graphics g) Graphics2D g2
(Graphics2D)g Rectangle rectangle new
Rectangle(10,10,50,50) g2.draw(rectangle)
// End of paint() method // End of
RectangleApplet class
9Experience with Graphics Example 1
Here is an example of simple plain text on a
frame.
import java.awt.public class FrameTest1
public static void main(String args)
MyFrame frame new MyFrame()
frame.setTitle("Graphics Using Frames")
frame.show() // End of main method // End
of class FrameTest1class MyFrame extends Frame
public MyFrame() final int
FRAME_WIDTH 300 final int FRAME_HEIGHT
300 setSize(FRAME_WIDTH, FRAME_HEIGHT)
// End of MyFrame() constructor public void
paint(Graphics g) g.drawString("Hello
world!",100,100) // End of paint() method
// End of MyFrame class
10Experience with Graphics Example 2
Now, let us change the font size and color of the
text.
import java.awt.class MyFrame extends Frame
public MyFrame() final int FRAME_WIDTH
300 final int FRAME_HEIGHT 300
setSize(FRAME_WIDTH, FRAME_HEIGHT) // End of
MyFrame() constructor public void
paint(Graphics g) Graphics2D g2
(Graphics2D)g final int size 48
Color myColor new Color(0.9F, 0.3F, 0.5F)
Font myFont new Font("Times", Font.BOLD,
size) g2.setColor(myColor)
g2.setFont(myFont) g2.drawString("Hello
World!",60,150) // End of paint() method //
End of MyFrame class
public class FrameTest1 public static void
main(String args) MyFrame frame new
MyFrame() frame.setTitle("Graphics Using
Frames") frame.show() // End of main
method // End of class FrameTest1
11Applications with Many Frames
- one can develop standalone graphical applications
with more than one window frame as shown in the
example below
class MyFrame1 extends Frame public
MyFrame1() final int FRAME_WIDTH
300 final int FRAME_HEIGHT
300 setSize(FRAME_WIDTH, FRAME_HEIGHT) //
End of MyFrame() constructor public void
paint(Graphics g) g.drawString("Hello
world!",100,100) // End of paint() method
// End of MyFrame1 class
12Applications with Many Frames (Contd)
class MyFrame2 extends Frame public
MyFrame2() final int FRAME_WIDTH
500 final int FRAME_HEIGHT
300 setSize(FRAME_WIDTH, FRAME_HEIGHT) //
End of MyFrame() constructor public void
paint(Graphics g) Graphics2D g2
(Graphics2D)g final int size 48 Color
myColor new Color(0.9F, 0.3F, 0.5F) Font
myFont new Font("Times", Font.BOLD,
size) g2.setColor(myColor) g2.setFont(myFont
) g2.drawString("Hello World!",60,150) //
End of paint() method
13Applications with Many Frames (Contd)
- import java.awt.
- import java.applet.
- public class MultiFrameTest1
-
- public static void main(String args)
-
- MyFrame1 frame1 new MyFrame1()
- frame1.setTitle("Graphics Using Frame 1")
- frame1.setSize(300, 300)
- frame1.show()
- MyFrame2 frame2 new MyFrame2()
- frame2.setTitle("Graphics Using Frame 2")
- frame2.setSize(300, 300)
- frame2.show()
- // End of main method
- // End of class MultiFrameTest1
14Java Support for Graphics Component Class
- public void paint(Graphics g)This method must
be overridden by the programmer to produce some
desired effect. Note that this is the same
paint() method that was used in the previous
applet example and the Applet class inherited it
from Component class. - When you make a change to the data, your drawing
is not automatically updated. You must tell the
window manager that the data has changed, by
calling the repaint method. - When any graphical t object is displayed, the
paint() method is called automatically. - To explicitly call paint() method, repaint()
method of the same object must called. - Then, repaint() method calls update() method
which in turn calls paint(). - Note that repaint() method performs some
system-dependent tasks and should thus NOT be
overridden. - The update() method is often called directly and
can be overridden to enable smoothening of
animations in multimedia applications.
15Displaying Images
- Java currently supports two image formats GIF
(Graphics Interchange Format) and JPEG(Joint
Photographic Expert Group). Image filename for
each of these types end with .gif and jpg. To
display an image, you need to perform the
following steps - 1- Retrieve the image from a file or from an
Internet source - 2-Draw the image.
- To load an image from a local file or download it
from an internet source you need to use
getImage() method in the Applet class. - To load image from the specified URL
- public Image getImage(URL url)
- You need to draw an image in a graphics context.
The Graphics class has the drawImage()method for
displaying an image - drawImage(Image img, int x, int y, Color bgcolor,
ImageObserver observer) - Observer is the object on which the image is
displayed.
16Example Displaying Images
import java.applet. import java.awt. import java.awt.event. public class DisplayImageApplet extends Applet implements ActionListener private ImageCanvas c //the canvas for displaying image private TextField tfFilename //the name of the image file private Button btShow //the "Show" button public void init() //create Panel p1 to hold a text field and a button Panel p1 new Panel() p1.setLayout(new FlowLayout()) p1.add(new Label("Filename")) p1.add(tfFilename new TextField(" ", 10)) p1.add(btShow new Button("Show")) //place an ImageCanvas object and p1 in the frame setLayout(new BorderLayout()) add("Center", c new ImageCanvas()) add("South", p1) c.setBackground(Color.gray) //register listener btShow.addActionListener(this) tfFilename.addActionListener(this) //handling the "Show" button public void actionPerformed(ActionEvent e) if ((e.getSource() instanceof Button) (e.getSource() instanceof TextField)) displayImage()
17Example Displaying Image(Cont.)
private void displayImage() //retrieving image Image image getImage(getCodeBase(), tfFilename.getText().trim()) //show image in the canvas c.showImage(image) //define the canvas for showing an image class ImageCanvas extends Canvas private String filename private Image image null public ImageCanvas() //set image public void showImage(Image image) this.image image repaint() public void paint(Graphics g) if (image ! null) g.drawImage(image, 0, 0, getSize().width, getSize().height, this)
18Example Displaying Image(Cont.)