Re: Essentially skinning my app..



jackroofman@xxxxxxxxx wrote:
How do I make this ImageViewer work? I can't get it to so much as call
its paint command. Trying to set a JComponent to an ImageViewer only
results in throwing a NullPointerException.

It's clearly doing something, because a System.out.println() of the
ImageViewer does return a slew of:

sun.awt.image.ToolkitImage@1a33d48
sun.awt.image.ToolkitImage@b2002f
sun.awt.image.ToolkitImage@406199
sun.awt.image.ToolkitImage@1f6f296
sun.awt.image.ToolkitImage@1df5a8f
sun.awt.image.ToolkitImage@1e13d52
sun.awt.image.ToolkitImage@1b9ce4b
sun.awt.image.ToolkitImage@1292d26
sun.awt.image.ToolkitImage@1db699b

but I can't actually get anything to come of it.



How about some thing like this... using a canvas. Use a special Canvas to preload 2 GIFs, and using a MouseListener simply toggle the image.

[JDK1.1]

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

public class ToggleGifCanvas extends Canvas
  implements MouseListener {
  Image img1, img2;
  int     index = 0;
  MediaTracker tracker;

  public ToggleGifCanvas(URL n1, URL n2) {
    tracker = new MediaTracker(this);
    try {
      img1 = Toolkit.getDefaultToolkit().getImage(n1);
      img2 = Toolkit.getDefaultToolkit().getImage(n2);
      tracker.addImage(img1,0);
      tracker.addImage(img2,1);
      tracker.waitForAll();
      addMouseListener(this);
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }

  public void paint(Graphics g) {
    if (img1 != null) {
       if (index == 0) {
          g.drawImage(img1,0,0,this);
          index++;
          }
       else {
          g.drawImage(img2,0,0,this);
          index--;
          }
       }
     }

  public Dimension getPreferredSize (){
    return new Dimension
      (img1.getHeight(this), img2.getWidth(this));
    }

  public void mouseClicked(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {
    index = 1; repaint();
    }
  public void mouseExited(MouseEvent e) {
    index = 0; repaint();
     }
}

To use such Canvas, try something like this. This example needs our Gumby GIFs ( and ).

import java.applet.*;
import java.awt.*;
import java.net.*;

public class tcanvas extends Applet {
  ToggleGifCanvas tgc;

  public void init() {
     try {
       tgc = new ToggleGifCanvas
           (new URL(getDocumentBase(),"gumby.gif"),
            new URL(getDocumentBase(),"gumby2.gif"));
       add(tgc);
       }
     catch (Exception e) {
       e.printStackTrace();
       }
    }
  }


--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________


'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
.



Relevant Pages

  • Adding a MouseListener to a canvas.
    ... I'm having problems adding a MouseListener to a canvas. ... public void MouseClicked{ ... But since the class MousingAround extends Canvas... ...
    (comp.lang.java.gui)
  • awt and placing images - infinite loop?
    ... I am using a Frame, and within that placing a Canvas. ... gameCanvas = new GameCanvas; ... public void mouseClicked ...
    (comp.lang.java.gui)
  • Re: Canvas
    ... I added a Canvas in a JFrame. ... public void actionPerformed{ ... public class MyCanvas2 extends Canvas{ ...
    (comp.lang.java.programmer)
  • Re: Painting Something on a Canvas
    ... > Ok What I need to do is make a applet called which has a Balloon in it ... > panels and I also need the Ballon to be displayed onto a Canvas. ... > public void actionPerformed{ ...
    (comp.lang.java.help)
  • Re: Genercis advice
    ... > a complete Canvas class. ... > class SubShape extends Shape { ... > public void drawAll() { ... > public void add{ ...
    (comp.lang.java.programmer)