9 Replies - 4202 Views - Last Post: 18 August 2011 - 01:38 PM Rate Topic: -----

#1 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

Drawing the image in JFrame

Posted 17 August 2011 - 01:48 PM

Hello guys need help!

I am trying to draw the image in JFrame,,but do not know why I am always getting null pointer exception with my code

public final class NewMain extends JFrame {

    Image img;
    
    NewMain(String MYFRAME) throws IOException {

        Canvas c = new Canvas();
        img = c.createImage(100, 100);

        Graphics gc = img.getGraphics();
        gc.setColor(Color.red);
        gc.fillRect(0, 0, 100, 100);

        Container cnt = this.getContentPane();
        cnt.setBackground(Color.yellow);
        cnt.add(c);

        this.setSize(1000, 1000);
        this.setVisible(true);
    }

    public static void main(String args[]) throws IOException {

        NewMain ob1 = new NewMain("MYFRAME");
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(img, 100, 100, this);
    }
}



any suggetions?

Is This A Good Question/Topic? 0
  • +

Replies To: Drawing the image in JFrame

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Drawing the image in JFrame

Posted 17 August 2011 - 02:01 PM

Quote

Returns:
an off-screen drawable image, which can be used for double buffering. The return value may be null if the component is not displayable. This will always happen if GraphicsEnvironment.isHeadless() returns true.
~ Component.createImage(int, int)

Don't know much about Swing, but that could be your problem. Maybe you want to do something like this
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Drawing the image in JFrame

Posted 17 August 2011 - 02:08 PM

When you use the method createImage(); on a Component that component must be displayable for the image to be created or the returned Image will be null

so before doing:

Canvas c = new Canvas();
img = c.createImage(100, 100);

you will have to put your Canvas into the JFrame, to make the JFRame visible and give a certain Dimension to your JFrame

This post has been edited by pbl: 17 August 2011 - 02:10 PM
Reason for edit:: *Edited: ninja'd by [member=blackcompe]

Was This Post Helpful? 1
  • +
  • -

#4 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

Re: Drawing the image in JFrame

Posted 17 August 2011 - 02:18 PM

View Postpbl, on 17 August 2011 - 02:08 PM, said:

When you use the method createImage(); on a Component that component must be displayable for the image to be created or the returned Image will be null

so before doing:

Canvas c = new Canvas();
img = c.createImage(100, 100);

you will have to put your Canvas into the JFrame, to make the JFRame visible and give a certain Dimension to your JFrame


thanks pbl..

its now not giving the Null pointer problem but i can't see my image in Jframe

this.setSize(1000, 1000);
        this.setVisible(true);
        
        Container cnt = this.getContentPane();
        cnt.setBackground(Color.yellow);

        Canvas c = new Canvas();      
        img = c.createImage(100, 100);
        
        Graphics gc = img.getGraphics();
        gc.setColor(Color.red);
        
        gc.fillRect(0, 0, 100, 100);
        cnt.add(c); 



any idea?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Drawing the image in JFrame

Posted 17 August 2011 - 02:25 PM

I doubt... you might see a part of the Frame, but you will still have the null pointer Exception.

As long as the Canvas is not visible you cannot extract an Image out of it. And as the Canavas is not (based on your code) on the contentPane yet, it is not visible

the createImage() method has to know the size of the displayable area of the Canavas before being able to actually draw.
Was This Post Helpful? 0
  • +
  • -

#6 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

Re: Drawing the image in JFrame

Posted 17 August 2011 - 02:35 PM

View Postpbl, on 17 August 2011 - 02:25 PM, said:

I doubt... you might see a part of the Frame, but you will still have the null pointer Exception.

As long as the Canvas is not visible you cannot extract an Image out of it. And as the Canavas is not (based on your code) on the contentPane yet, it is not visible

the createImage() method has to know the size of the displayable area of the Canavas before being able to actually draw.


well you are absolutely right:I did not notice the error:
Now after making some more changes::I can see the Jframe and no Erro::but image is not visible

this.setSize(1000, 1000);
        this.setVisible(true);
        
        Container cnt = this.getContentPane();

        Canvas c = new Canvas();
        c.setSize(200, 200);
        c.setVisible(true);
        cnt.add(c);
        
        img = c.createImage(100, 100);
        Graphics gc = img.getGraphics();
        gc.setColor(Color.red);
        gc.fillRect(0, 0, 100, 100);


Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: Drawing the image in JFrame

Posted 17 August 2011 - 03:03 PM

Why do you getGraphics() (which isn't good practice) and override paint()? The best thing you can do is:
-Extend JPanel and override its paint() method to draw the Image on it
-Add an instance of your JPanel subclass to the JFrame
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Drawing the image in JFrame

Posted 17 August 2011 - 07:13 PM

The paint() method of your Canvas knows nothing about the image
You will have to overload it to draw your image

import javax.swing.*;
import java.awt.*;

public final class NewMain extends JFrame {

    Image img;
    
    NewMain(String MYFRAME) {

        Canvas c = new MyCanvas();

        Container cnt = this.getContentPane();
        cnt.setBackground(Color.yellow);
        cnt.add(c);

        this.setSize(500, 500);
        this.setVisible(true);
        img = c.createImage(100, 100);

        Graphics gc = img.getGraphics();
        gc.setColor(Color.red);
        gc.fillRect(0, 0, 100, 100);
    }

    public static void main(String args[]) {

        NewMain ob1 = new NewMain("MYFRAME");
    }

    @Override
    public void paint(Graphics g) {
    	super.paint(g);
        g.drawImage(img, 100, 100, this);
    }
    
    class MyCanvas extends Canvas {
    	public void paint(Graphics g) {
            g.drawImage(img, 100, 100, this);
    		
    	}
    }
}



Was This Post Helpful? 0
  • +
  • -

#9 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

Re: Drawing the image in JFrame

Posted 18 August 2011 - 10:54 AM

View Postpbl, on 17 August 2011 - 07:13 PM, said:

The paint() method of your Canvas knows nothing about the image
You will have to overload it to draw your image

import javax.swing.*;
import java.awt.*;

public final class NewMain extends JFrame {

    Image img;
    
    NewMain(String MYFRAME) {

        Canvas c = new MyCanvas();

        Container cnt = this.getContentPane();
        cnt.setBackground(Color.yellow);
        cnt.add(c);

        this.setSize(500, 500);
        this.setVisible(true);
        img = c.createImage(100, 100);

        Graphics gc = img.getGraphics();
        gc.setColor(Color.red);
        gc.fillRect(0, 0, 100, 100);
    }

    public static void main(String args[]) {

        NewMain ob1 = new NewMain("MYFRAME");
    }

    @Override
    public void paint(Graphics g) {
    	super.paint(g);
        g.drawImage(img, 100, 100, this);
    }
    
    class MyCanvas extends Canvas {
    	public void paint(Graphics g) {
            g.drawImage(img, 100, 100, this);
    		
    	}
    }
}




thanks pbl and max for your reply:but can't we directly create the image with the JFrame create Image method or we always need to used canvas to do that?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Drawing the image in JFrame

Posted 18 August 2011 - 01:38 PM

View Postmahi.aw, on 18 August 2011 - 01:54 PM, said:

thanks pbl and max for your reply:but can't we directly create the image with the JFrame create Image method or we always need to used canvas to do that?

Weird question :) Actually you kind of miss a concept here
Any Component will accept createImage() so this will work excactly the same way

public final class NewMain extends JFrame {

    Image img;
    
    NewMain(String MYFRAME) {

        Container cnt = this.getContentPane();
        cnt.setBackground(Color.yellow);

        this.setSize(500, 500);
        this.setVisible(true);
        img = createImage(100, 100);

        Graphics gc = img.getGraphics();
        gc.setColor(Color.red);
        gc.fillRect(0, 0, 100, 100);
    }

    public static void main(String args[]) {

        NewMain ob1 = new NewMain("MYFRAME");
    }

    @Override
    public void paint(Graphics g) {
    	super.paint(g);
        g.drawImage(img, 100, 100, this);
    }    
}



So why not simply make

public final class NewMain extends JFrame {

    NewMain(String MYFRAME) {

        Container cnt = this.getContentPane();
        cnt.setBackground(Color.yellow);

        this.setSize(500, 500);
        this.setVisible(true);
    }

    public static void main(String args[]) {

        NewMain ob1 = new NewMain("MYFRAME");
    }

    @Override
    public void paint(Graphics g) {
    	super.paint(g);
        g.setColor(Color.red);
        g.fillRect(100, 100, 100, 100);
    }    
}


The advantage in using the Canavas i that it will be in the CENTER region of the BorderLayout

But Canvas are usually used when you do Buffered Image outside the displayed frame like in:

http://www.dreaminco...way-to-repaint/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1