Hi I am trying to change the size of my square by putting a button, let's name it plus, that it adds a units for example, if my square is 1cm*1cm the plus button makes it 10cm*10cm I did diminuer() to test if it works but it doesn't it says that those variables in addSquare is not public in Compenent here's the code for you to see what I have for now:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class Carre extends JFrame implements ActionListener {
JButton boutPlus, boutMoins, boutCouleur;
Squares squares;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
}
public Carre() {
super("Carre");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
squares = new Squares();
getContentPane().add(squares);
//for (int i = 0; i < 1; i++) {
squares.addSquare(10, 10, 100, 100);
Insets insets = getInsets();
System.out.println("insets.left = " + insets.left);
System.out.println("insets.right = " + insets.right);
System.out.println("insets.top = " + insets.top);
System.out.println("insets.bottom = " + insets.bottom);
pack();
setLocationRelativeTo(null);
setVisible(true);
JPanel simplePanel = new JPanel();
simplePanel.setLayout(null);
add(simplePanel);
boutPlus = new JButton("PLUS");
boutPlus.setForeground(Color.white);
boutPlus.setBackground(new Color(63, 107, 220));
simplePanel.add(boutPlus);
boutPlus.setBounds(325, 50, 200, 80);
boutPlus.addActionListener(this);
boutMoins = new JButton("MOINS");
boutMoins.setForeground(Color.white);
boutMoins.setBackground(new Color(145, 110, 220));
simplePanel.add(boutMoins);
boutMoins.setBounds(325, 150, 200, 80);
boutMoins.addActionListener(this);
boutCouleur = new JButton("COULEUR");
boutCouleur.setForeground(Color.white);
boutCouleur.setBackground(new Color(150, 200, 80));
simplePanel.add(boutCouleur);
boutCouleur.setBounds(325, 250, 200, 80);
boutCouleur.addActionListener(this);
}
public static void main(String[] args) {
new Carre();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boutPlus) {
squares.augmenter();
}
if (e.getSource() == boutMoins && squares.diminuer()< Double.MIN_VALUE ) { throw new HorsLimitException ();
squares.diminuer();
}
if (e.getSource() == boutCouleur) {
Graphics g=null;
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.YELLOW);
g2.fillRect(10,20,100,150);
}
}
}
class Squares extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private List<Rectangle> squares = new ArrayList<Rectangle>();
public void addSquare(int x, int y, int width, int height) {
Rectangle rect = new Rectangle(x, y, width, height);
squares.add(rect);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Rectangle rect : squares) {
g2.draw(rect);
}
}
public void diminuer() {
int x = 0;
squares.addSquare(x - 10);
}
public void augmenter() {
int x = 0;
squares.addSquare(x - 10);
}
}
class HorsLimitException extends ArithmeticException {
public HorsLimitException () { //constructeur par défaut
super( "Tentative de d\u00e9passement de limite" );
}
public HorsLimitException ( String message ) {
super( message );
}
}
You aren't passing enough parameters to your addSquare() method and you are trying to call it using an ArrayList which doesn't exist. The method addSquare() only exists for your Squares Class.
public void diminuer() {
int x = 0;
squares.addSquare(x - 10); //too few parameters for your method
}
The method addSquare() takes 4 parameters
public void addSquare(int x, int y, int width, int height)
You should grab the last square added to your list and based on those dimensions create the new square. However the Rectangle Class isn't given so I don't know how to retrieve the member variables but you could do something similar to the following for the diminuer() method
public void diminuer() {
if(squares.size() > 0)
{
Rectangle obj = squares.get(squares.size() - 1);
addSquare(obj.x * 10, obj.y * 10, obj.width * 10, obj.height * 10);
}
}