2017-12-18 3 views
-2

안녕하세요, 제 사각형의 크기를 변경하려고합니다. 버튼을 두어 크기를 변경하려고합니다. 예를 들어, 단위가 추가됩니다. 제곱이 1cm * 1cm 인 경우 플러스입니다. 버튼을 누르면 10cm * 10cm이됩니다. 작동하는지 테스트하기 위해 diminuer()를 사용했으나 addSquare의 변수가 Compenent에 public이 아니라고 표시하지 않습니다.버튼을 눌러 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); 
      } 
} 

답변

0

addSquare() 메서드에 충분한 매개 변수를 전달하지 않고 있으며 존재하지 않는 ArrayList를 사용하여 매개 변수를 호출하려고합니다. addSquare() 메서드는 Squares 클래스에만 존재합니다.

public void diminuer() { 
    int x = 0; 
    squares.addSquare(x - 10); //too few parameters for your method 
} 

방법의 addSquare는() 당신은 마지막 사각형 목록에 추가하고 그 크기에 따라 새로운 사각형을 만들 잡아해야 4 개 매개 변수

public void addSquare(int x, int y, int width, int height) 

걸립니다. 그러나 사각형 클래스는 제공되지 않으므로 멤버 변수를 검색하는 방법을 모르지만 diminuer() 메서드에 대해 다음과 비슷한 작업을 수행 할 수 있습니다.

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); 
    } 
} 
관련 문제