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

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

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

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

Q&A

解決済

2回答

2111閲覧

Javaでペイントソフトを作っています。画面のクリアと画面消去の仕様をどうすればいいか困っています。

asapan

総合スコア60

Java

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

1グッド

1クリップ

投稿2018/01/07 17:50

Javaでペイントソフトを作っています。画面のクリアと画面消去の仕様をどうすればいいか困っています。

具体的にはClearというボタンを押したら画面に書かれている図をすべて消す
Quitというボタンを押したらウィンドウを消す。

という仕様です。
以下が途中のソースコードになります。

package java07sentaku;

import javax.swing.;
import java.awt.
;
import java.awt.event.;
import java.util.
;

abstract class Figure {
protected int x, y, width, height;
protected Color color;

public Figure(int x, int y, int w, int h, Color c) { this.x = x; this.y = y; width = w; height = h; color = c; } public void setSize(int w, int h) { width = w; height = h; } public void setLocation(int x, int y) { this.x = x; this.y = y; } abstract public void reshape(int x1, int y1, int x2, int y2); abstract public void paint(Graphics g);

}

class LineFigure extends Figure {
public LineFigure(int x, int y, int w, int h, Color c) {
super(x, y, w, h, c);
}

public void reshape(int x1, int y1, int x2, int y2) { setLocation(x1, y1); setSize(x2, y2); } public void paint(Graphics g) { g.setColor(color); g.drawLine(x, y, width, height); }

}

class CircleFigure extends Figure {
public CircleFigure(int x, int y, int w, int h, Color c) {
super(x, y, w, h, c);
}

public void reshape(int x1, int y1, int x2, int y2) { int newx = Math.min(x1, x2); int newy = Math.min(y1, y2); int neww = Math.abs(x1 - x2); int newh = Math.abs(y1 - y2); setLocation(newx, newy); setSize(neww, newh); } public void paint(Graphics g) { g.setColor(color); g.drawOval(x, y, width, height); }

}

class RectangleFigure extends Figure {
public RectangleFigure(int x, int y, int w, int h, Color c) {
super(x, y, w, h, c);
}

public void reshape(int x1, int y1, int x2, int y2) { int newx = Math.min(x1, x2); int newy = Math.min(y1, y2); int neww = Math.abs(x1 - x2); int newh = Math.abs(y1 - y2); setLocation(newx, newy); setSize(neww, newh); } public void paint(Graphics g) { g.setColor(color); g.drawRect(x, y, width, height); }

}

class DrawApplication {
protected Vector<Figure> figures;
protected Figure drawingFigure;
protected String figurelabel;
protected Color currentColor;
protected DrawPanel drawPanel;

public DrawApplication() { figures = new Vector<Figure>(); drawingFigure = null; currentColor = Color.BLACK; figurelabel = "rect"; } public void setDrawPanel(DrawPanel c) { drawPanel = c; } public int getNumberOfFigures() { return figures.size(); } public Figure getFigure(int index) { return (Figure) figures.elementAt(index); } public void createFigure(int x, int y) { Figure f = null; ; if (figurelabel == "rect") f = new RectangleFigure(x, y, 0, 0, currentColor); else if (figurelabel == "circ") f = new CircleFigure(x, y, 0, 0, currentColor); else if (figurelabel == "line") f = new LineFigure(x, y, x, y, currentColor); figures.addElement(f); drawingFigure = f; drawPanel.repaint(); } public void reshapeFigure(int x1, int y1, int x2, int y2) { if (drawingFigure != null) { drawingFigure.reshape(x1, y1, x2, y2); drawPanel.repaint(); } } public void changecolor(Color c) { currentColor = c; } public void changefigure(String s) { figurelabel = s; } public void undo() { figures.remove(figures.size()-1); drawPanel.repaint(); }

}

class DrawPanel extends JPanel {
protected DrawApplication drawApplication;

public DrawPanel(DrawApplication app) { setBackground(Color.white); drawApplication = app; } public void paintComponent(Graphics g) { super.paintComponent(g); // [すべてのFigureをpaintする] for (int i = 0; i < drawApplication.getNumberOfFigures(); i++) { Figure f = drawApplication.getFigure(i); f.paint(g); } }

}

class DrawMouseListener implements MouseListener, MouseMotionListener {
protected DrawApplication drawApplication;
protected int dragStartX, dragStartY;

public DrawMouseListener(DrawApplication a) { drawApplication = a; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { dragStartX = e.getX(); dragStartY = e.getY(); if (SwingUtilities.isRightMouseButton(e) == true) drawApplication.undo(); else if (SwingUtilities.isLeftMouseButton(e) == true) drawApplication.createFigure(dragStartX, dragStartY); } public void mouseReleased(MouseEvent e) { drawApplication.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { drawApplication.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); } public void mouseMoved(MouseEvent e) { }

}

class Select implements ActionListener {
DrawApplication a;

Select(DrawApplication ap) { a = ap; } public void actionPerformed(ActionEvent e) { // 設定の変更 String es = e.getActionCommand(); if (es.equals("red")) a.changecolor(Color.red); if (es.equals("green")) a.changecolor(Color.green); if (es.equals("black")) a.changecolor(Color.black); if (es.equals("rect")) a.changefigure("rect"); if (es.equals("circ")) a.changefigure("circ"); if (es.equals("line")) a.changefigure("line"); if(es.equals("clear")){ //ここに画面消去の処理 } if(es.equals("Quit")){ //ここにウィンドウを閉じる処理 } }

}

class DrawMain {
public static void main(String argv[]) {
JFrame f = new JFrame("Draw");
JPanel pc = new JPanel();
JPanel pf = new JPanel();
pc.setLayout(new GridLayout(1, 3));
pf.setLayout(new GridLayout(1, 3));
JButton r = new JButton("Red");
JButton g = new JButton("Green");
JButton b = new JButton("Black");

JButton rect = new JButton("Rect"); JButton circ = new JButton("Oval"); JButton line = new JButton("Line"); JButton clear = new JButton("Clear"); JButton quit = new JButton("Quit"); r.setActionCommand("red"); g.setActionCommand("green"); b.setActionCommand("black"); rect.setActionCommand("rect"); circ.setActionCommand("circ"); line.setActionCommand("line"); //clear.setActioncommand("clear"); //clear.setActioncommand("quit"); DrawApplication a = new DrawApplication(); DrawPanel dp = new DrawPanel(a); a.setDrawPanel(dp); DrawMouseListener ml = new DrawMouseListener(a); dp.addMouseListener(ml); dp.addMouseMotionListener(ml); pc.add(r); pc.add(g); pc.add(b); pf.add(rect); pf.add(circ); pf.add(line); pf.add(clear); pf.add(quit); b.addActionListener(new Select(a)); g.addActionListener(new Select(a)); r.addActionListener(new Select(a)); rect.addActionListener(new Select(a)); circ.addActionListener(new Select(a)); line.addActionListener(new Select(a)); clear.addActionListener(new Select(a)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(dp, BorderLayout.CENTER); f.getContentPane().add(pc, BorderLayout.SOUTH); f.getContentPane().add(pf, BorderLayout.NORTH); f.setSize(400, 300); f.setVisible(true); }

}

この書き方で実装できない場合は
書き換えても全然構いません。

どうぞよろしくお願い致します。

doy597👍を押しています

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

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

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

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

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

guest

回答2

0

自己解決

if(es.equals("clear")){
int num = a.getNumberOfFigures();
for (int i = 0; i < num ; i++) {
a.undo();
}
}
if(es.equals("quit")){
System.exit(0);
}

これで解決できました。

投稿2018/01/07 18:24

asapan

総合スコア60

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

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

0

自己解決おめでとうございます。力作なペイントソフトですね。
解決済みですが、ソースコードを見て気になった点がありましたので、勝手ですが添削したソースコードをコメント致します。

まず、画面のレイアウト定義ですが、eclipseを使用しているのでしたらマーケットプライスの画面よりWindowBuilderをインストールして、デザイン画面を使用し画面のレイアウトの作成を行う方法がお勧めです。ご参考まで。

以下は添削したソースコードです。

Java

1import java.awt.BorderLayout; 2import java.awt.Color; 3import java.awt.Graphics; 4import java.awt.GridLayout; 5import java.awt.Rectangle; 6import java.awt.event.ActionEvent; 7import java.awt.event.ActionListener; 8import java.awt.event.MouseEvent; 9import java.util.ArrayDeque; 10import java.util.Deque; 11 12import javax.swing.JButton; 13import javax.swing.JFrame; 14import javax.swing.JPanel; 15import javax.swing.SwingUtilities; 16import javax.swing.event.MouseInputAdapter; 17 18abstract class Figure { 19 //protected int x, y, width, height; 20 // 4つの変数をRectangleに変更 21 protected Rectangle rect = new Rectangle(); 22 protected Color color; 23 24 public Figure(int x, int y, int w, int h, Color c) { 25 setLocation(x, y); 26 setSize(w, h); 27 color = c; 28 } 29 30 public void setSize(int w, int h) { 31 rect.setSize(w, h); 32 } 33 34 public void setLocation(int x, int y) { 35 rect.setLocation(x, y); 36 } 37 38 abstract public void reshape(int x1, int y1, int x2, int y2); 39 40 abstract public void paint(Graphics g); 41} 42 43class LineFigure extends Figure { 44 public LineFigure(int x, int y, int w, int h, Color c) { 45 super(x, y, w, h, c); 46 } 47 //@Overrideアノテーションを付けてOverrideメソッドを明示する。 48 @Override 49 public void reshape(int x1, int y1, int x2, int y2) { 50 setLocation(x1, y1); 51 setSize(x2, y2); 52 } 53 54 @Override 55 public void paint(Graphics g) { 56 g.setColor(color); 57 g.drawLine(rect.x, rect.y, rect.width, rect.height); 58 } 59} 60 61class CircleFigure extends Figure { 62 public CircleFigure(int x, int y, int w, int h, Color c) { 63 super(x, y, w, h, c); 64 } 65 66 @Override 67 public void reshape(int x1, int y1, int x2, int y2) { 68 int newx = Math.min(x1, x2); 69 int newy = Math.min(y1, y2); 70 int neww = Math.abs(x1 - x2); 71 int newh = Math.abs(y1 - y2); 72 setLocation(newx, newy); 73 setSize(neww, newh); 74 } 75 76 @Override 77 public void paint(Graphics g) { 78 g.setColor(color); 79 g.drawOval(rect.x, rect.y, rect.width, rect.height); 80 } 81} 82 83class RectangleFigure extends Figure { 84 public RectangleFigure(int x, int y, int w, int h, Color c) { 85 super(x, y, w, h, c); 86 } 87 88 @Override 89 public void reshape(int x1, int y1, int x2, int y2) { 90 int newx = Math.min(x1, x2); 91 int newy = Math.min(y1, y2); 92 int neww = Math.abs(x1 - x2); 93 int newh = Math.abs(y1 - y2); 94 setLocation(newx, newy); 95 setSize(neww, newh); 96 } 97 98 @Override 99 public void paint(Graphics g) { 100 g.setColor(color); 101 g.drawRect(rect.x, rect.y, rect.width, rect.height); 102 } 103} 104 105class DrawApplication { 106 // protected Vector<Figure> figures; 107 // java.util.Dequeに変更 108 protected Deque<Figure> figures; 109 protected Figure drawingFigure; 110 protected String figurelabel; 111 protected Color currentColor; 112 protected DrawPanel drawPanel; 113 114 public DrawApplication() { 115 figures = new ArrayDeque<>(); 116 drawingFigure = null; 117 currentColor = Color.BLACK; 118 figurelabel = "rect"; 119 } 120 121 public void setDrawPanel(DrawPanel c) { 122 drawPanel = c; 123 } 124 125 public int getNumberOfFigures() { 126 return figures.size(); 127 } 128 129 public void paintComponent(Graphics g) { 130 // [すべてのFigureをpaintする] 131 for (Figure f : figures) { 132 f.paint(g); 133 } 134 } 135 136 public void createFigure(int x, int y) { 137 Figure f = null; 138 ; 139 if (figurelabel == "rect") 140 f = new RectangleFigure(x, y, 0, 0, currentColor); 141 else if (figurelabel == "circ") 142 f = new CircleFigure(x, y, 0, 0, currentColor); 143 else if (figurelabel == "line") 144 f = new LineFigure(x, y, x, y, currentColor); 145 146 figures.add(f); 147 drawingFigure = f; 148 drawPanel.repaint(); 149 } 150 151 public void reshapeFigure(int x1, int y1, int x2, int y2) { 152 if (drawingFigure != null) { 153 drawingFigure.reshape(x1, y1, x2, y2); 154 drawPanel.repaint(); 155 } 156 } 157 158 public void changecolor(Color c) { 159 currentColor = c; 160 } 161 162 public void changefigure(String s) { 163 figurelabel = s; 164 } 165 166 public void undo() { 167 // undo対象がない時は処理を抜ける 168 if (figures.isEmpty()) { 169 return; 170 } 171 figures.removeLast(); 172 drawPanel.repaint(); 173 } 174 175 public void undoAll() { 176 int num = figures.size(); 177 for (int i = 0; i < num; i++) { 178 this.undo(); 179 } 180 } 181} 182 183@SuppressWarnings("serial") 184class DrawPanel extends JPanel { 185 protected DrawApplication drawApplication; 186 187 public DrawPanel(DrawApplication app) { 188 setBackground(Color.white); 189 drawApplication = app; 190 } 191 192 @Override 193 public void paintComponent(Graphics g) { 194 super.paintComponent(g); 195 //// [すべてのFigureをpaintする] 196 // for (int i = 0; i < drawApplication.getNumberOfFigures(); i++) { 197 // Figure f = drawApplication.getFigure(i); 198 // f.paint(g); 199 // } 200 // DrawApplicationにpaintComponent処理を新設し処理を委譲 201 drawApplication.paintComponent(g); 202 } 203} 204 205// class DrawMouseListener implements MouseListener, MouseMotionListener { 206//MouseInputAdapterを継承するように変更 207class DrawMouseListener extends MouseInputAdapter { 208 protected DrawApplication drawApplication; 209 protected int dragStartX, dragStartY; 210 211 public DrawMouseListener(DrawApplication a) { 212 drawApplication = a; 213 } 214 215 @Override 216 public void mousePressed(MouseEvent e) { 217 dragStartX = e.getX(); 218 dragStartY = e.getY(); 219 if (SwingUtilities.isRightMouseButton(e) == true) 220 drawApplication.undo(); 221 else if (SwingUtilities.isLeftMouseButton(e) == true) 222 drawApplication.createFigure(dragStartX, dragStartY); 223 224 } 225 226 @Override 227 public void mouseReleased(MouseEvent e) { 228 drawApplication.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); 229 } 230 231 @Override 232 public void mouseDragged(MouseEvent e) { 233 drawApplication.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); 234 } 235} 236 237class Select implements ActionListener { 238 DrawApplication a; 239 240 Select(DrawApplication ap) { 241 a = ap; 242 } 243 244 @Override 245 public void actionPerformed(ActionEvent e) { // 設定の変更 246 String es = e.getActionCommand(); 247 if (es.equals("red")) 248 a.changecolor(Color.red); 249 if (es.equals("green")) 250 a.changecolor(Color.green); 251 if (es.equals("black")) 252 a.changecolor(Color.black); 253 if (es.equals("rect")) 254 a.changefigure("rect"); 255 if (es.equals("circ")) 256 a.changefigure("circ"); 257 if (es.equals("line")) 258 a.changefigure("line"); 259 if (es.equals("clear")) { 260 a.undoAll(); 261 } 262 if (es.equals("quit")) { 263 System.exit(0); 264 } 265 } 266} 267 268class DrawMain { 269 public static void main(String argv[]) { 270 SwingUtilities.invokeLater(() -> { 271 JFrame f = new JFrame("Draw"); 272 //setDefaultCloseOperationは一番先頭に 273 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 274 JPanel pc = new JPanel(); 275 JPanel pf = new JPanel(); 276 pc.setLayout(new GridLayout(1, 3)); 277 pf.setLayout(new GridLayout(1, 3)); 278 JButton r = new JButton("Red"); 279 JButton g = new JButton("Green"); 280 JButton b = new JButton("Black"); 281 282 JButton rect = new JButton("Rect"); 283 JButton circ = new JButton("Oval"); 284 JButton line = new JButton("Line"); 285 JButton clear = new JButton("Clear"); 286 JButton quit = new JButton("Quit"); 287 288 r.setActionCommand("red"); 289 g.setActionCommand("green"); 290 b.setActionCommand("black"); 291 292 rect.setActionCommand("rect"); 293 circ.setActionCommand("circ"); 294 line.setActionCommand("line"); 295 clear.setActionCommand("clear"); 296 quit.setActionCommand("quit"); 297 298 DrawApplication a = new DrawApplication(); 299 DrawPanel dp = new DrawPanel(a); 300 a.setDrawPanel(dp); 301 DrawMouseListener ml = new DrawMouseListener(a); 302 dp.addMouseListener(ml); 303 dp.addMouseMotionListener(ml); 304 pc.add(r); 305 pc.add(g); 306 pc.add(b); 307 308 pf.add(rect); 309 pf.add(circ); 310 pf.add(line); 311 pf.add(clear); 312 pf.add(quit); 313 //アクションリスナーを生成後に追加 314 Select s = new Select(a); 315 b.addActionListener(s); 316 g.addActionListener(s); 317 r.addActionListener(s); 318 rect.addActionListener(s); 319 circ.addActionListener(s); 320 line.addActionListener(s); 321 clear.addActionListener(s); 322 quit.addActionListener(s); 323 324 f.getContentPane().add(dp, BorderLayout.CENTER); 325 f.getContentPane().add(pc, BorderLayout.SOUTH); 326 f.getContentPane().add(pf, BorderLayout.NORTH); 327 328 f.setSize(400, 300); 329 f.setVisible(true); 330 }); 331 } 332}

投稿2018/01/07 20:12

編集2018/01/07 20:15
umyu

総合スコア5846

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

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

asapan

2018/01/07 20:21

そのようなやり方があったのですね!勉強になります、ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問