• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with an Image Button

 
Bharat Agarwal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a student and want to create an image button in my applet so that it can take up the events. Can some one help me out please. The example code would be a best help.
Thanx a lot.
Bharat
 
Meghna ks
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code .. I hope this helps .. You may have to do some modifications

import java.awt.*;
import symantec.itools.multimedia.ImageViewer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import symantec.itools.awt.ImagePanel;
import java.net.URL;
//Modifications given by Ravi inorder to work on Browser

public class ImageButton extends javax.swing.JComponent
{
OSSIBRenderInfo IBBeanInfo = new OSSIBRenderInfo();
Vector panes = new Vector();
String currSelection = "";
URL codeBase;
protected URL getURL(String filename) {
//URL codeBase = getCodeBase();
URL url = null;
try {
url = new URL(codeBase, filename);
} catch (java.net.MalformedURLException e) {
System.err.println("Couldn't create image: " + filename +
" :badly specified URL");
return null;
}

return url;
}
public ImageButton()
{
setSize(243,83);

try {
imageViewer1.setStyle(symantec.itools.multimedia.ImageViewer.IMAGE_SCALED_TO_FIT);
}
catch(java.beans.PropertyVetoException e) { }
add(imageViewer1);
imageViewer1.setBounds(36,24,183,38);
//}}

//{{REGISTER_LISTENERS
SymMouse aSymMouse = new SymMouse();
imageViewer1.addMouseListener(aSymMouse);
//}}
}
//{{DECLARE_CONTROLS
symantec.itools.multimedia.ImageViewer imageViewer1 = new symantec.itools.multimedia.ImageViewer();
//}}
public static void main(String argv[])
{
class DriverFrame extends javax.swing.JFrame
{
public DriverFrame()
{
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent event)
{
dispose(); // free the system resources
System.exit(0); // close the application
}
});
getContentPane().setLayout(null);
setSize(400,300);
getContentPane().add(new ImageButton());
}
}
new DriverFrame().show();
}
// Select two states of Bitmaps. i.e for mouseon and mouseoff
String onBitmap = "";
String offBitmap = "";

class SymMouse extends java.awt.event.MouseAdapter
{
public void mouseExited(java.awt.event.MouseEvent event)
{
Object object = event.getSource();
if (object == imageViewer1)
imageViewer1_mouseExited(event);
}
public void mouseEntered(java.awt.event.MouseEvent event)
{
Object object = event.getSource();
if (object == imageViewer1)
imageViewer1_mouseEntered(event);
}
public void mouseClicked(java.awt.event.MouseEvent event)
{
Object object = event.getSource();
if (object == imageViewer1)
//imageViewer1_mouseClicked(event);
pane_mouseClicked(event);

}
}
void pane_mouseClicked(java.awt.event.MouseEvent event){

if ( event.getClickCount() == 1 ) {

Object object = event.getSource();

//System.out.println("ImageButton Click");

java.awt.event.MouseEvent event1 = new java.awt.event.MouseEvent(imageViewer1.getParent(),
event.getID(), event.getWhen(), event.getModifiers(), event.getX(),event.getY(), event.getClickCount(), event.isPopupTrigger());
(imageViewer1.getParent()).dispatchEvent(event1);

//Resetting cursor to normal
setCursor(new Cursor( Cursor.WAIT_CURSOR));

}
/*
Object object = event.getSource();

for ( int i = 0; i < panes.size(); i++ ){

if ( object == (Object)panes.elementAt(i)) {

symantec.itools.awt.ImagePanel Button = (symantec.itools.awt.ImagePanel)panes.elementAt(i);

//OSSCompId comp = (OSSCompId)eqBeanInfo.components.elementAt(i);

System.out.println("Sending event from pane to Applet...");

//currSelection = Integer.toString(comp.CompId);

System.out.println(currSelection);

java.awt.event.MouseEvent event1 = new java.awt.event.MouseEvent(Button.getParent().getParent().getParent(),
event.getID(), event.getWhen(), event.getModifiers(), event.getX(),event.getY(), event.getClickCount(), event.isPopupTrigger());
(Button.getParent().getParent()).dispatchEvent(event1);
}

}

}
*/
}

void imageViewer1_mouseEntered(java.awt.event.MouseEvent event)
{

try {
imageViewer1.setImageURL(getURL("Images/" + onBitmap));
}
catch(java.beans.PropertyVetoException e) { }
try {
imageViewer1.setStyle(symantec.itools.multimedia.ImageViewer.IMAGE_SCALED_TO_FIT);
}
catch(java.beans.PropertyVetoException e) { }
setCursor(new Cursor( Cursor.HAND_CURSOR) );

}
public void imageViewer1_mouseExited(java.awt.event.MouseEvent event)
{
// System.out.println("ImageButton mouseExited");
try {
imageViewer1.setImageURL(getURL("Images/" + offBitmap));
}
catch(java.beans.PropertyVetoException e) { }
try {
imageViewer1.setStyle(symantec.itools.multimedia.ImageViewer.IMAGE_SCALED_TO_FIT);
}
catch(java.beans.PropertyVetoException e) { }
setCursor(new Cursor( Cursor.HAND_CURSOR) );

}


//Resizing the Bean
public void setBounds(int x, int y, int w, int h)
{
System.out.println("setBounds......." +
"x = " + Integer.toString(x) +
"y = " + Integer.toString(y) +
"w = " + Integer.toString(w) +
"h = " + Integer.toString(h)
);
imageViewer1.setBounds(0, 0, w, h);
super.setBounds(x, y, w, h);
}

// User methods
public void setOnBitmap(String bitmap)
{
onBitmap = bitmap;
}
public void setOffBitmap(String bitmap)
{
offBitmap = bitmap;
}

public void initialize(Object param1, Object param2)
{
codeBase = (URL)param1;
}

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!