Solved

Creating an image button...

Posted on 1998-02-17
10
174 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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
 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

773 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