Solved

Creating an image button...

Posted on 1998-02-17
10
169 Views
Last Modified: 2010-04-16
I want to create an image button.  I have already created an ImageCanvas class which extends from java.awt.Canvas.  Now I want to add this canvas to a Button.  How can I do this?  Any help or examples would be great.  Thanks
0
Comment
Question by:beano
  • 7
  • 3
10 Comments
 

Author Comment

by:beano
ID: 1233098
This is the code I have so far.  I creates an image button, but the size of the button is distorted, and not all of the image is displayed.  The button is very skinny and very tall.  Here is the code:

import java.awt.*;
import java.io.*;
import user.bgentile.gui.*;

public class ImageButton extends Button
{
      private Image image;

      public ImageButton(String imagefile)
      {
            super();
            loadImage(readImage(imagefile));
      }

      public void paint(Graphics g)
      {
            g.drawImage(image,0, 0, this);
      }

      public Image readImage(String filename)
      {
            Image image = null;
            try
            {
                      FileInputStream file = new FileInputStream(filename);
                  byte[] bytes = new byte[file.available()];
                      file.read(bytes);
                      image = Toolkit.getDefaultToolkit().createImage(bytes);
                      loadImage(image);
            }
            catch(IOException e)
            {
                  System.out.println("Error reading " + filename + " - " + e);
            }
            return image;
      }

      private void loadImage(Image image)
      {
            if (image != null)
            {
                      this.image = image;
                      MediaTracker tracker = new MediaTracker(this);
                      tracker.addImage(image, 0);
                      try
                  {
                          tracker.waitForID(0);
                  }
                  catch(InterruptedException e)
                  {
                          System.err.println("Cannot load image - " + e);
                      }
                      setSize(image.getWidth(this), image.getHeight(this));
            } else
            {
                  System.out.println("Error loading image - image is null");
            }
      }
}
0
 
LVL 5

Accepted Solution

by:
fontaine earned 70 total points
ID: 1233099
You have to override the getPreferredSize method of java.awt.Component, from which a Button inherits, in order to inform the layout manager of the size of the button. If I drop the border around the image:

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


0
 

Author Comment

by:beano
ID: 1233100
I added this code, and it works a little better, but the height of the button is still the height of the entire frame.  How do I correct this?  Also what if I wanted the image to be a little smaller than the button size, what do I do?
0
 

Author Comment

by:beano
ID: 1233101
I corrected the sizing problem, but I still do not know how to make the image a little smaller than the button.  What I want is to actually have a little border around the entire image.  The border would just be the button's background.  So I want the image to be a little smaller than the button.  How can I do this?
0
 

Author Comment

by:beano
ID: 1233102
I have corrected these sizing problems.  But how can I add a label to the button also?  I also would like to be able to position the label either on top, bottom, left, or right of the image.  How can I do this?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:fontaine
ID: 1233103
Sorry for late reply, I was unable to login! If you want a border around the image, or a label, you have to draw it in the paint method.

Ex:

public void paint(Graphics g)
{
 g.drawRect(0,0,image.getWidth(this)+4,image.getHeight(this)+4);
 g.drawImage(image,2, 2, this);
 g.drawString("Beano",2,image.getHeight(this)/2);
}
0
 

Author Comment

by:beano
ID: 1233104
What if I want the label centered and on the bottom, or top of the image?  Right now is draws the label on the image.
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1233105
drawString(String txt,int x, int y)

x and y are the coordinates for the base line of the String. This gives you full control on how the text is drawn...

http://java.sun.com/docs/books/tutorial/ui/drawing/drawingText.html
-------------------------------------------

I just found an article for you:

http://www.javaworld.com/javaworld/jw-03-1998/jw-03-howto.html

The author is explaining how to build an image button! :-)
0
 

Author Comment

by:beano
ID: 1233106
He is explaining how to build a circular button.  I am just having trouble determining how to draw the label centered and either on top or on bottom of the image.  Also for some odd reason, sometimes when I click on the button, the images move off of the button and onto other parts of the screen.  What the heck is going on there?
0
 

Author Comment

by:beano
ID: 1233107
Also, what if I just wanted to draw text and not an image.  But the text is multi-line.  So I want a button that would look like:

---------
| test  |
| test2 |
---------


0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now