Moving a square on Y-axis in JPanel

Viewed 178

I was trying to move a square on Y-axis with the help of buttons up "upp" and down "ner". But I am not really good at JFrame functions so I need help.

Edit: Fix the issue by defining Y with the second values of component g and increase or lower it by 5 in the buttons action performed and repaint(); them

package MoveSQ;

import java.awt.Color;
import java.awt.Graphics;

public class MoveSQJPanel extends javax.swing.JPanel {

    private int Y = 120;
    public MoveSQJPanel() {
        initComponents();
    }

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

        g.setColor(Color.RED);
        g.drawRect(120, Y, 50, 50);
        g.fillRect(120, Y, 50, 50);
    }

    private void btnUppActionPerformed(java.awt.event.ActionEvent evt) {
    }
    Y+=5;
    repaint();
    private void btnNerActionPerformed(java.awt.event.ActionEvent evt) {
    }
    Y-=5;
    repaint();
    private javax.swing.JButton btnNer;
    private javax.swing.JButton btnUpp;
}
0 Answers
Related