公式アカウントからの投稿が始まります

以下の作成した図形描写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件)

> 同様にfillRectやLineをつかって追加をしようとしましたがエラーが出ました


> どのように行い挿入すればよいでしょうか
LineはFigureのサブクラスを作ればよいですが、widthとheightの意味が違ってきますね(幅や高さではなく線の終端の座標になる)。fillRectは、Figureのサブクラスを新たに作るか、または、既存のRectangleFigureクラスにbooleanのフラグパラメータを設けて、paint()の中でifすればよいでしょう。

> 赤と緑のループになってしまい青が反映されなくなってしまいました
> どこを修正すればよいでしょうか
これは、このドジなループを見たら幼稚園児にでも分かるでしょう。

◎図形の種類や色数が多いときは、ボタンではなくJComboBoxを使ってください。
    • good
    • 0

> 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);
 }
}
------------------------------------------------
    • good
    • 0
この回答へのお礼

詳しいプログラム、本当にありがとうございました
さらに塗りつぶした四角や直線を描けるようにしようと
同様に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);
}
}
});
と組んだところ
赤と緑のループになってしまい青が反映されなくなってしまいました
どこを修正すればよいでしょうか

お礼日時:2008/05/13 22:14

図形や色の選択は、ボタンをたくさん並べるよりも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/ …
    • good
    • 0
この回答へのお礼

回答ありがとうございます
JComboBoxを使用して切り替えが可能なようにする場合にはどこにどのように組めばいいでしょうか?

ソースのどこに組めば良いかもわからなくなってきました

お礼日時:2008/05/13 15:56

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!