ImageButton


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: ImageButton

  1. #1
    Nir Guest

    ImageButton


    Hi all,

    I have to use a awt.Button object within an applet, and I need to 'plant'
    an image on top of it. I know there's an ImageButton in Swing, but I can't
    use it since I'm restricted to 'NO DOWNLOADS' (which are needed of course
    by swing components).
    Does anyone has a solution ?

    Thanx in advance,
    Nir.

  2. #2
    Jacky Guest

    Re: ImageButton


    Try to build your own ImageButton class which can inheritance from Canvas(heavyweight)
    or Component(lightweight).
    You can overload your constructor like:
    class ImageButton extends Canvas implements MouseListener {

    ImageButton( String label, Image image)...
    ImageButton( Image image, String label )...
    ImageButton( String label )...
    ImageButton( Image image )...

    You should also overwirte your paint method.
    public void paint( Graphics g )
    {
    Dimension d = getSize();
    g.setColor( Color.gray );
    g.fillRect( 0, 0, d.width, d.height ) // background
    g.setColor( Color.light );
    g.drawline( 0, 0, d.width-1, 0 ); // upper border
    g.drawline( 0, 1, 0, d.height ); // left border
    g.setColor( Color.darkGray );
    ...... // draw another two lines as right and bottom borders
    // you can also draw more light to get thicker borders
    g.drawImage( image, /* x and y positions of image */, null );
    ...
    }

    You should also define the action for the ImageButton.
    void mouseClicked(MouseEvent event)...
    mousePressed....
    mouseEnter....
    ...etc.



    "Nir" <nir@effective-i.com> wrote:
    >
    >Hi all,
    >
    >I have to use a awt.Button object within an applet, and I need to 'plant'
    >an image on top of it. I know there's an ImageButton in Swing, but I can't
    >use it since I'm restricted to 'NO DOWNLOADS' (which are needed of course
    >by swing components).
    >Does anyone has a solution ?
    >
    >Thanx in advance,
    >Nir.



  3. #3
    Cup 'o' Decafe Guest

    Re: ImageButton


    Here you go:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.table.*;

    public class BackgroundTable
    {
    public static void main(String[] args) {
    JFrame frame = new JFrame("Button Background Image");
    frame.addWindowListener( new WindowAdapter() {
    public void windowClosing(WindowEvent e)
    {
    Window w = e.getWindow();
    w.setVisible(false);
    w.dispose();
    System.exit(0);
    }
    });

    JTable imTable = new JTable(35, 3) {
    public Component prepareRenderer(TableCellRenderer renderer, int
    row, int column)
    {
    Component c = super.prepareRenderer( renderer, row, column);

    // We want renderer component to be
    //transparent so background image is visible
    if( c instanceof JComponent )
    ((JComponent)c).setOpaque(false);
    return c;
    }

    public void paint( Graphics g )
    {
    ImageIcon image = new ImageIcon("Yourimage.gif");
    // tile the background image
    Dimension d = getSize();
    for( int x = 0; x < d.width; x += image.getIconWidth() )
    for( int y = 0; y < d.height; y += image.getIconHeight() )
    g.drawImage( image.getImage(), x, y, null, null );
    // Now let the paint do its usual work
    super.paint(g);
    }
    };

    //make the table transparent
    imTable.setOpaque(false);

    JScrollPane jsp = new JScrollPane(imTable);
    frame.getContentPane().add(jsp);

    frame.pack();
    frame.show();
    }
    }
    "Nir" <nir@effective-i.com> wrote:
    >
    >Hi all,
    >
    >I have to use a awt.Button object within an applet, and I need to 'plant'
    >an image on top of it. I know there's an ImageButton in Swing, but I can't
    >use it since I'm restricted to 'NO DOWNLOADS' (which are needed of course
    >by swing components).
    >Does anyone has a solution ?
    >
    >Thanx in advance,
    >Nir.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center