Start of Tutorial > Start of Trail | Search |
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:
You 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/index.html
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 theBufferedImage
into theGraphics2D
context and then draw the line to theGraphics2D
context. Here's a sample that does this:Map_Line.javaJust substitute the name of your image forimages/bld.jpg
.
Problem: How do you create aBufferedImage
from a gif or jpeg file?
- To create a
BufferedImage
from a gif or jpeg, you load your gif or jpeg into anImage
object and then draw theImage
to theBufferedImage
object. 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);getImage
is anApplet
method. If you have an application, you can use:Image img = Toolkit.getDefaultToolkit().getImage("picture.gif");BufferedImage.TYPE_INT_RGB
is one of manyBufferedImage
types. For more information, see:You need to create ahttp://java.sun.com/products/java-media/2D/forDevelopers/2Dapi/java/awt/image/BufferedImage.html
Graphics2D
context for theBufferedImage
by using thecreateGraphics
method. Then, you can use thedrawImage
method from theGraphics2D
class to draw the image into the buffered image. In theImageOps
sample 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 andBufferedImage
objects.
Problem: I can't compile the source code forStrokeAndFill.java
andTransform.java
with jdk1.2beta4.
- The
TextLayout.getOutline
implementation was changed between beta4 and the current JDK. The new implementation takes only anAffineTransform
as 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
GeneralPath
with them, or by implementingShape
andPathIterator
and reading back the line segments "on demand" to save the intermediate storage of theGeneralPath
object. 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
Graphics2D
was added with JDK 1.2 (now called Java 2 SDK). This class extendsGraphics
. There aredrawString
methods inGraphics2D
that you can use. If you will be rotating the text, you should useGraphics2D
rather thanGraphics
so that you can perform rotations and other transformations on yourGraphics2D
context.The
Transform
sample in the 2D tutorial doesn't usedrawString
to render the text. What happens is that aTextLayout
is created from the string "Text." TheTextLayout
allows us to create aShape
object from theString
by getting it's outline. We enter thisShape
into the shapes array, along with the rectangle and ellipse shapes. When we draw or fill the selectedShape
from the shapes array, we callg2.draw(Shape)
org2.fill(Shape)
.You could use
drawString
to render the text to theGraphics2D
context and then callg2.rotate
(angle of rotation). This will rotate everything that you've already rendered into theGraphics2D
context, however. So, you could reset theg2
context 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 theg2
context.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
Fonts
group of samples. There is another one in theTransforms
group 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.setTransform
to 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 aBufferedImage
and then draw thisBufferedImage
to theg2
context 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 |