Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating an image button...

Posted on 1998-02-17
10
Medium Priority
?
179 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

972 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