Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

draw a JPG 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
0
0
CH
hi,
i'm driving nuts not understanding what it takes to draw a jpg to my tab panel (or any other place in my applet).

this is what i did so far. it fails when i want to instantiate the Graphics object. what am i doing wrong

String firstImage = "C:\\folder\\photos\\test.jpg";
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = tk.getImage(firstImage);
img = getImage(getDocumentBase(), firstImage);
Graphics g = img.getGraphics();


i'd appreciate any help. thanks heaps. (btw. i just read Toolkit class touches OperationSystem dependent resp. native OS behaviours. Any other way to output images in an applet?)
 
btw. error message is always a nullpointerexeption no mather how i code. this example also throws an error where i want to get the graphics.


String firstImage = "C:\\$User\\FotosDownload\\test.jpg";
Image img = getImage(getCodeBase(),firstImage);
Graphics g = img.getGraphics();
 
I don't believe that getImage() can read jpgs ... only gifs.
In any case, the below code is a lot more scalable, as it will read any image type.

Here is an example using a JFrame :

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class TestImage extends JFrame {
	public static void main(String args[]) {
		new TestImage();
	}

	public TestImage() {
		this.setSize(500, 500);
		this.show();

	}

	public void paint(Graphics g) {

		String firstImage = "C:\\java\\coastline2.jpg";
		try {
			BufferedImage bi = ImageIO.read(new java.io.FileInputStream(firstImage));
			g.drawImage(bi, 0,0, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

--------------------------------------------------
Free Database Connection Pooling Software
 
valuable code. thanks a lot. this is what i was looking for. cheers.
 
sedj,

To catch on, do you think this is a way that works on any operating system (running this java applet)?

The path of the image is no issue. since the path is given by a selection from a jTree.

thanks heaps

 
Yes, this code will run on any OS.

All windowing & imaging Java classes will run on any OS, but their implementation on the native level differs from OS to OS.

Java achieves platform independance by having the parts that interface with the OS (ie socket code, file IO, windowing, imaging etc), that have to be native (ie C/C++) all written for each platform. So the code that creates a JFrame, or Applet is different on the native implementation level from Win32 to Linux.

--------------------------------------------------
Free Database Connection Pooling Software
 
Sedj, I just stumbled over the state-of-art API. I thought you (and others) might be intrested. Sun developed a very sufisticated library for this topic due to high demand.


In case you're into graphics, have a closer look at the interactive tutorial. well done.
 
Actually, JAI is not as great as you may think ... while it is written in Java, and has lots of functionality, its is actually quite slow. The C library GD is a lot quicker, and is capable of many of the things JAI does. I had written an imaging server in JAI, but had to dump JAI in favour of GD, because it was about 10 times quicker on loading, scaling, cropping and other image manipulation techniques.

Actually, the javax.imageio package was written by the same people that wrote JAI ...

--------------------------------------------------
Free Database Connection Pooling Software
 
from what i've seen so far i'd agree. if i'd know that there are win clients only connecting, i'd use GD or ImageMagick(.org)... but....

cheers
 
GD will compile on Win32 or linux ...
To be fair though, for loading images into Applets, I would still use the ImageIO.read() method ... it performs OK for that kind of operation :)

--------------------------------------------------
Free Database Connection Pooling Software
 
Sedj,

u got a clue where the ImageDisplay class of JAI resides? (frankly speaking i got no clue wheather it's even part of it)

it's in the code for displaying images everywhere at suns doc. but even searching the library didn't turn in any hints.
e.g.
canvas = new ImageDisplay(src_gray);

any clue?
cheers
 
If I remember correctly, ImageDisplay is an old class that you shouldn't use anymore.

Try the class com.sun.media.jai.widget.DisplayJAI instead, which is in jai_core.jar

You might want to look at JadeDisplay :


--------------------------------------------------
Free Database Connection Pooling Software
 
Sedj,

hi again. short question. did u notice the akward quality of the output image when scaled with JAI? is there a way to get better antialiasing? (jpeg images only)

thanks

my code
----------------------------------
PlanarImage canvas = JAI.create("fileload", "D:\\image.jpg");
ParameterBlock pb = new ParameterBlock();
pb.addSource(canvas); // The source image
pb.add(0.3F); // The x scale factor
pb.add(0.3F); // The y scale factor
pb.add(0F); // The x translation
pb.add(0F); // The y translation
pb.add(new InterpolationBilinear());


// Create the scale operation...
RenderingHints hints= null;
RenderingHints hints = new RenderingHints(JAI.KEY_INTERPOLATION, "InterpolationBiliniear");

RenderedOp scaledImg = JAI.create("scale", pb, hints);
DisplayJAI img = new DisplayJAI(scaledImg);
//output to panel
jPanelBild.add(img);
 
I always used this code for scaling, it seems to work fine :

Code:
RenderedOp image = JAI.create("fileload", "D:\\image.jpg");
float scale = 0.3f;
image = JAI.create("scale",
		(new ParameterBlock())
		.addSource(image)
		.add(scale)
		.add(scale)
		.add(0.0f)
		.add(0.0f)
		.add(new InterpolationNearest())
		,null
	);
DisplayJAI img = new DisplayJAI(image);

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top