| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
| Working with Graphics in Java |
|
Here You will learn how to create the simple geometric shapes and display images.
|
|
Drawing simple Shapes
|
|
Graphics class defines methods for drawing the following types of shapes:
- Lines ( drawLine() )
- Rectangles ( drawRect() & fillRect() )
- Raised or lowered rectangles ( draw3DRect() and fill3DRect() )
- Round-edged rectangles ( drawRoundRect() and fillRoundRect() )
- Ovals ( drawOval() and fillOval())
- Arcs ( drawArc() and fillArc() )
- Polygons ( drawPolygon() and fillPolygon() )
Except for polygons and lines, all shapes are specified using their bounding rectangle.
Once you understand rectangles, drawing other shapes is relatively easy. For this
reason, this page will concentrate on rectangle drawing.
|
|
Drawing Simple Rectangle
|
|
The applet used the draw3DRect() and fillRect() methods to draw
its interface. Here is an applet to drawing simple rectangle
|
//In FramedArea (a Panel subclass):
public void paint(Graphics g)
{
Dimension d = size();
Color bg = getBackground();
//Draw a fancy frame around the applet.
g.setColor(bg);
g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
}
//In CoordinateArea (a Canvas subclass):
public void paint(Graphics g)
{
//If user has clicked, paint a tiny rectangle where click occurred
if (point != null)
{
g.fillRect(point.x - 1, point.y - 1, 2, 2);
}
}
|
|
|
Loading the Images
|
|
AWT makes it easy to load the images in either of the two formats: GIF and JPEG. The Applet
and Toolkit classes provides the getImage() methods that works for either format.We use
them like this:
|
myImage = getImage(URL); //in an Applet subclass only
or
myImage = Toolkit.getDefaultToolkit().getImage(filenameOrURL);
|
|
|
getImage() methods returns immediately, so that we don't have to wait for the
image to be loaded before going to perform other operations in our program.
While this improves the performance, some programs requires more control or information
about the image loading. We can track image loading status either by using MediaTracker
class or by implementing an imageUpdate() method defined by the ImageObserver
interface.
|
|
Displaying the Images
|
|
It is easy to display an image using Graphics object that is passed into our
update() or paint() methods. we simply invoke the drawImage() method on the Graphics
object. Consider for example:
|
|
g.drawImage(myImage, 0, 0, this);
|
|
|
This section explains four forms of the drawImage(), two of which scale image.
Like getImage(), drawImage() is asynchronous, returning immediately even if the image
has not been fully loaded or drawn yet.
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords: graphics in java shape, graphics context, graphics in java drawing,
graphics in java platforms
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|