?
Solved

Creating an image button...

Posted on 1998-02-17
10
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 140 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

719 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