Solved

canvas does not display image insde a jframe

Posted on 2006-11-07
5
659 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

775 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