×

Discussion Board

Results 1 to 11 of 11

Hybrid View

  1. #1
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    Viewing image on Personal Profile

    I tring to do this since yesterday and i ve got no idea what is wrong. This is teh code:
    <code>
    import java.awt.*;
    import java.awt.event.*;

    public class proba extends Frame {
    private Image image;

    public proba(String fileName) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    image = toolkit.getImage(fileName);
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 0);
    try
    {
    mediaTracker.waitForID(0);
    }
    catch (InterruptedException ie)
    {
    System.err.println(ie);
    System.exit(1);
    }
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    setSize(image.getWidth(null), image.getHeight(null));
    setTitle(fileName);
    show();
    }

    public void paint(Graphics graphics) {
    graphics.drawImage(image, 0, 0, null);
    }

    public static void main(String[] args) {
    new proba("res/scott.jpg");
    }
    }
    </code>

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Hi,

    Create a Canvas object, add the canvas to your Frame and override the paint() method of your canvas with the one that draws the image. That's all. The paint() method of the Frame is not suitable for this purpose.

    An example ImageCanvas (a bit more complicated because it is part of an image button class, supports double buffer, offset etc):



    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.Graphics;
    import java.awt.SystemColor;


    public class ImageCanvas extends Canvas {

    private Image image;
    private int horizontalRepaintOffset;
    private int verticalRepaintOffset;
    private Color bgColor = SystemColor.control;
    private boolean doubleBuffered = true; //true is the default value
    private Image offscreenImage;
    protected int width;
    protected int height;



    public ImageCanvas(Image image) {
    this.image = image;
    width = image.getWidth(null);
    height = image.getHeight(null);
    }


    public final void setBackground(Color bgColor) {
    this.bgColor = bgColor;
    paint(getGraphics());
    }


    //during redrawing the the background is filled with white if the color isn't set thru this method. I don't know why. You can set the redrawing background color here

    public final void setBaseBackground(Color bgColor) {
    super.setBackground(bgColor);
    }


    public final void setDoubleBuffered(boolean doubleBuffered) {
    this.doubleBuffered = doubleBuffered;
    }


    public final void setImage(Image image) {

    if(this.image == image)
    return;
    if(image == null)
    return;
    this.image = image;
    if(width != image.getWidth(null) || height != image.getHeight(null)) {
    width = image.getWidth(null);
    height = image.getHeight(null);
    setSize(width, height);
    }
    }


    public final void setRepaintOffset(int horizontal, int vertical) {
    horizontalRepaintOffset = horizontal;
    verticalRepaintOffset = vertical;
    }


    public final void paint(final Graphics g) {
    if(g == null)
    return;

    if(doubleBuffered) {
    if(offscreenImage == null) {
    offscreenImage = createImage(width, height); //TODO after resize we must set offscreenImage to null
    }
    final Graphics offscreenGraphics = offscreenImage.getGraphics();
    offscreenGraphics.setColor(bgColor);
    offscreenGraphics.fillRect(0, 0, width, height);
    if(image != null)
    offscreenGraphics.drawImage(image, horizontalRepaintOffset, verticalRepaintOffset, null);
    g.drawImage(offscreenImage, 0, 0, null);
    offscreenGraphics.dispose();
    g.dispose();
    return;

    }

    g.setColor(bgColor);
    g.fillRect(0, 0, width, height);
    if(image != null)
    g.drawImage(image, horizontalRepaintOffset, verticalRepaintOffset, null);
    g.dispose();
    }


    public final void repaint() {
    paint(getGraphics());
    }
    }

  3. #3
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    no can do

    tried the thing you have described in mail as fallows and still nothing here is the code that uses Yours:
    import java.awt.*;
    import java.awt.event.*;

    public class proba extends Frame {
    private Image image;

    public proba() {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image im = tk.createImage("res/scott.jpg");
    CommercialCanvas cv = new CommercialCanvas(im);
    add(cv);
    pack();
    show();


    }

    public void paint(Graphics graphics) {
    graphics.drawImage(image, 0, 0, null);
    }

    public static void main(String[] args) {
    new proba();
    }
    }

    wainting for reply

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Hi,

    Don't forget to use MediaTracker for the images. That will wait until the image is loaded properly and ready to use.

    So, create the Image and do this:
    final MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(image, 0);
    try {
    tracker.waitForAll();
    }
    catch(java.lang.Exception e) {
    e.printStackTrace();
    }

    After this MediaTracker stuff, create the canvas and pass it the image object.

  5. #5
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    i ve done it also

    i have done it already but still nothing, i even tried to put a button above and beneth it to see if everything is fine with positioning and still I get a button a blank area and a button. I even made an experiment copied the code to eclipse 3.0 SE environment and compile it and run it - i get an error during execution under eclipse !!! what is wrong i have no idea i run debugger and the picture data isn't null so what is happening ...

    AWT isn't compatibile in PP and SE ??

    mchmiel

  6. #6
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    correction

    as always the path to png file was incorrect for the eclipse SE project i ve done the changes to PP procect in WSDD and nothing blank on Nokia s80 emulator but when i run the application under simple Run application in WSDD everything is oki ??? any ideas??

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    The method I described works.

    Try to use gif or jpg images.
    Create an image object (make sure the path is correct in pp environment)
    Load the image with MediaTracker.
    Create an ImageCanvas, pass the Image object to its constructor
    add ImageCanvas to your Frame.
    Show Frame. That's all. It must work.

  8. #8
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    Perhaps this is the answer

    Tried everything You have described, and still no results. Everytime i execute an emulator of S80 i do not get the desired picture on my canvas.
    Perhaps it is the fault of the environment so i would like You to execute my simple class in Your IDE. Could You do that ??

    Or perhaps You send me some similar example to execute.Very simple ofcourse.

    Thanx in advance.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Maybe you canvas has no size (0 pixel width, 0 pixel height).

  10. #10
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    no can do my canvas has the right size

    check the code:

    import java.awt.*;
    import java.awt.event.*;

    public class proba extends Frame {
    private Image image;

    public proba() {
    this.setLayout(new GridLayout(3, 1));

    Toolkit tk = Toolkit.getDefaultToolkit();

    Image im = tk.createImage("reklama.gif");
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(im, 1);
    try {
    tracker.waitForID(1);

    } catch (Exception e) {
    e.printStackTrace();
    }
    CommercialCanvas cv = new CommercialCanvas(im);

    cv.setSize(new Dimension(im.getWidth(null),im.getHeight(null)));
    cv.setVisible(true);

    add(cv);
    cv.repaint();
    pack();
    show();
    setVisible(true);


    }

    public static void main(String[] args) {


    System.out.println("IDZIEMY");
    new proba();
    }
    }

    class CommercialCanvas extends Canvas {


    private Image image;

    public CommercialCanvas(Image image) {
    this.image = image;
    }


    public void paint(Graphics g) {
    g.drawImage(
    image,
    10,10,image.getWidth(this),image.getHeight(this),
    this);


    }




    }


    please try to run the code on Your emulator od S80 perhaps somethings is wrong with mine.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Upper left position should be 0, 0 and not 10, 10 in g.drawImage(). And temporarily change the bg color of you canvas to some vivid color and see if it's shown on your frame (in the appropriate size).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×