0

My question is how to display buttons on top of a circle which created with Graphics. My code is :

public class Grafik extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Grafik frame = new Grafik();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public Grafik() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 784, 419);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("Click");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fire();
            }
        });

        btnNewButton.setBounds(64, 73, 32, 32);
        contentPane.add(btnNewButton);
    }

    public void fire()
    {
        JPanel panel = new JPanel(){
            @Override
            public void paint(Graphics g)
            {
                g.setColor(Color.black);
                g.drawOval(20, 20, 400, 400);
                g.fillOval(20,20,400,400);

                g.setColor(Color.white);
                g.drawOval(60, 60, 300, 300);
                g.fillOval(60,60,300,300);



            }
        };

        panel.setBounds(10, 27, 383, 233);
        setContentPane(panel);

        JButton btn = new JButton();
        btn.setIcon(new ImageIcon("C:\\Users\\nuria\\Desktop\\icon1.png"));
        btn.setBounds(75, 76, 32, 32);
        panel.add(btn);

    }   
}

This code is a little part of my project. I have a lot of panels in my project and my fire method isn't called firstly. I mean the fire method is called by sixth panel. I have to draw two circles with buttons. These buttons should be between two circles. But i cant display buttons directly. When i hover the buttons, they show. I want to show buttons instantly when the sixth panel opens. I have two buttons above. When the first button is clicked, another panel should open and show circles(which created with Graphics) and another button(with a icon) between them.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 06 '17 at 12:41

1 Answers1

1
    JPanel panel = new JPanel(){
        @Override
        public void paint(Graphics g)
        {

Don't override paint(). Custom painting is done by overriding paintComponent() and don't forget to invoke super.paintComponent() as the first statement.

The problem with your current code is that by overriding paint you have changed the default painting logic of the panel and the child components are never painted.

Take a look at the section from the Swing tutorial on Custom Painting for more information and examples, especially the section on A Closer Look at the Paint Mechanism.

camickr
  • 321,443
  • 19
  • 166
  • 288