Solved

canvas does not display image insde a jframe

Posted on 2006-11-07
5
637 Views
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()
      {
    super("Spiel");
    setBounds(0, 0, 372, 272);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    Container con = this.getContentPane();
   
    GCanvas canvas = new GCanvas();
    con.add(canvas);
    setVisible(true);
  }
 
  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);
  }
}
0
Comment
Question by:Knightley
  • 2
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 40 total points
Comment Utility
>     g.drawImage(_image, 0, 0, null);

try:

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


or better still use a JLabel instead of GCanvas
0
 
LVL 1

Author Comment

by:Knightley
Comment Utility
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?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
JLabel can display ImageIcon's
You can use a MouseListener to get clicks
0
 
LVL 1

Author Comment

by:Knightley
Comment Utility
ok thanx
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now