1

I have an existing graphics object, and I'm attempting to add a JRadioButton on top of it. Once the program is run, the button does not show up, and I think it's because there isn't a way to add a JPanel to a Graphics object. I add the JRadiobutton to its appropriate ButtonGroup and then I add the button to a JPanel, but I haven't seen any way to add a button on top of my graphics object.

Is there a way to add a radio button to a graphics object? It's important that I continue using this graphics object. Let me know if seeing the code will help, I think I just need a better way to approach this.

private void redrawTitle(Graphics gc) {
    gc.setColor(Color.yellow);
    gc.fillRect(0, 0, view_width, view_height);
    gc.setFont(largeBannerFont);
    FontMetrics fm = gc.getFontMetrics();
    gc.setColor(Color.red);
    centerString(gc, fm, "Start", 100);
    gc.setColor(Color.blue);
    gc.setFont(smallBannerFont);
    fm = gc.getFontMetrics();
    centerString(gc, fm, "by DavidVee", 160);
    centerString(gc, fm, "a;lskdf", 190);
    gc.setColor(Color.black);
    centerString(gc, fm, "To start, select a skill level.", 250);

    JRadioButton kruskalButton = new JRadioButton("Kruskal");
    ButtonGroup group = new ButtonGroup();
    group.add(kruskalButton);
    JPanel panel = new JPanel();
    panel.add(kruskalButton);

    centerString(gc, fm, "(Press a number from 0 to 9,", 300);
    centerString(gc, fm, "or a letter from A to F)", 320);
    centerString(gc, fm, "v1.2", 350);
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
davidVee
  • 61
  • 3
  • 13

1 Answers1

1

This method will "paint" the component onto the supplied graphics context, it is nothing more the "rubber stamp"/"snap shot" of the component, no interaction will be possible (without you coding for it yourself)...

public class PaintControls {

    public static void main(String[] args) {
        new PaintControls();
    }

    public PaintControls() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private JRadioButton radioButton = new JRadioButton("I'm painted...");

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

            Dimension dim = radioButton.getPreferredSize();
            int x = (getWidth() - dim.width) / 2;
            int y = (getHeight() - dim.height) / 2;

            radioButton.setBounds(x, y, dim.width, dim.height);

            g2d.translate(x, y);
            radioButton.printAll(g);
            g2d.translate(-x, -y);

            g2d.dispose();
        }
    }
}

To add new content to the parent pane, use the "add" method of the container, BUT, DO NOT do this within the paintXxx methods...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you. This creates a button that is in a seperate window. Is there a way to create a button that is actually a part of the screen created by graphics? Or is that just not how graphics is used? – davidVee Oct 15 '12 at 03:16
  • A graphics context is created only for each top level container. What are you trying to do? – MadProgrammer Oct 15 '12 at 03:28
  • I'm trying to add a button(preferably a radio button) in a screen that is being generated by a graphics object. – davidVee Oct 15 '12 at 03:31
  • So, take the code from the `paintComponent` I've shown and use it within your code to render it. The trick is, you need to tell the component what size it should be and where it needs to be rendered. – MadProgrammer Oct 15 '12 at 03:37
  • Sorry, I'm pretty new to Java and GUI's. I'm not sure how to go about using this code. I tried using your paintComponent method within my code(after putting the entire method into the same class that my redrawTitle() method is in), but it says that "paintComponent is undefined for type Graphics." – davidVee Oct 15 '12 at 15:17