Javaの勉強中です。三種類の図形を自由に描けるドローエディタのようなものを作成しようと思い、以下のものを参考にしてコードを書いてみたのですが、エラーが出てしまいます。
参考にしたサイト
自分が書いたコード
Java
1// ex1.java 2 3import javax.swing.*; 4import java.awt.*; 5import java.awt.event.*; 6import java.util.*; 7 8 9public class Figure { 10 protected int x, y, width, height; 11 protected Color color; 12 13 public Figure(int x, int y, int w, int h, Color c) { 14 this.x = x; this.y = y; 15 width = w; height = h; 16 color = c; 17 } 18 19 public void setSize(int w, int h) { 20 width = w; height = h; 21 } 22 23 public void setLocation(int x, int y) { 24 this.x = x; this.y = y; 25 } 26 27 public void reshape(int x1, int y1, int x2, int y2) { 28 int newx = Math.min(x1, x2); 29 int newy = Math.min(y1, y2); 30 int neww = Math.abs(x1 - x2); 31 int newh = Math.abs(y1 - y2); 32 setLocation(newx, newy); 33 setSize(neww, newh); 34 } 35 36 public void draw(Graphics g) {} 37 38} 39 40public class DrawFigure extends Figure{ 41 protected int version; 42 43 public DrawFigure(int x, int y, int w, int h, Color c, int v){ 44 super(x, y, w, h, c); 45 version = v; 46 } 47 48 public void draw(Graphics g){ 49 g.setColor(color); 50 if(version == 1){ 51 g.drawRect(x, y, width, height); 52 }else if(version == 2){ 53 g.drawOval(x, y, width,height); 54 }else if(version == 3){ 55 g.drawLine(x, y, x + width, y + height); 56 } 57 } 58} 59 60public class DrawModel extends Observable{ 61 protected ArrayList<Figure> fig; 62 protected Figure drawingFigure; 63 protected Color currentColor; 64 protected int version; 65 66 public DrawModel() { 67 fig = new ArrayList<Figure>(); 68 drawingFigure = null; 69 currentColor = Color.red; 70 version = 1; 71 } 72 73 public ArrayList<Figure> getFigures() { 74 return fig; 75 } 76 77 public Figure getFigure(int idx) { 78 return fig.get(idx); 79 } 80 81 public void createFigure(int x, int y) { 82 Figure f = new DrawFigure(x, y, 0, 0, currentColor); 83 fig.add(f); 84 drawingFigure = f; 85 setChanged(); 86 notifyObservers(); 87 } 88 89 public void reshapeFigure(int x1, int y1, int x2, int y2) { 90 if (drawingFigure != null) { 91 drawingFigure.reshape(x1, y1, x2, y2); 92 setChanged(); 93 notifyObservers(); 94 } 95 } 96 97 public void setColor(Color c){ 98 currentColor = c; 99 setChanged(); 100 notifyObservers(); 101 } 102 103 public void setVersion(int ver){ 104 version = ver; 105 setChanged(); 106 notifyObservers(); 107 } 108} 109 110public class ViewPanel extends JPanel implements Observer { 111 protected DrawModel model; 112 113 public ViewPanel(DrawModel m, DrawController c) { 114 this.setBackground(Color.white); 115 this.addMouseListener(c); 116 this.addMouseMotionListener(c); 117 model = m; 118 model.addObserver(this); 119 } 120 121 public void paintComponent(Graphics g) { 122 super.paintComponent(g); 123 ArrayList<Figure> fig = model.getFigures(); 124 for(int i = 0; i < fig.size(); i++) { 125 Figure f = fig.get(i); 126 f.draw(g); 127 } 128 } 129 130 public void update(Observable o, Object arg) { 131 repaint(); 132 } 133} 134 135public class DrawFrame extends JFrame implements ActionListener,ChangeListener{ 136 DrawModel model; 137 ViewPanel view; 138 DrawController cont; 139 JMenueBar menuebar; 140 JMenue color, shape; 141 JMenueItem rect, elipse, line; 142 JColorChooser colorchooser; 143 144 public DrawFrame() { 145 model = new DrawModel(); 146 cont = new DrawController(model); 147 view = new ViewPanel(model,cont); 148 colorchooser = new JColorChooser(Color.GREEN); 149 150 menuebar = new JMenueBar(); 151 152 color = new JMenue("COLOR"); 153 shape = new JMenue("SHAPE"); 154 155 rect = new JMenueItem("SQUARE"); 156 elipse = new JMenueItem("CIRCLE"); 157 line = new JMenueItem("LINE"); 158 159 menuebar.add(color); 160 color.add(colorchooser); 161 menuebar.add(shape); 162 shape.add(rect); 163 shape.add(elipse); 164 shape.add(line); 165 166 rect.addActionListener(this); 167 elipse.addActionListener(this); 168 line.addActionListener(this); 169 170 colorchooser.getSrlrctionModel().addChangeListener(this); 171 172 this.setJMenueBar(menuebar); 173 this.setBackground(Color.black); 174 this.setTitle("Draw Editor"); 175 this.setSize(1200, 1200); 176 this.add(view); 177 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 178 this.setVisible(true); 179 } 180 181 public static void main(String[] args) { 182 new DrawFrame(); 183 } 184} 185 186@Override 187public void actionPerformed(ActionEvent e) { 188 if (e.getActionCommand() == "SQUARE") { 189 model.setVersion(1); 190 } else if(e.getActionCommand() == "CIRCLE") { 191 model.setVersion(2); 192 } else if(e.getActionCommand() == "LINE") { 193 model.setVersion(3); 194 } 195} 196 197public class DrawController implements MouseListener, MouseMotionListener { 198 protected DrawModel model; 199 protected int dragStartX, dragStartY; 200 public DrawController(DrawModel a) { 201 model = a; 202 } 203 public void mouseClicked(MouseEvent e) {} 204 public void mousePressed(MouseEvent e) { 205 dragStartX = e.getX(); dragStartY = e.getY(); 206 model.createFigure(dragStartX, dragStartY); 207 } 208 public void mouseDragged(MouseEvent e) { 209 model.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); 210 } 211 public void mouseReleased(MouseEvent e) {} 212 public void mouseEntered(MouseEvent e) {} 213 public void mouseExited(MouseEvent e) {} 214 public void mouseMoved(MouseEvent e) {} 215}
出力されたエラーメッセージ
ex1.java:185: エラー: class、interfaceまたはenumがありません
public void actionPerformed(ActionEvent e) {
^
ex1.java:188: エラー: class、interfaceまたはenumがありません
} else if(e.getActionCommand() == "CIRCLE") {
^
ex1.java:190: エラー: class、interfaceまたはenumがありません
} else if(e.getActionCommand() == "LINE") {
^
ex1.java:192: エラー: class、interfaceまたはenumがありません
}
^
エラー4個
初心者すぎるのと、コードが長すぎてどこを修正すればいいのか分かりません。
どこをどのように修正すればいいのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。