Results 1 to 11 of 11
- 08-20-2011, 11:58 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
JButton States Set to Pictures From Image Editor
I am aiming to design a circular JButton that has its states set to images that I create in an Image Editor. I have been told that by creating a JButton and removing its default borders, the button's shape will be determined by its icon. I decided to try this out and designed a simple program to do so. The image Redinner.gif is a simple image I created on an image editor, a 50 by 50 pixel red circle.
public static void main(String[] args) {
ImageIcon myIcon = new ImageIcon("images/Redinner.gif");
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
JButton test = new JButton(myIcon);
test.setBorderPainted(false);
test.setFocusPainted(false);
panel.add(test);
Dimension dim = new Dimension(80, 80);
test.setPreferredSize(dim);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE );
}
I am wondering if the path I used, images/Redinner.gif, images being the folder it is in, contains the problem. There was no error, however the frame came up blank. Moreover, the image editor only allows me to build off of a rectangular window. Thus, I don't understand the point of setting the shape of a JButton to an image as it will only be rectangular, and is juts a more complex way of adjusting the size of a JButton. Thank you very much for your time.
-
You need a valid link and the ImageIcon would require a URL. If you use a String, Java will assume that you man a file name (and how could it know any different). If you want to use a web address, you'll need to wrap it in a URL.
-
You also will want to post only compilable runnable well formatted code with code tags. For e.g.,
Java Code:import java.awt.*; import java.net.*; import javax.swing.*; public class FooSwing01 { public static final String IMAGE_1 = "http://duke.kenai.com/iconSized/duke4.gif"; public static final String IMAGE_2 = "http://www.java-forums.org/images/Redinner.gif"; public static void main(String[] args) throws MalformedURLException { ImageIcon myIcon = new ImageIcon(new URL(IMAGE_1)); JFrame frame = new JFrame(); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Paint paint = new GradientPaint(0, 0, Color.blue, 30, 30, Color.red, true); Graphics2D g2 = (Graphics2D)g; g2.setPaint(paint); g2.fillRect(0, 0, getWidth(), getHeight()); } }; frame.add(panel); JButton test = new JButton(myIcon); test.setBorderPainted(false); test.setFocusPainted(false); test.setOpaque(false); test.setBackground(new Color(0, 0, 0, 0)); panel.add(test); Dimension dim = new Dimension(80, 80); test.setPreferredSize(dim); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); } }
- 08-21-2011, 12:58 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
Sorry if I wasn't clear, I was trying to load a file name.
- 08-21-2011, 01:38 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
And also, why is it that the jbutton is no longer opaque after it is pressed?
- 08-21-2011, 02:51 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
If the image contains transparent pixels outside of the round image then the button will appear round. You can also make it react only to mouse clicks in the round area of the image by overriding the contains() method of the JButton:I don't understand the point of setting the shape of a JButton to an image as it will only be rectangular,
Java Code:// Hit detection. Shape shape; public boolean contains(int x, int y) { // If the button has changed size, make a new shape object. if (shape == null || !shape.getBounds().equals(getBounds())) { shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); } return shape.contains(x, y); }
- 08-21-2011, 03:03 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
I still am unable to take an image file and use it as an ImageIcon.
- 08-21-2011, 03:13 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Perhaps you are not looking in the right place for your image:
[Edit] "Redinner.gif" should be preceeded by the images directory both times.Java Code:[color=green]System.out.println("Looking for image in " + new File("Redinner.gif").getAbsolutePath());[/color] ImageIcon myIcon = new ImageIcon("Redinner.gif"); [color=green]System.out.println("Found image " + myIcon.getImage());[/color] // etc
Fubarable's suggestion (compilable, runnable, formatted code) is good - and I'll add brief. This gives everyone more to work on than "I am still unable...".Last edited by pbrockway2; 08-21-2011 at 03:19 AM.
- 08-21-2011, 03:35 AM #9
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
what do i use the absolute path for. do i do..
ImageIcon myIcon = new ImageIcon(myabsolutepath/Redinner.gif);
- 08-21-2011, 04:24 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
You print the path in order to verify that you are looking for the image in the same place that the image file exists.
- 08-21-2011, 04:33 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
The Funny Pictures Thread(this means a lot of pictures...)
By joshua in forum EntertainmentReplies: 22Last Post: 07-25-2012, 09:51 PM -
image on a JButton...how??
By Habib in forum AWT / SwingReplies: 2Last Post: 02-27-2011, 06:56 PM -
select and load image onto editor pane
By vikram.jamakhandi in forum AWT / SwingReplies: 0Last Post: 03-03-2010, 11:44 AM -
Java image editor
By jokerbla in forum New To JavaReplies: 0Last Post: 04-11-2009, 04:57 PM -
adding Image to JButton
By mayhewj7 in forum New To JavaReplies: 3Last Post: 03-31-2009, 03:39 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks