-2
こんにちは私の正方形が1cm * 1cmのプラスの場合は、ボタンを置いて正方形のサイズを変更しようとしています。ボタンを押すと10cm * 10cmになります。動作しているかどうかを調べるには、私は何かを行いましたが、addSquareの変数がCompenentで公開されていないとは言いません。ボタンを押してJavaの正方形のサイズを変更する
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);
}
}