編集:私はそれを理解し、プログラムを終了しました(最終的に)。助けてくれてありがとう!
ユーザーがラジオボタンをクリックして、マウスでドラッグしてパネルに描画できる形状を選択できる単純なペイントプログラムを作成しようとしています。自分でそれを理解し、できると思いたいので、コードでこれを実行したいだけです。しかし、メソッド「public void newShape(String shape) {」を宣言している行でこれらのエラーが発生しています。この1行で4つのエラーが発生しています。そのうちの 2 つは「エラー: 式の開始が不正です」、残りの 2 つは「エラー: ';'」です。期待される"。誰かが私を助けて、私が間違っていることを教えてくれたら、とても感謝しています。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
public class HW4_Paint extends JFrame {
private static Color color = Color.BLACK; // current selected drawing color
private static boolean filled = false; // fill mode - "Filled" or "Empty"
private static String currentShape = "Line"; // current selected shape
private static ArrayList<Shape> shapes = new ArrayList<Shape>(); // a list of all of the shapes in the current drawing
public static void main(String[] args) {
// create the frame and format it
JFrame frame = new HW4_Paint();
frame.setTitle("Paint Program");
frame.setSize(800,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public HW4_Paint() {
// create the menu bar
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
// make the file menu
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
jmb.add(fileMenu);
// make the exit menu item under the file menu
JMenuItem exitMenuItem = new JMenuItem("Exit", 'X');
fileMenu.add(exitMenuItem);
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// make the help menu
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
jmb.add(helpMenu);
// make the help menu item under the help menu
JMenuItem helpMenuItem = new JMenuItem("Help", 'H');
helpMenu.add(helpMenuItem);
helpMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = "This program allows you to draw pictures using different shapes and colors.\n"
+ "To change what shape you are drawing with, select the button next to Line, Oval, or Rectangle, depending on which shape you want to draw with.\n"
+ "If you want the shape to be filled in (only applicable with oval and rectangle), select the checkbox next to Filled.\n"
+ "To change the color of the shape that you are drawing, click the button of the color that you want to change to.\n"
+ "To undo the last shape that you drew, click the undo button once. You can repeat this as many times as you want until there are no remaining shapes.\n"
+ "To clear all of the shapes that you have drawn, click the Clear All button.";
JOptionPane.showMessageDialog(null, message, "Help", JOptionPane.INFORMATION_MESSAGE);
}
});
// make the about menu item under the help menu
JMenuItem aboutMenuItem = new JMenuItem("About", 'A');
helpMenu.add(aboutMenuItem);
aboutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = "HW4_Paint Program\n\n" + "Written by Joey Drees\n\n" + "CSC 360-001";
JOptionPane.showMessageDialog(null, message, "About", JOptionPane.INFORMATION_MESSAGE);
}
});
// make the panels that will be added to the frame for the GUI
JPanel guiPanel = new JPanel();
guiPanel.setLayout(new BorderLayout());
JPanel colorPanel = new JPanel(new FlowLayout());
JPanel buttonPanel = new JPanel(new GridLayout(6,1));
JPanel drawingPanel = new DrawingPanel();
// add the guiPanel to the frame and them add the other panels to the guiPanel
add(guiPanel);
guiPanel.add(colorPanel, BorderLayout.SOUTH);
guiPanel.add(buttonPanel, BorderLayout.EAST);
guiPanel.add(drawingPanel, BorderLayout.CENTER);
// create and add the buttons that will allow the user to change the color of the shapes
JButton blackButton = new JButton("Black");
colorPanel.add(blackButton);
blackButton.setSelected(true);
JButton whiteButton = new JButton("White");
colorPanel.add(whiteButton);
JButton grayButton = new JButton("Gray");
colorPanel.add(grayButton);
JButton redButton = new JButton("Red");
colorPanel.add(redButton);
JButton yellowButton = new JButton("Yellow");
colorPanel.add(yellowButton);
JButton greenButton = new JButton("Green");
colorPanel.add(greenButton);
JButton blueButton = new JButton("Blue");
colorPanel.add(blueButton);
JButton newColorButton = new JButton("New Color");
colorPanel.add(newColorButton);
// create and add the radio buttons to a button group that will allow the user to change the shape they are drawing with
ButtonGroup jbtGroup = new ButtonGroup();
JRadioButton jrbLine = new JRadioButton("Line");
jrbLine.setMnemonic('L');
jbtGroup.add(jrbLine);
buttonPanel.add(jrbLine);
jrbLine.setSelected(true);
jrbLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
currentShape = "Line";
guiPanel.newShape(currentShape);
repaint();
}
});
JRadioButton jrbOval = new JRadioButton("Oval");
jrbOval.setMnemonic('O');
jbtGroup.add(jrbOval);
buttonPanel.add(jrbOval);
jrbOval.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
currentShape = "Oval";
guiPanel.newShape(currentShape);
repaint();
}
});
JRadioButton jrbRectangle = new JRadioButton("Rectangle");
jrbRectangle.setMnemonic('R');
jbtGroup.add(jrbRectangle);
buttonPanel.add(jrbRectangle);
jrbLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
currentShape = "Rectangle";
guiPanel.newShape(currentShape);
repaint();
}
});
// create and add the check box that will allow the user to create filled shapes
JCheckBox jcbFilled = new JCheckBox("Filled");
jcbFilled.setMnemonic('F');
buttonPanel.add(jcbFilled);
if (jrbLine.isSelected())
jcbFilled.setEnabled(false);
else
jcbFilled.setEnabled(true);
// create and add the buttons that will allow the user to undo their last shape and clear the whole panel
JButton undoButton = new JButton("Undo");
undoButton.setMnemonic('U');
buttonPanel.add(undoButton);
JButton clearAllButton = new JButton("Clear All");
clearAllButton.setMnemonic('C');
buttonPanel.add(clearAllButton);
// create the action listener for the undo button
undoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!shapes.isEmpty()) {
shapes.remove(shapes.size() - 1);
repaint();
}
}
});
// create the action listener for the clear all button
clearAllButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!shapes.isEmpty()) {
shapes.clear();
repaint();
}
}
});
public void newShape(String shape) {
switch (shape) {
case "Line":
Shape line = new Line(startX, startY, endX, endY);
shapes.add(line);
break;
case "Oval":
Shape oval = new Oval(startX, startY, endX, endY);
shapes.add(oval);
break;
case "Rectangle":
Shape rectangle = new Rectangle(startX, startY, endX, endY);
shapes.add(rectangle);
break;
default:
System.out.println("ERROR. Check logic.");
}
}
}
}
private class DrawingPanel extends JPanel {
private int startX, startY, endX, endY;
public DrawingPanel() { // sets the background to white and adds the required listeners
setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startX = endX = e.getX();
startY = endY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
Shape lastShape = shapes.get(shapes.size()-1);
lastShape.setEnd(endX, endY);
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Shape shape: shapes)
shape.draw(g);
}
}