Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


canvas does not display image insde a jframe

Posted on 2006-11-07
Medium Priority
Last Modified: 2011-09-20
Any body could help?
the app runs  without error, the image just does not show inside the jframe.

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

class t extends JFrame
      public t()
    setBounds(0, 0, 372, 272);
    Container con = this.getContentPane();
    GCanvas canvas = new GCanvas();
  public static void main(String arg[])
        new t();

class GCanvas extends Canvas // create a canvas for your graphics
      public Toolkit _toolkit;
      public Image _image;
  GCanvas() //constructor
        _toolkit = Toolkit.getDefaultToolkit();
            _image = _toolkit.getImage("s.jpg");
  public void paint(Graphics g) // display shapes on canvas
    // Graphics2D g2D = (Graphics2D) g;
    g.drawImage(_image, 0, 0, null);
Question by:Knightley
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 92

Accepted Solution

objects earned 160 total points
ID: 17892834
>     g.drawImage(_image, 0, 0, null);


    g.drawImage(_image, 0, 0, this);

or better still use a JLabel instead of GCanvas

Author Comment

ID: 17892882
well that is fast, works, thanx.

about JLabel, what i need is show a image and then get some mouse
position while i click my mouse on the image. both left mouse key
and right mouse key, any tutorial on this?

i guess i need to use a mouseAdapter or something but not sure
how to determine if the left or the right ouse button is clicked.

does JLabel support all this?
LVL 92

Expert Comment

ID: 17893231
JLabel can display ImageIcon's
You can use a MouseListener to get clicks

Author Comment

ID: 17893581
ok thanx

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question