user4964200
user4964200

Reputation: 39

custom JPanel not updating

the code:

import java.awt.BorderLayout;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class PaintWindow {

private JFrame frame;
private aJPanel panel;

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

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

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

    panel = new aJPanel();
    frame.getContentPane().add(panel, BorderLayout.CENTER);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel.stam();
        }
    });
    frame.getContentPane().add(btnNewButton, BorderLayout.NORTH);

    frame.setBounds(100, 100, 450, 300);
    frame.setVisible(true);
}

public class aJPanel extends JPanel {
    private static final long serialVersionUID = 8874943072526915834L;
    private Graphics g;

    public aJPanel() {
        super();
        System.out.println("Constructor");
    }

    public void paint(Graphics g) {
        super.paint(g);
        System.out.println("paint");
        g.fillRect(10, 10, 10, 10);
        this.g = g;

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("paintComponent");
        g.fillRect(20, 20, 20, 20);
        this.g = g;
    }

    public void stam() {
        System.out.println("stam");
        this.g.fillRect(40, 40, 40, 40);
        this.repaint();
    }
}
}

what I want to do is paint custom shapes (lines, rectangles etc) after the user clicks a button or triggers a mouse event. but calling aJPanel.stam() does not show the rectangle its supposed to. I am fairly new to JPanel. any suggestions?

Upvotes: 1

Views: 67

Answers (1)

camickr
camickr

Reputation: 324207

First of all:

  1. class names should start with a capital letter
  2. class names should be descriptive.

"aJPanel" does not follow either of the above.

but calling aJPanel.stam() does not show the rectangle its supposed to

See Custom Painting Apporoaches for the two common ways to do dynamic painting:

  1. Add objects to a List and iterate the List to paint all the objects
  2. Paint directly to a BufferedImage and then paint the BufferedImage

The examples add the Rectangle by dragging the mouse, but you can easily add Rectangles by just invoking the addRectangle(...) method when you click a button.

Upvotes: 1

Related Questions