![](http://oshiete.xgoo.jp/images/v2/pc/qa/question_title.png?e8efa67)
初心者ですみませ。
次のリストがどうしてコンパイルを通っても実行時にエラーになってしまいます。どなたか、判る方原因を教えてください。
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);
}
}
No.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を使えば、クラス変数に対して行っている処理、
つまりクラス全体に関わってる処理なんだろう、とすぐに分かると思われます。
詳しい説明ありがとうございます。
また、言葉足らずですみませんでした。
継承や抽象などをたくさん組み合わせているうちに、何がなんだかわからなくなってしまい・・。
コンパイル時のエラーも無く、私の拙い知識では一見問題ないように思われたのですが、マウスクリックで表示されるはずの四角がでず。
変わりにAWT関連のエラーらしきものが表示されていたもので。
自分が書いたプログラムでの構文エラーならなんとか読みこなせたんですけど、一気にチンぷんカンプンになってしまったんです。
お蔭様で、一気に霧が晴れてきた感じでした。ありがとうございました。
No.2
- 回答日時:
あ、念のためにこの後で回答してくれる識者様が居られたら,個人的に聞きたいこと
public DrawPanel(DrawApplication app) {
setBackground(Color.white);
drawApplication = app;
app.setDrawPanel(this); /*ここを導入したけれど,こういうプログラムの場合,drawApplication.setDrawPanel(this);の方が正解?*/
}
ありがとうございます。
なぜか、実行時に何も出なくて・・・。
今から、なぜそうなるのか検証してみます。
本当にありがとうございました。
No.1
- 回答日時:
俺は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);
}
}
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Java java 入力 3 4 3 出力 ABC DEFG HIJ このようなプログラムの書き方を教えてくだ 2 2022/07/15 14:18
- Java Java プログラム public class Main { public static void 3 2023/08/10 23:46
- Ruby 【JAVA】数字をひし形に出力するプログラムについて 2 2022/07/11 23:32
- C言語・C++・C# 大量のデータを読み込んで表示する速度を改善したい 8 2023/05/07 13:29
- Java javaでのプログラム(配列)について質問です. 2 2022/10/14 22:27
- Java 直し方について教えて頂きたいです。 4 2022/08/13 02:11
- C言語・C++・C# C++プログラミングコードにポリモーフィズムを取り入れ方を教えてください。 2 2023/06/09 11:17
- Java Java 配列<選挙> 4 2023/07/31 15:07
- 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でcsvファイル読込時の改行...
-
javaで電卓を作りたいのですが...
-
Java 初心者 int型の取り扱い方
-
「配列定数は、イニシャライザ...
-
C#で「型または名前空間名 'int...
-
intが負の時に投げる例外はあり...
-
JUnit4のアノテーションについて
-
コード中の謎のエラー
-
Swing実装での図形の追加と色の...
-
JAVAでCの関数ポインタのような...
-
java初心者の質問(3つの整数の...
-
Swing の実装でどうしてもエラ...
-
式の型は配列型で int に解決済...
-
ダブルクォーテーションのrepla...
-
javaで質問です。 文字列2023/2...
-
java junit list同士の比較方法
-
VBAで配列の計算
-
[JAVA]try 内の変数を外で!?
-
JSPやサーブレットでSystem.out...
-
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型の取り扱い方
おすすめ情報