Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

canvas does not display image insde a jframe

Posted on 2006-11-07
5
Medium Priority
?
701 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
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 160 total points
ID: 17892834
>     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
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?
0
 
LVL 92

Expert Comment

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

Author Comment

by:Knightley
ID: 17893581
ok thanx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

783 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