Hi..
I would like to know how to set a radio button to a specific point on the panel correctly..
So what i'm trying to do is to load an image as background image (a simple map), and put radio buttons on it..
The result would roughly looks like the image i attached.. (i give a red line border because it's quite hard to see the radio buttons)
map.jpg
This is what i've done so far..
(mostly taken from internet)
The first code is to load an image into the panel..
and the second code is to create radio buttons on the location i prefer..import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; import java.applet.Applet; /** * This class demonstrates how to load an Image from an external file */ public class LoadImageApp extends Component { BufferedImage img; static radioButtonGenerator radButtonGen = new radioButtonGenerator(); public void paint(Graphics g) { g.drawImage(img, 0, 0, 757, 392, this); // draw the image } public LoadImageApp() { try { img = ImageIO.read(new File("Singapore.png")); } catch (IOException e) { } } public Dimension getPreferredSize() { if (img == null) { return new Dimension(100,100); } else { //return new Dimension(img.getWidth(null), img.getHeight(null)); //return new Dimension(800, 600); return new Dimension(757, 392); } } public static void main(String[] args) { JFrame f = new JFrame("Load Image Sample"); radButtonGen.init(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(new LoadImageApp()); f.add("Center", radButtonGen); f.pack(); f.setVisible(true); } }
this code is generated by a program called javagui, where i just need to drag and drop the radio button and generate the code out of it..
import java.awt.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.*; public class radioButtonGenerator extends Applet { ButtonGroup cbg; JRadioButton radio_1; JRadioButton radio_2; JRadioButton radio_3; JRadioButton radio_4; public void init() { frameTestLayout customLayout = new frameTestLayout(); setFont(new Font("Helvetica", Font.PLAIN, 12)); setLayout(customLayout); cbg = new ButtonGroup(); radio_1 = new JRadioButton("radio_1", false); cbg.add(radio_1); add(radio_1); radio_2 = new JRadioButton("radio_2", false); cbg.add(radio_2); add(radio_2); radio_3 = new JRadioButton("radio_3", false); cbg.add(radio_3); add(radio_3); radio_4 = new JRadioButton("radio_4", false); cbg.add(radio_4); add(radio_4); setSize(getPreferredSize()); } /* public static void main(String args[]) { radioButtonGenerator applet = new radioButtonGenerator(); Frame window = new Frame("frameTest"); window.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); applet.init(); window.add("Center", applet); window.pack(); window.setVisible(true); } */ } class frameTestLayout implements LayoutManager { public frameTestLayout() { } public void addLayoutComponent(String name, Component comp) { } public void removeLayoutComponent(Component comp) { } public Dimension preferredLayoutSize(Container parent) { Dimension dim = new Dimension(0, 0); Insets insets = parent.getInsets(); //dim.width = 320 + insets.left + insets.right; //dim.height = 240 + insets.top + insets.bottom; dim.width = 757 + insets.left + insets.right; dim.height = 392 + insets.top + insets.bottom; return dim; } public Dimension minimumLayoutSize(Container parent) { Dimension dim = new Dimension(0, 0); return dim; } public void layoutContainer(Container parent) { Insets insets = parent.getInsets(); Component c; c = parent.getComponent(0); if (c.isVisible()) {c.setBounds(insets.left+0,insets.top+0,16,16);} c = parent.getComponent(1); if (c.isVisible()) {c.setBounds(insets.left+0,insets.top+208,16,16);} c = parent.getComponent(2); if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+0,16,16);} c = parent.getComponent(3); if (c.isVisible()) {c.setBounds(insets.left+296,insets.top+216,16,16);} } }
but it give me this result instead..
result.jpg
i wonder what is wrong with the code?
thank you..