Solved

canvas does not display image insde a jframe

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 213
diffSum example 4 51
servlet example issue 6 46
Notify sent to other threads in Java 9 33
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 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…
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 …
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.

828 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