質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

757閲覧

Javaでドローエディタを作成したい.

pirika

総合スコア6

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2022/06/30 05:48

このサイト(https://fclout.hateblo.jp/entry/20110528/1306553657)を参考にして,図形3つから色を3色選べるドローエディタを作成したところ,色は選べるものの,図形が四角形(rect)しか選べない状態となっています.

どのような改良が必要か教えていただけますでしょうか.

java

1import javax.swing.*; 2import java.awt.*; 3import java.awt.event.*; 4import java.util.*; 5 6class Figure { 7 protected int x, y, width, height; 8 protected Color color; 9 10 public Figure(int x, int y, int w, int h, Color c) { 11 this.x = x; 12 this.y = y; 13 width = w; 14 height = h; 15 color = c; 16 } 17 18 public void setSize(int w, int h) { 19 width = w; 20 height = h; 21 } 22 23 public void setLocation(int x, int y) { 24 this.x = x; 25 this.y = y; 26 } 27 28 public void reshape(int x1, int y1, int x2, int y2) { 29 } 30 31 public void draw(Graphics g) { 32 } 33 34} 35 36class CircleFigure extends Figure { 37 public CircleFigure(int x, int y, int w, int h, Color c) { 38 super(x, y, w, h, c); 39 } 40 41 public void reshape(int x1, int y1, int x2, int y2) { 42 int newx = Math.min(x1, x2); 43 int newy = Math.min(y1, y2); 44 int neww = Math.abs(x1 - x2); 45 int newh = Math.abs(y1 - y2); 46 setLocation(newx, newy); 47 setSize(neww, newh); 48 } 49 50 public void paint(Graphics g) { 51 g.setColor(color); 52 g.drawOval(x, y, width, height); 53 } 54} 55 56class LineFigure extends Figure { 57 public LineFigure(int x, int y, int w, int h, Color c) { 58 super(x, y, w, h, c); 59 } 60 61 public void reshape(int x1, int y1, int x2, int y2) { 62 setLocation(x1, y1); 63 setSize(x2, y2); 64 } 65 66 public void paint(Graphics g) { 67 g.setColor(color); 68 g.drawLine(x, y, width, height); 69 } 70} 71 72class RectangleFigure extends Figure { 73 public RectangleFigure(int x, int y, int w, int h, Color c) { 74 super(x, y, w, h, c); 75 } 76 77 public void reshape(int x1, int y1, int x2, int y2) { 78 int newx = Math.min(x1, x2); 79 int newy = Math.min(y1, y2); 80 int neww = Math.abs(x1 - x2); 81 int newh = Math.abs(y1 - y2); 82 setLocation(newx, newy); 83 setSize(neww, newh); 84 } 85 86 public void draw(Graphics g) { 87 g.setColor(color); 88 g.drawRect(x, y, width, height); 89 } 90} 91 92class DrawModel extends Observable { 93 protected ArrayList<Figure> fig; 94 protected String figurelabel; 95 protected Figure drawingFigure; 96 protected Color currentColor; 97 protected ViewPanel viewPanel; 98 99 public DrawModel() { 100 fig = new ArrayList<Figure>(); 101 drawingFigure = null; 102 currentColor = Color.red; 103 } 104 105 public void setViewPanel(ViewPanel c) { 106 viewPanel = c; 107 } 108 109 public ArrayList<Figure> getFigures() { 110 return fig; 111 } 112 113 public Figure getFigure(int idx) { 114 return fig.get(idx); 115 } 116 117 public void createFigure(int x, int y) { 118 Figure f = null; 119 ; 120 if (figurelabel == "rect") 121 f = new RectangleFigure(x, y, 0, 0, currentColor); 122 else if (figurelabel == "circ") 123 f = new CircleFigure(x, y, 0, 0, currentColor); 124 else if (figurelabel == "line") 125 f = new LineFigure(x, y, x, y, currentColor); 126 fig.add(f); 127 drawingFigure = f; 128 viewPanel.repaint(); 129 setChanged(); 130 notifyObservers(); 131 } 132 133 public void reshapeFigure(int x1, int y1, int x2, int y2) { 134 if (drawingFigure != null) { 135 drawingFigure.reshape(x1, y1, x2, y2); 136 viewPanel.repaint(); 137 setChanged(); 138 notifyObservers(); 139 } 140 } 141 142 public void changecolor(Color col) { 143 currentColor = col; 144 } 145 146 public void changefigure(String fi) { 147 figurelabel = fi; 148 } 149} 150 151class ViewPanel extends JPanel implements Observer { 152 protected DrawModel model; 153 154 public ViewPanel(DrawModel m) { 155 this.setBackground(Color.white); 156 model = m; 157 model.addObserver(this); 158 } 159 160 public void paintComponent(Graphics g) { 161 super.paintComponent(g); 162 ArrayList<Figure> fig = model.getFigures(); 163 for (int i = 0; i < fig.size(); i++) { 164 Figure f = fig.get(i); 165 f.draw(g); 166 } 167 } 168 169 public void update(Observable o, Object arg) { 170 repaint(); 171 } 172 173} 174 175class Select implements ActionListener { 176 DrawModel a; 177 178 Select(DrawModel ap) { 179 a = ap; 180 } 181 182 public void actionPerformed(ActionEvent e) { 183 String es = e.getActionCommand(); 184 if (es.equals("red")) 185 a.changecolor(Color.red); 186 if (es.equals("green")) 187 a.changecolor(Color.green); 188 if (es.equals("blue")) 189 a.changecolor(Color.blue); 190 if (es.equals("rect")) 191 a.changefigure("rect"); 192 if (es.equals("circ")) 193 a.changefigure("circ"); 194 if (es.equals("line")) 195 a.changefigure("line"); 196 197 } 198} 199 200class DrawFrame extends JFrame { 201 DrawModel model; 202 ViewPanel view; 203 DrawController cont; 204 205 public static void main(String[] args) { 206 JFrame f = new JFrame("Draw"); 207 JPanel pc = new JPanel(); 208 JPanel pf = new JPanel(); 209 pc.setLayout(new GridLayout(1, 3)); 210 pf.setLayout(new GridLayout(1, 3)); 211 JButton r = new JButton("red"); 212 JButton g = new JButton("green"); 213 JButton b = new JButton("blue"); 214 215 JButton rect = new JButton("rect"); 216 JButton circ = new JButton("circ"); 217 JButton line = new JButton("line"); 218 219 r.setActionCommand("red"); 220 g.setActionCommand("green"); 221 b.setActionCommand("blue"); 222 223 rect.setActionCommand("rect"); 224 circ.setActionCommand("circ"); 225 line.setActionCommand("line"); 226 227 DrawModel a = new DrawModel(); 228 ViewPanel dp = new ViewPanel(a); 229 a.setViewPanel(dp); 230 DrawController ml = new DrawController(a); 231 dp.addMouseListener(ml); 232 dp.addMouseMotionListener(ml); 233 234 pc.add(r); 235 pc.add(g); 236 pc.add(b); 237 238 pf.add(rect); 239 pf.add(circ); 240 pf.add(line); 241 242 b.addActionListener(new Select(a)); 243 g.addActionListener(new Select(a)); 244 r.addActionListener(new Select(a)); 245 246 circ.addActionListener(new Select(a)); 247 rect.addActionListener(new Select(a)); 248 line.addActionListener(new Select(a)); 249 250 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 251 f.getContentPane().add(dp, BorderLayout.CENTER); 252 f.getContentPane().add(pc, BorderLayout.SOUTH); 253 f.getContentPane().add(pf, BorderLayout.NORTH); 254 255 f.setSize(400, 300); 256 f.setVisible(true); 257 } 258} 259 260class DrawController implements MouseListener, MouseMotionListener { 261 protected DrawModel model; 262 protected int dragStartX, dragStartY; 263 264 public DrawController(DrawModel a) { 265 model = a; 266 } 267 268 public void mouseClicked(MouseEvent e) { 269 } 270 271 public void mousePressed(MouseEvent e) { 272 dragStartX = e.getX(); 273 dragStartY = e.getY(); 274 model.createFigure(dragStartX, dragStartY); 275 } 276 277 public void mouseDragged(MouseEvent e) { 278 model.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); 279 } 280 281 public void mouseReleased(MouseEvent e) { 282 model.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); 283 } 284 285 public void mouseEntered(MouseEvent e) { 286 } 287 288 public void mouseExited(MouseEvent e) { 289 } 290 291 public void mouseMoved(MouseEvent e) { 292 } 293}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2022/06/30 09:31

> ドローエディタを作成したい > どのような改良が必要か教えていただけますでしょうか ご自身の作品なのですから、どのようにでもなさって良いのではないでしょうか。
guest

回答1

0

ベストアンサー

図形が四角形(rect)しか選べない状態となっています.

CircleFigureLineFigure で、public void paint(Graphics g) { }となっていますが、drawの間違いでしょう。

こういったうっかりミスを防ぐために、@Overrideを書くようにしてください。
@Override public void draw(Graphics g) { }


文字列を==で比較するところが残っています。equalsで比較してください。

投稿2022/06/30 11:20

TN8001

総合スコア9317

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問