Java Forum / GUI / January 2006
Essentially skinning my app..
jackroofman@gmail.com - 25 Jan 2006 02:01 GMT I'm designing an MP3 player program, and I'm to the point that I want a better GUI. Using Images and Graphics, I've been able to draw what look like buttons on my JDialog. However, I can't add a MouseListener to the Images. A friend suggested putting the Images into a JComponent, but I can't manage that either.
I'm sure the solution is obvious, but it continues to escape me. I need some way to add a MouseListener to my Image, even if I have to get a little creative and put the Image in something else.
Thanks in advance for the help.
Roedy Green - 25 Jan 2006 02:18 GMT >I'm sure the solution is obvious, but it continues to escape me. I need >some way to add a MouseListener to my Image, even if I have to get a >little creative and put the Image in something else. for a component to hold your Image see ImageViewer or MagnifyingImageViewer
See http://mindprod.com/products1.html#COMMON11 for source.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
jackroofman@gmail.com - 25 Jan 2006 04:17 GMT 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.
IchBin - 25 Jan 2006 05:47 GMT > 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 [quoted text clipped - 14 lines] > > but I can't actually get anything to come of it. All you need to do is add a MouseListener to the component in which you are drawing the image.
If you're drawing the image in the applet itself, just add a MouseListener to the applet. If you're using a Canvas, add a mouse listener to the canvas.
For example:
public class ImageCanvas extends Canvas implements MouseListener {
public ImageCanvas() { addMouseListener(this); }
public void paint(Graphics g) { // draw image }
public void mousePressed(MouseEvent e) { // handle mouse being pressed }
public void mouseReleased(MouseEvent e) { // handle mouse being released }
// other methods in MouseListener
 Signature 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-)
IchBin - 25 Jan 2006 05:48 GMT > 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 [quoted text clipped - 14 lines] > > 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(); } } }
 Signature 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-)
Roedy Green - 25 Jan 2006 06:57 GMT >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: Look at some code that uses it such as http://mindprod.com/products1.html#BIO
fro ResizingImageViewer see http://mindprod.com/products1.html#NETWORKCAM for sample code that uses it.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Fred Kleinschmidt - 25 Jan 2006 18:33 GMT > I'm designing an MP3 player program, and I'm to the point that I want a > better GUI. Using Images and Graphics, I've been able to draw what look [quoted text clipped - 7 lines] > > Thanks in advance for the help. You don't explain what you are trying to do with this things that "looks like buttons". What's wrong with actually using a JButton?
 Signature Fred L. Kleinschmidt Boeing Associate Technical Fellow Technical Architect, Software Reuse Project
zero - 25 Jan 2006 19:17 GMT >> I'm designing an MP3 player program, and I'm to the point that I want a >> better GUI. Using Images and Graphics, I've been able to draw what look [quoted text clipped - 10 lines] > You don't explain what you are trying to do with this things that "looks > like buttons". What's wrong with actually using a JButton? JButton should indeed be fine for most uses - including "skinning". You can make the JButton look like anything you want.
For example:
JButton button = new JButton(); button.setIcon(new ImageIcon("button.png")); button.setMargin(new Insets(0,0,0,0)); button.setIconTextGap(0); button.setBorderPainted(false); button.setBorder(null); button.setText(null); button.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null)); button.setPressedIcon(new ImageIcon("btndown.png")) button.setDisabledIcon(new ImageIcon("btndisabled.png")); // ...
Wrap all this in a nice helper method/class, and you've got a button that looks like a dancing clown that jumps when you click it. No need to mess with MouseListeners or whatever.
Oliver Wong - 25 Jan 2006 19:39 GMT > JButton should indeed be fine for most uses - including "skinning". You > can make the JButton look like anything you want. [quoted text clipped - 17 lines] > looks like a dancing clown that jumps when you click it. No need to mess > with MouseListeners or whatever. Would this code correct support PNG's alpha transparency? (e.g. semi-transparent buttons) I assume it would, because Sun's library supports PNG's alpha transparency elsewhere, but I don't have an image editing program handy to test this out on this machine.
- Oliver
zero - 25 Jan 2006 20:01 GMT > Would this code correct support PNG's alpha transparency? (e.g. > semi-transparent buttons) I assume it would, because Sun's library > supports PNG's alpha transparency elsewhere, but I don't have an image > editing program handy to test this out on this machine. > > - Oliver I'm 90% sure it would. And if it doesn't, using a GIF image should work fine. I have not tested with transparent images though.
Oliver Wong - 25 Jan 2006 20:38 GMT >> Would this code correct support PNG's alpha transparency? (e.g. >> semi-transparent buttons) I assume it would, because Sun's library [quoted text clipped - 5 lines] > I'm 90% sure it would. And if it doesn't, using a GIF image should work > fine. I have not tested with transparent images though. Yes, but GIF is proprietary, limited to 8 bit colour depth, and 1 bit transparency. Boo GIF, yay PNG.
- Oliver
Roedy Green - 25 Jan 2006 21:31 GMT >I'm 90% sure it would. And if it doesn't, using a GIF image should work >fine. I have not tested with transparent images though. see http://mindprod.com/applets/masker.html and http://mindprod.com/jgloss/png.html for sample images. All the icons on my site are PNG.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
jackroofman@gmail.com - 26 Jan 2006 04:33 GMT I've managed to work the problem out through a combination of the various techniques on here. The reason I didn't just use JButton.setIcon() is because I use one image file to hold 4 different states of the button: normal, moused over, depressed, and depressed and mousedout. This require cropping of the image, and the method I found for that requires the use of an Image, not an ImageIcon. Similarly, I couldn't find a way to set all 4 of those states, and the use of a JButton just doesn't give the same look as the custom buttons. And, finally, it was a really good learning experience.
Thanks for all the help.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|