Quadrition
Quadrition

Reputation: 173

Java Doesn't draw on JPanel

This is my main class:

public class Sad {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sad window = new Sad();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Sad() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 512, 399);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, "name_12361565901507");
        panel.setLayout(null);

        JButton btnNes = new JButton("Nes");
        btnNes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                Grafik grafik = new Grafik(20, 20, 100);
                panel.add(grafik);
            }
        });
        btnNes.setBounds(90, 146, 89, 23);
        panel.add(btnNes);
    }


}

And this is drawing class

public class Grafik extends JPanel{

    private int x;
    private int y;
    private int r;

    public Grafik(int x, int y, int r){
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 =(Graphics2D) g;

        Ellipse2D circ = new Ellipse2D.Float(x, y, r, r);
        g2.setColor(Color.RED);
        g2.draw(circ);


        }

}

They are in same package. And when i click button its suposed to draw Ellipse in red color, but it doesn't show anything. Can someone help me? BTW Sorry for bad english

Upvotes: 1

Views: 431

Answers (1)

Lukas Rotter
Lukas Rotter

Reputation: 4188

It's because you don't call panel.setBounds(), revalidate() and repaint().

  • But you shouldn't use a null layout anyway: Use layout managers.
  • You should call super.paintComponent(g) at the beginning of the paintComponent method.
  • Rather than adding a new component to the panel after every button press you might want to just toggle a boolean value inside the Grafik instance which determines wheter the ellipse should be visible or not.
  • If you want the ellipse to be "smooth", you can call g2.setRenderingHint(hintKey, hintValue).

Modified code including my suggestions:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Sad {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sad window = new Sad();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Sad() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 512, 399);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Grafik grafik = new Grafik(20, 20, 100);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(grafik);

        JButton btnNes = new JButton("Nes");
        btnNes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                grafik.setEllipseVisible(true);
                panel.repaint();
            }
        });

        JPanel btnPanel = new JPanel();
        btnPanel.add(btnNes);
        panel.add(btnPanel, BorderLayout.SOUTH);

        frame.setContentPane(panel);
    }

}

class Grafik extends JPanel {

    private int x;
    private int y;
    private int r;
    private boolean ellipseVisible;

    public Grafik(int x, int y, int r) {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isEllipseVisible()) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            Ellipse2D circ = new Ellipse2D.Float(x, y, r, r);
            g2.setColor(Color.RED);
            g2.draw(circ);
        }
    }

    public boolean isEllipseVisible() {
        return ellipseVisible;
    }

    public void setEllipseVisible(boolean ellipseVisible) {
        this.ellipseVisible = ellipseVisible;
    }

}

Upvotes: 3

Related Questions