Swing実装での図形の追加と色の指定追加
以下の作成した図形描写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);
}
}
お礼
回答ありがとうございます! なるほど! 勝手にクラスだと思ってました。