0
何らかの理由で、ユーザーがマウスを離した後に矩形を描くことができない理由がわかりません。 draw()
メソッドを有効にし、オブジェクトをdrawPanel
に描画する矩形オブジェクトを作成する必要があります。マウスを離した後に形を描くように見えない
ShapeGui.javaクラス:
public class ShapeGui extends JFrame{
private JButton[] buttons;
private final String[] butLabel = {"Red","Green","Blue","Rectangle","Circle","Exit"};
private Container pane;
private JLabel dateLabel;
private int x1,x2,y1,y2;
private String color;
JTextArea textArea;
ButtonHandler butHandle = new ButtonHandler();
private boolean isRect=true; //set default draw shape when program run to rectangel
JPanel drawPanel= new JPanel();
public ShapeGui(){
super("Draw Shape GUI");
pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(createButton(), BorderLayout.CENTER);
pane.add(textArea(), BorderLayout.EAST);
pane.add(drawSpace(), BorderLayout.WEST);
}
public JPanel createButton(){
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(2,3));
buttons = new JButton[butLabel.length];
for(int i=0; i<butLabel.length; i++){
buttons[i]= new JButton(butLabel[i]);
buttons[i].addActionListener(butHandle);
buttonPanel.add(buttons[i]);
}
return buttonPanel;
}
public JPanel drawSpace(){
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
leftPanel.setPreferredSize(new Dimension(200,450));
//create panel for mouse click event
drawPanel= new JPanel();
leftPanel.add(drawPanel, BorderLayout.CENTER);
MouseHandler mouseHandle = new MouseHandler();
drawPanel.addMouseListener(mouseHandle);
drawPanel.addMouseMotionListener(mouseHandle);
//create date label and format to show only date
LocalDate today = LocalDate.now(ZoneId.of("America/Montreal"));
dateLabel= new JLabel(today.toString());
leftPanel.add(dateLabel,BorderLayout.SOUTH); //add date to panel
return leftPanel;
}
public JPanel textArea(){
JPanel textPanel= new JPanel();
textArea= new JTextArea(15,15);
//add textarea inside scroll pane
JScrollPane scroll = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
textPanel.add(scroll);
return textPanel;
}
//method to draw shape after button is pressed
public void drawShape(){
if(isRect){
Rectangle rect = new Rectangle(x1,y1,x2,y2,color);
drawPanel.add(rect);
rect.draw();
}
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj == buttons[0]){
color="RED";
System.out.println("Color: "+color);
}
if(obj == buttons[1]){
color="GREEN";
System.out.println("Color: "+color);
}
if(obj == buttons[2]){
color="BLUE";
System.out.println("Color: "+color);
}
if(obj == buttons[3]){
isRect=true;
System.out.println("Boolean Rectangle: "+isRect);
}
if(obj == buttons[4]){ //change to false once button circle is clicked
isRect=false;
System.out.println("Boolean Rectangle: "+isRect);
}
if(obj == buttons[5]){
//serialization
System.exit(0);
}
}
} //end of buttonHandler
private class MouseHandler extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e){
textArea.append(String.format("Pressed at [%d, %d]\n", e.getX(),e.getY()));
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e){
textArea.append(String.format("Released at [%d, %d]\n", e.getX(),e.getY()));
x2=e.getX();
y2=e.getY();
drawShape();
}
}
}
Rectangleクラス:
public class Rectangle extends JPanel{
private int x1,x2,y1,y2;
String color;
public Rectangle(int x1, int y1, int x2, int y2,String color) {
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
//change color
if(color.equalsIgnoreCase("Red"))
g.setColor(Color.RED);
else if(color.equalsIgnoreCase("Green")){
g.setColor(Color.GREEN);
}
else
g.setColor(Color.BLUE);
g.fillRect(x1, y1, (x2-x1), (y2-y1));
}
public void draw(){
repaint();
}
}
ありがとうございました!私は主にあなたがしたことを理解しています。それは非常に役に立ちました。しかし、私はpaintComponentメソッドで "スレッド内の例外" AWT-EventQueue-0 "java.lang.NullPointerException"を受け取ったようです。何らかの理由でプログラムが先に方法を実行しました。マウスをドラッグして離した後も引き続き実行されます。 –