
This is a discussion on Applets: Paint & Graphics within the Java forums, part of the Application Development category; Can anyone link me to a GOOD tutorial for applets and the paint method and how graphics work all around? ...
| |||||||
| 120 Online | Register | Projects | Products | Members List | Calendar | Today's Posts | Search |
| |||
| Applets: Paint & Graphics
Can anyone link me to a GOOD tutorial for applets and the paint method and how graphics work all around? I've searched and searched but can't find a site that's really explained how to use graphics to paint on the screen from the view of a person who's never used that function of Java before. I just need to get the basic idea of it down and I haven't been able to do that with the walk-throughs that I've found.
|
| |||
|
Applets involving graphics and animations usually look more exciting than applets that don't. Here is a basic overview of how to implement graphics in an applet. 1. To draw images, import the class Image. Type this at the top of your code (not in your class): import java.awt.Image; Now, create an Image object. Here is the code for making an Image object. Instead of typing "getCodeBase()", you can also replace it with a URL. If your picture file is inside a folder, you can also type that folder with the name: Image myImage = getImage(getCodeBase(), "FolderName\\Pic5.jpg"); To draw the image, use the drawImage method of Graphics. The arguments to the method should be, in order, the image object name, the x coordinate, the y coordinate, the width, the height, and "this". Here is the code snippet: g.drawImage(myImage, 300, 200, 60, 120, this); The image's top left corner will be at (300,200). Its width will be 60 pixels, and its height will be 120 pixels. Ads by Google Java Graph Applet Create, analyze, layout and print diagrams and graphs online. yEd - Graph Editor Java SCJP Questions SCJP mock exam Easy to use interface JavaQ | Sun certified java professional practice exam |Welcome Graphics2D In the majority of browsers the Graphics object that the paint method receives can be casted into Graphics2D that provides more advanced features: Graphics2D g2d = (Grapics2D) g; // Turn everything that we will paint by the angle of 45 degrees: g2d.rotate(Math.PI/4); Generated images.To show your generated images like Mandelbrot set, use the BufferedImage class:Graphics2D g2d = (Grapics2D) g; // Create 40*50 buffered image BufferedImage img = new BufferedImage(40, 50, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D imageGraphics = img.createGraphics(); // Paint something here using the given graphics object imageGraphics.setColor(Color.BLUE); imageGraphics.drawLine(5,5,10,10); // Set individual pixels (red dot at 1,1): int redRGB = Color.RED.getRGB(); img.setRGB(1,1, redRGB); // Draw the image as in example above: g.drawImage(img, 300, 200, 60, 120, this); Painting the BufferedImage will likely to take much less time than preparing it, so think if it is not possible to remember the image, reusing the same one if nothing has changed since the paint method have been previously called. |
![]() |
| |