Results 1 to 8 of 8

Thread: Create Image without createImage() for Graphics usage...

  1. #1

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Arrow 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...


    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Create Image without createImage() for Graphics usage...

    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);
    	}
    }


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    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");

    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Create Image without createImage() for Graphics usage...

    Quote 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

  6. #6

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    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...


    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  8. #8

    Thread Starter
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    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"...



    Using VB.NET 2005/.NET 2.0, NetBeans IDE 5, Fujitsu Cobol85,
    Website: http://DreamForgery.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width