Java 四角を書くツール
Javaの初心者です。
下のプログラムで(2)のfを(1)のfに対応付けたいのですが、エラーが出てしまいます。
どなたか判る方いらっしゃいましたら教えてください。
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;
protected Figure drawingFigure;
static Color currentColor;
protected DrawPanel drawPanel;
public DrawApplication() {
figures = new Vector<Figure>();
drawingFigure = null;
currentColor = Color.blue;
}
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 = new RectangleFigure(x,y,0,0,currentColor); // (1)
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;
drawApplication.setDrawPanel(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=0; i < drawApplication.getNumberOfFigures(); i++){
f = drawApplication.getFigure(i); //(2) この部分にエラーが出てしまう
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");
ButtonPanel b=new ButtonPanel();
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);
}
}
お礼
ありがとうございます。実行したらできました。repaint();はいつ実行されるかわからないんですね。線を全部保存するのもやってみようと思います。