ユーザーが自分の GUI に描きたい形状を選択できるようにしようとしています。円形、正方形、長方形のボタンを選択できます。私の actionListener は、文字列をコンソールに出力するように機能しますが、GUI に形状が表示されません。actionCommand
を使用してパネルにその形状を描画するにはどうすればよいですか。
public void paintComponent(Graphics g) {
g2D = (Graphics2D) g;
//Rectangle2D rect = new Rectangle2D.Double(x, y, x2-x, y2-y);
//g2D.draw(rect);
repaint();
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("Rect")){
System.out.println("hello");
Rectangle2D rect = new Rectangle2D.Double(x, y, x2-x, y2-y);
g2D.draw(rect); //can only be accessed within paintComponent method
repaint();
}