![](http://oshiete.xgoo.jp/images/v2/pc/qa/question_title.png?e8efa67)
以下の作成した図形描写javaプログラムに図形の変更ボタン(円や直線)、色選択(例:赤、青、緑の三種)をおこなうことの出きるようにすればどうすれば良いでしょうか?
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 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; /* Generics */
protected Figure drawingFigure;
protected Color currentColor;
protected DrawPanel drawPanel;
public DrawApplication() {
figures = new Vector<Figure>(); /* Generics */
drawingFigure = null;
currentColor = Color.red;
}
public void setDrawPanel(DrawPanel c) {
System.out.print("セットされました");
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 = new RectangleFigure(x,y,0,0,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();
}
}
}
class DrawPanel extends JPanel {
protected DrawApplication drawApplication;
public DrawPanel(DrawApplication app) {
setBackground(Color.white);
drawApplication = app;
app.setDrawPanel(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Figure f = new RectangleFigure(0,0,0,0,drawApplication.currentColor);
for(int i=0;i<drawApplication.getNumberOfFigures();i++){
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();
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 DrawMain {
public static void main(String argv[]) {
JFrame f = new JFrame("Draw");
DrawApplication app = new DrawApplication();
JPanel c =new DrawPanel(app);
c.addMouseListener(new DrawMouseListener(app));
c.addMouseMotionListener(new DrawMouseListener(app));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(c,BorderLayout.CENTER);
f.setSize(400,300);
f.setVisible(true);
}
}
A 回答 (3件)
- 最新から表示
- 回答順に表示
No.3
- 回答日時:
> 同様にfillRectやLineをつかって追加をしようとしましたがエラーが出ました
> どのように行い挿入すればよいでしょうか
LineはFigureのサブクラスを作ればよいですが、widthとheightの意味が違ってきますね(幅や高さではなく線の終端の座標になる)。fillRectは、Figureのサブクラスを新たに作るか、または、既存のRectangleFigureクラスにbooleanのフラグパラメータを設けて、paint()の中でifすればよいでしょう。
> 赤と緑のループになってしまい青が反映されなくなってしまいました
> どこを修正すればよいでしょうか
これは、このドジなループを見たら幼稚園児にでも分かるでしょう。
◎図形の種類や色数が多いときは、ボタンではなくJComboBoxを使ってください。
No.2
- 回答日時:
> JComboBoxを使用して切り替えが可能なようにする場合
> にはどこにどのように組めばいいでしょうか?
以下に、JComboBoxではなくボタンを使う超簡単なバージョンをお見せします。JComboBoxも、置き場所やActionListenerの書き方も同じでよいでしょう。ActionListenerは、単独のクラスにしてもよいですが。
----------------------------------------------------
全角スペース->半角スペース2に
/* save and compile as DrawMain.java */
/* run: > java DrawMain */
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 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) {
x = Math.min(x1, x2);
y = Math.min(y1, y2);
width = Math.abs(x1 - x2);
height = Math.abs(y1 - y2);
}
public void paint(Graphics g) {
g.setColor(color);
g.drawRect(x, y, width, height);
}
}
class OvalFigure extends Figure{
public OvalFigure(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) {
x = Math.min(x1, x2);
y = Math.min(y1, y2);
width = Math.abs(x1 - x2);
height = Math.abs(y1 - y2);
}
public void paint(Graphics g) {
g.setColor(color);
g.drawOval(x, y, width, height);
}
}
class DrawApplication {
protected Vector<Figure> figures; /* Generics */
protected Figure drawingFigure;
protected Color currentColor;
protected DrawPanel drawPanel;
boolean oval = false;
public DrawApplication() {
figures = new Vector<Figure>(); /* Generics */
drawingFigure = null;
currentColor = Color.red;
}
public void setDrawPanel(DrawPanel c) {
drawPanel = c;
}
public int getNumberOfFigures() {
return figures.size();
}
public void setColor(Color c){
currentColor = c;
}
public Color getColor(){
return currentColor;
}
public void toggleOval(){
oval = ! oval;
}
public Figure getFigure(int index) {
return (Figure)figures.elementAt(index);
}
public void createFigure(int x, int y) {
Figure f
= oval ? new OvalFigure(x, y, 0, 0, currentColor)
: new RectangleFigure(x, y, 0, 0, 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();
}
}
}
class DrawPanel extends JPanel {
protected DrawApplication drawApplication;
public DrawPanel(DrawApplication app) {
setBackground(Color.white);
drawApplication = app;
app.setDrawPanel(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
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();
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) { }
}
public class DrawMain {
public static void main(String argv[]) {
JFrame f = new JFrame("Draw");
final DrawApplication app = new DrawApplication();
DrawPanel c =new DrawPanel(app);
DrawMouseListener dm = new DrawMouseListener(app);
c.addMouseListener(dm);
c.addMouseMotionListener(dm);
JPanel p = new JPanel();
JButton cc = new JButton("Change Next Color");
JButton fc = new JButton("Change Next Figure");
p.add(cc);
p.add(fc);
cc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (app.getColor() == Color.red){
app.setColor(Color.blue);
}
else{
app.setColor(Color.red);
}
}
});
fc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
app.toggleOval();
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(c, BorderLayout.CENTER);
f.getContentPane().add(p, BorderLayout.SOUTH);
f.setSize(400,300);
f.setVisible(true);
}
}
------------------------------------------------
詳しいプログラム、本当にありがとうございました
さらに塗りつぶした四角や直線を描けるようにしようと
同様にfillRectやLineをつかって追加をしようとしましたがエラーが出ました
どのように行い挿入すればよいでしょうか
さらに色の変更に緑を追加しようと
cc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (app.getColor() == Color.red){
app.setColor(Color.blue);
}
if (app.getColor() == Color.blue){
app.setColor(Color.green);
}
else{
app.setColor(Color.red);
}
}
});
と組んだところ
赤と緑のループになってしまい青が反映されなくなってしまいました
どこを修正すればよいでしょうか
No.1
- 回答日時:
図形や色の選択は、ボタンをたくさん並べるよりもJComboBoxが楽ですが、まあボタンでもいいです。
それらのActionListenerの中で、paintComponent()メソッドから見える変数やその値を変えて、それからパネルのrepaint()を呼ぶだけです。
たとえばcolor変数を変えたら、paintComponent()の中の
g.setColor(color);で勝手に色が変わるし、
shapeの値を変えたら、paintComponent()の中のif文で
if (shape == CIRCLE){ //適当なintの値でよい、enumが使えればenumがベター
g.drawOval(.......);
else if (shape == SQUARE){
g.drawRect(........);
などなどのようになります。
今現在のあなたのpaintComponent()メソッドは、完全に、めちゃめちゃに間違いですから、消してください。
参考文献としては、このあたり:
http://java.sun.com/docs/books/tutorial/uiswing/ …
回答ありがとうございます
JComboBoxを使用して切り替えが可能なようにする場合にはどこにどのように組めばいいでしょうか?
ソースのどこに組めば良いかもわからなくなってきました
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Java java 入力 3 4 3 出力 ABC DEFG HIJ このようなプログラムの書き方を教えてくだ 2 2022/07/15 14:18
- Ruby 【JAVA】数字をひし形に出力するプログラムについて 2 2022/07/11 23:32
- Java Java プログラム public class Main { public static void 3 2023/08/10 23:46
- C言語・C++・C# 大量のデータを読み込んで表示する速度を改善したい 8 2023/05/07 13:29
- Java javaでのプログラム(配列)について質問です. 2 2022/10/14 22:27
- Java 直し方について教えて頂きたいです。 4 2022/08/13 02:11
- Java Java 配列<選挙> 4 2023/07/31 15:07
- C言語・C++・C# C++プログラミングコードにポリモーフィズムを取り入れ方を教えてください。 2 2023/06/09 11:17
- C言語・C++・C# c言語の問題です 3 2023/01/10 16:15
- C言語・C++・C# C言語の課題が出たのですが自力でやっても分かりませんでした。 要素数がnであるint型の配列v2の並 3 2022/11/19 17:41
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
Javaで電卓を作りたい
-
javaでcsvファイル読込時の改行...
-
コード中の謎のエラー
-
intが負の時に投げる例外はあり...
-
Javaについて
-
「配列定数は、イニシャライザ...
-
Java 初心者 int型の取り扱い方
-
SwingでJtableのヘッダ行が表示...
-
C#で「型または名前空間名 'int...
-
全組み合わせの出力
-
JUnit4のアノテーションについて
-
初心者ですが、今javaで簡単な...
-
C#でfirefoxのウインドウを移動...
-
C#で実行時にメソッドの返り値...
-
Swing の実装でどうしてもエラ...
-
式の型は配列型で int に解決済...
-
Swing実装での図形の追加と色の...
-
JAVAでCの関数ポインタのような...
-
java初心者の質問(3つの整数の...
-
C言語のポインターに関する警告
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
「配列定数は、イニシャライザ...
-
Javaで電卓を作りたい
-
式の型は配列型で int に解決済...
-
javaでカレンダー作成
-
JAVA エラー 式の開始が不正で...
-
JAVAでCの関数ポインタのような...
-
java spring でエラーが出て困...
-
6桁の数字を重複なしでランダム...
-
c# デリゲート関連の命名について
-
C++からC#のdllを参照する際、...
-
メインが含まれていません
-
(Swing)JTextFieldを半角のみ入...
-
DataSet(DataTable)の使い方
-
三目並べ(Tick-Tack-Toe)をJav...
-
JUnit4のアノテーションについて
-
初心者ですが、今javaで簡単な...
-
classを使って座標軸を求めるコ...
-
javaでcsvファイル読込時の改行...
-
C#で実行時にメソッドの返り値...
-
Java 初心者 int型の取り扱い方
おすすめ情報