1

I'm sure this is a simple answer, but I can't figure it out.

I am trying to make a basic shape that I can control in a window. Obviously it will be more involved when the whole project is complete, but I am working on the early steps still. I am using WindowBuilder to make the layout, and have a JPanel and a JButton. The JPanel draws a rectangle, and has a method to move it. The JButton calles that moving command. And that's it. The problem is in the repaint. The shape keeps all old versions of itself, and the button makes weird copies of itself. All these go away when I resize the window, which I thought was the same as calling repaint. Again, I'm sure is something simple I am missing. Below are my 2 classes.

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class Drawing {

private JFrame frame;

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

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

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    drawpanel panel = new drawpanel();
    panel.setBounds(58, 68, 318, 182);
    frame.getContentPane().add(panel);

    JButton btnMove = new JButton("move");
    btnMove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            panel.moves();
        }
    });
    btnMove.setBounds(169, 34, 89, 23);
    frame.getContentPane().add(btnMove);

}
}

^ This one, besides the buttonListener, was autocreated by WindowBuilder.

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class drawpanel extends JPanel {

    int x = 50, y = 50;
    int sizeX = 50, sizeY = 50;

    public void paintComponent( Graphics g) {
        super.paintComponents(g);

        g.setColor(Color.BLACK);
        g.drawRect(x, y, sizeX, sizeY);
    }

    public void moves() {
        x +=5;
        repaint();
    }
}

^ This one has my drawing of the shape and the moving/repainting method. It was written mostly from other examples I found on this site.

Thanks to any help in advanced.

  • `frame.getContentPane().setLayout(null);` 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). To work optimally, `drawpanel` needs to override `getPreferredSize()`, then `pack()` the frame. – Andrew Thompson Nov 10 '15 at 08:34
  • Thanks for the advice. As I continue working on this project, I intended to use a layout manager. For these early steps, I just wanted the functionality. But as I am new to this GUI stuff, any help I can get is appreciated. – Aaron Burton Nov 10 '15 at 15:17

1 Answers1

4
public void paintComponent(Graphics g) {
    super.paintComponents(g); // wrong method! (Should not be PLURAL)

Should be:

public void paintComponent(Graphics g) {
    super.paintComponent(g); // correct method!
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433