|
-
May 31st, 2006, 02:28 PM
#1
Thread Starter
Fanatic Member
Create Image without createImage() for Graphics usage...
I created an image with the following code:
Code:
int pix[] = null;
Image image = createImage(new MemoryImageSource(width, height,
ColorModel.getRGBdefault(),
pix, 0, width));
it's working fine,
but when I try to draw within the image using the Graphics object... it's gives me an error at the method
image.getGraphics();
so... I'm currently using this code to fix the situation:
Code:
try {
image = ImageIO.read(new java.io.File("blank.bmp"));
} catch (Exception ex) {
}
Using a blank bmp to load the initial image... the reason I need to load it without a file is that I need to specify the width and height of the new image...!
I need my project done by midnight... any help before that time will greatly be appreciated... if not then I'll resolve to this file load patch... 
-
May 31st, 2006, 03:32 PM
#2
-
May 31st, 2006, 03:51 PM
#3
Thread Starter
Fanatic Member
Re: Create Image without createImage() for Graphics usage...
good, idea, creating a file instead of loading one... but how would I specify the image width and height from your code?:
Code:
fImage = new File("noimage.jpg");
-
May 31st, 2006, 04:20 PM
#4
Re: Create Image without createImage() for Graphics usage...
Manavo's code doesn't create a file, it creates an object of class File. uses the ImageIO to read the image from the file
Code:
Image image = ImageIO.read(fImage) ;
and puts the image in a label.
Anyway this code is useless for you.
Answering your question: after you create the image from the file, the width and height are set by the VM, you can use
Code:
image.getWidth(this);
image.getHeight(this);
to find the width and height of the image
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 31st, 2006, 04:21 PM
#5
Re: Create Image without createImage() for Graphics usage...
 Originally Posted by manavo11
Here is something I had done a while ago... Not sure if it's any help though. It's probably far from a good solution. I don't even remember if all the libraries I've imported are all needed  :
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class Picture extends Panel
{
public void createPicture(String fileName)
{
JLabel label = new JLabel();
try
{
File fImage = new File(fileName);
if (fImage.exists() == false)
{
fImage = new File("noimage.jpg");
}
Image image = ImageIO.read(fImage);
label = new JLabel(new ImageIcon(image));
}
catch (IOException e)
{
System.out.println("No image");
}
this.setLayout(new BorderLayout());
this.add(label,BorderLayout.CENTER);
}
public Picture(String fileName)
{
createPicture(fileName);
}
}
those are your proper imports:
Code:
import java.awt.* ;
import java.io.* ;
import javax.imageio.* ;
import javax.swing.* ;
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 31st, 2006, 04:26 PM
#6
Thread Starter
Fanatic Member
Re: Create Image without createImage() for Graphics usage...
nonono, I don't need to find the height and width... I need to create a blank image with specified height and width... AND be able to use the Graphics class on it...
-
May 31st, 2006, 04:31 PM
#7
Re: Create Image without createImage() for Graphics usage...
then you don't need to use the ImageIO class, create an Image normally (setting width and height) and use the createGraphics method on it.
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 31st, 2006, 04:36 PM
#8
Thread Starter
Fanatic Member
Re: Create Image without createImage() for Graphics usage...
try it, it causes an exception!
saying the getGraphics method cannot be called over an image produced by the "ImageProducer"...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|