|
|
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Also see Improving Printing Performance, which has tips for solving printing performance problems.
Problem: I can run my Java2D applets with appletviewer, but they do not run in my browser. The Java console of the browser says:
defn not found for java/awt/Graphics2D.
- You need to download Java Plugin 1.2 to run Swing and 2D applets in a browser. You can download the plugin here:
http://java.sun.com/products/plugin/index.htmlYou will need to adjust your HTML files to point to the plugin. Here is a page with several examples, including a 2D sample at the bottom:http://java.sun.com/products/plugin/1.2/demos/applets.html
Problem: How can you write over a previous image? Our problem is that our applet displays an image of a map, but when we draw a line on top of the map, the line overwrites the map.
- You should try drawing your image into a
BufferedImage. Then, draw theBufferedImageinto theGraphics2Dcontext and then draw the line to theGraphics2Dcontext. Here's a sample that does this:Map_Line.javaJust substitute the name of your image forimages/bld.jpg.
Problem: How do you create aBufferedImagefrom a gif or jpeg file?
- To create a
BufferedImagefrom a gif or jpeg, you load your gif or jpeg into anImageobject and then draw theImageto theBufferedImageobject. The following snippet illustrates this:Image img = getImage("picture.gif"); int width = img.getWidth(this); int height = img.getHeight(this); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D biContext = bi.createGraphics(); biContext.drawImage(img, 0, 0, null);getImageis anAppletmethod. If you have an application, you can use:Image img = Toolkit.getDefaultToolkit().getImage("picture.gif"); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img, 0); tracker.waitForID(0); } catch (Exception e) {} int width = img.getWidth(this); int height = img.getHeight(this); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D biContext = bi.createGraphics(); biContext.drawImage(img, 0, 0, null);BufferedImage.TYPE_INT_RGBis one of manyBufferedImagetypes. For more information, see:http://java.sun.com/products/java-media/2D/forDevelopers/2Dapi/java/awt/image/BufferedImage.htmlYou need to create aGraphics2Dcontext for theBufferedImageby using thecreateGraphicsmethod. Then, you can use thedrawImagemethod from theGraphics2Dclass to draw the image into the buffered image. In theImageOpssample on the "Filtering aBufferedImage" page, images are drawn into buffered images. You could also look at this page: http://java.sun.com/products/java-media/2D/samples/index.html, which has links to samples that use gifs, jpegs and
BufferedImageobjects.
Problem: I can't compile the source code forStrokeAndFill.javaandTransform.javawith jdk1.2beta4.
- The
TextLayout.getOutlineimplementation was changed between beta4 and the current JDK. The new implementation takes only anAffineTransformas an argument. You need to download the new JDK to run the sample.
Problem: Is there a way to specify a formula for a line and draw a picture according to it?
- Using line segments would be the easiest way. You can represent the line segments either by filling a
GeneralPathwith them, or by implementingShapeandPathIteratorand reading back the line segments "on demand" to save the intermediate storage of theGeneralPathobject. Note that you could analyze your formula and determine how to match it with cubic or quadratic bezier curves, but this is probably overkill.
Problem: How do I add text to a graphic field at a certain location?
- A class called
Graphics2Dwas added with JDK 1.2 (now called Java 2 SDK). This class extendsGraphics. There aredrawStringmethods inGraphics2Dthat you can use. If you will be rotating the text, you should useGraphics2Drather thanGraphicsso that you can perform rotations and other transformations on yourGraphics2Dcontext.The
Transformsample in the 2D tutorial doesn't usedrawStringto render the text. What happens is that aTextLayoutis created from the string "Text." TheTextLayoutallows us to create aShapeobject from theStringby getting it's outline. We enter thisShapeinto the shapes array, along with the rectangle and ellipse shapes. When we draw or fill the selectedShapefrom the shapes array, we callg2.draw(Shape)org2.fill(Shape).You could use
drawStringto render the text to theGraphics2Dcontext and then callg2.rotate(angle of rotation). This will rotate everything that you've already rendered into theGraphics2Dcontext, however. So, you could reset theg2context transform each time you want to transform a particular image or piece of text in the context separately from other things that have already been rendered into theg2context.There are other 2D samples you can look at here: http://java.sun.com/products/java-media/2D/samples/index.html
![]()
They are a little more advanced than the tutorial samples. If you will be transforming text, I would recommend looking at the sample, "Transformation of Characters" from the
Fontsgroup of samples. There is another one in theTransformsgroup of samples that performs animation of both text and images rotating, scaling and translating. It is the third sample in this group. This sample does useg2.drawString. It usesg2.setTransformto reset the transformation for each object to be transformed. If you will be using Swing components in your interface, keep in mind that these components are affected when you useg2.setTransform. To fix this problem, you can perform all of your drawing in aBufferedImageand then draw thisBufferedImageto theg2context when you are finished drawing. This sample solves the problem in this way.
Problem: I noticed your comment on the bottom of Creating and Deriving Fonts regarding bug 4155852. This bug has been closed with no action taken. Is it true that you can't apply a style to fonts such as Arial?The problem is that font-to-style matching is not working properly for physical fonts (such as Arial or Palatino). You can only apply styles to logical fonts at this point (such as Dialog or SansSerif). As a workaround until the bug is fixed, you could do the following:
instead of:Font f = new Font("Palatino Bold", Font.PLAIN, 12);Font f = new Font("Palatino", Font.BOLD, 12);
|
|
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.