0

Hello I am new to JAVA and recently studied graphics but got stuck on how to draw a circle (I learn on my own through Google) I would be happy if you help me with the following lines of code (Do not refer to the background sub button)

public class Panel_ {
    static JPanel panel1 = new JPanel();

    public static Color randomColor() {
        int r = (int) Math.round(Math.random() * 255 - 1);
        int g = (int) Math.round(Math.random() * 255 - 1);
        int b = (int) Math.round(Math.random() * 255 - 1);
        Color R_col = new Color(r, g, b);
        System.out.println(R_col);
        return R_col;
    }

    public static void Panel() {

        var color_changer = new JButton("To change color");

        color_changer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel1.setBackground(randomColor());
            }
        });

        var Panel = new JPanel();
        Panel.add(color_changer);
        Panel.setBackground(Color.blue);

        panel1.setBackground(Color.black);
        panel1.setPreferredSize(new Dimension(400, 430));

        var frame = new JFrame("Color changer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel1, BorderLayout.NORTH);
        frame.getContentPane().add(Panel, BorderLayout.SOUTH);
        frame.pack();
        frame.setSize(500, 500);
        frame.setBackground(Color.blue);
        frame.setVisible(true);
    }

    public void paintCircle(Graphics g) {
        g.setColor(Color.blue);
        g.fillOval(60, 80, 100, 100);

    }

    public static void main(String args[]) {
        Panel();
    }
}
  • https://stackoverflow.com/questions/1836440/how-to-draw-circle-on-jpanel-java-2d See if this helps – Vishal Jul 25 '21 at 09:34
  • 1
    *I learn on my own through Google* - Start with the [Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for Swing basics. (not Google). In particualar the section on `Custom Painting` will give examples and explanations. – camickr Jul 25 '21 at 14:55

1 Answers1

1

Use paintComponent for panel1 which I changed the name to topPanel in the runnable code below:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class Panel extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel topPanel;
    private JPanel panel;
    private JButton color_changer;
    
    
    public Panel() {
        initializeForm();
    }
    
    private void initializeForm() {
        setAlwaysOnTop(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.blue);
        
        color_changer = new JButton("To change color");
        color_changer.addActionListener((ActionEvent e) -> {
            topPanel.setBackground(randomColor());
        });

        topPanel = new JPanel() {
            private static final long serialVersionUID = 1L;
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.blue);
                g.fillOval(60, 80, 100, 100);
            };
        };
        
        panel = new JPanel();
        panel.add(color_changer);
        panel.setBackground(Color.blue);

        topPanel.setBackground(Color.black);
        topPanel.setOpaque(true);
        topPanel.setPreferredSize(new Dimension(400, 430));
        
        getContentPane().add(topPanel, BorderLayout.NORTH);
        getContentPane().add(panel, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(null);
    }
    
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(() -> {
            new Panel().setVisible(true);
        });
    }
    
    public static Color randomColor() {
        int r = (int) Math.round(Math.random() * 255 - 1);
        int g = (int) Math.round(Math.random() * 255 - 1);
        int b = (int) Math.round(Math.random() * 255 - 1);
        Color R_col = new Color(r, g, b);
        System.out.println(R_col);
        return R_col;
    }
    
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • 2
    You should also be overriding the `getPreferredSize()` method when doing custom painting. Every Swing component should be able to determine its own preferred size. I provided a link to the Swing tutorial in my comment to the question for more detailed working examples. Also, don't call your class "Panel". There is an AWT component with that name which causes confusion. Use a more descriptive class name. You should not extend JFrame. You only extend a class when adding functionality. Adding components to a frame is not adding functionality. – camickr Jul 25 '21 at 14:58