Solved

Creating an image button...

Posted on 1998-02-17
10
177 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 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

630 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