Solved

canvas does not display image insde a jframe

Posted on 2006-11-07
5
678 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
[X]
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
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 271
going to wrong jsp page 2 71
printf performancy 11 106
Why is enum singleton a better approach than static factory 3 67
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 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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

710 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