ショボ短歌会

初心者ですみませ。
次のリストがどうしてコンパイルを通っても実行時にエラーになってしまいます。どなたか、判る方原因を教えてください。

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 figures;
protected Figure drawingFigure;
protected Color currentColor;
protected DrawPanel drawPanel;
public DrawApplication() {
figures = new Vector();
drawingFigure = null;
currentColor = Color.red;
}
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);
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;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);

//
//[すべてのFigureをpaintする]
//
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とDrawPanelとDrawMouseListenerを作って組み立てる]
//
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件)

>なぜか、実行時に何も出なくて・・・。



そうですか?ちゃんと真っ白なDrawPanelが出て来ましたよ。
DrawPanelクラスのコンストラクタで、
setBackground(Color.white);

setBackground(Color.BLACK);
としたら、真っ黒なDrawPanelがでましたし。

また、DrawMouseListenerクラスの
public void mouseClicked(MouseEvent e) { }
あたりに、
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked!");
}
なんかすると、ちゃんと表示されますので、イベントも受け取ってます。
そこから先に何するかに関しては、DrawMainクラスに何も書いてないので分かりませんが…


>app.setDrawPanel(this); /*ここを導入したけれど,こういうプログラムの場合,
>drawApplication.setDrawPanel(this);の方が正解?*/

正解かどうかは分からないですが、自分ならdrawApplication.setDrawPanel(this);を使います。
(「参照先は同じだからどっちでもいいだろ」ってな意見もあると思いますが)
このコードの目的は、「クラス変数のsetDrawPanelメソッドを実行する」ことにあると思われます。
そこで、app.setDrawPanel(this);とする、つまりローカル変数を処理していると、
「この処理はローカル変数に対して行っている処理」に見えるので、
「クラス変数に対して行っている処理」というのが一見して分かりにくい。
一方、クラス変数drawApplicationを使えば、クラス変数に対して行っている処理、
つまりクラス全体に関わってる処理なんだろう、とすぐに分かると思われます。
    • good
    • 0
この回答へのお礼

詳しい説明ありがとうございます。
また、言葉足らずですみませんでした。
継承や抽象などをたくさん組み合わせているうちに、何がなんだかわからなくなってしまい・・。
コンパイル時のエラーも無く、私の拙い知識では一見問題ないように思われたのですが、マウスクリックで表示されるはずの四角がでず。
変わりにAWT関連のエラーらしきものが表示されていたもので。
自分が書いたプログラムでの構文エラーならなんとか読みこなせたんですけど、一気にチンぷんカンプンになってしまったんです。
お蔭様で、一気に霧が晴れてきた感じでした。ありがとうございました。

お礼日時:2007/04/17 09:15

あ、念のためにこの後で回答してくれる識者様が居られたら,個人的に聞きたいこと



public DrawPanel(DrawApplication app) {
setBackground(Color.white);
drawApplication = app;
app.setDrawPanel(this); /*ここを導入したけれど,こういうプログラムの場合,drawApplication.setDrawPanel(this);の方が正解?*/
}
    • good
    • 0
この回答へのお礼

ありがとうございます。
なぜか、実行時に何も出なくて・・・。

今から、なぜそうなるのか検証してみます。
本当にありがとうございました。

お礼日時:2007/04/16 18:43

俺はJava初心者なので,あっているかどうかはわからない。

このプログラムが何するものか読んでないし。とりあえず警告やエラーの原因をつぶして行った。なお環境変数PATH,CLASSPATHは正しくセットされているものとする。あってるかどうか補足くれ。
========================DrawMain.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("セットされました"); /* 元のプログラムでは一回も経由していない・・・A */
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(); /* 上記Aの理由からdrawPanelがNullPointerExceptionで落ちる */
}

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をpaintする]
//
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とDrawPanelとDrawMouseListenerを作って組み立てる]
//
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);
}
}
    • good
    • 0
この回答へのお礼

あああ!!!!

なるほど!
何となく、わかりました!!!
ありがとうございます。
やり方も勉強します。

お礼日時:2007/04/16 18:45

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