初心者が三角形を描く方法

このQ&Aのポイント
  • 初心者が三角形を描く方法について教えてください
  • Triangleクラスにおいて、g.fillTriangle()の引数に何を指定すれば良いのか分かりません
  • お願いします
回答を見る
  • ベストアンサー

初心者です 三角形を描きます

class Triangle { Paint pat; int xpoints0, ypoints0, xpoints1, ypoints1, xpoints2, ypoints2; public Triangle(Paint p, int x0, int y0, int x1, int y1, int x2, int y2) { pat = p; xpoints0 = x0; ypoints0 = y0; xpoints1 = x1; ypoints1 = y1; xpoints2 = x2; ypoints2 = y2; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillTriangle( ); } } ここまでは書けたのですが、一番最後の g.fillTriangle( ); で、( )に何を書いたらいいのかが、分かりません。 教えてください!お願いします。

  • Java
  • 回答数2
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • ssr-y6
  • ベストアンサー率71% (5/7)
回答No.2

 以下のように追加/変更すれば三角形が表示されます。 import java.awt.*; import java.awt.event.*; class Triangle { Paint pat; int xpoints0, ypoints0, xpoints1, ypoints1, xpoints2, ypoints2; public Triangle(Paint p, int x0, int y0, int x1, int y1, int x2, int y2) { pat = p; xpoints0 = x0; ypoints0 = y0; xpoints1 = x1; ypoints1 = y1; xpoints2 = x2; ypoints2 = y2; }; public void draw(Graphics2D g) { int x[] = {xpoints0, xpoints1, xpoints2}, y[] = {ypoints0, ypoints1, ypoints2}; g.setPaint(pat); // g.fillTriangle( ); g.fillPolygon(x, y, 3); }; } class drawtri extends Frame { Triangle T; public drawtri() { super("Draw Triangle"); setSize(200, 200); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); }; }); Paint pa = new GradientPaint((float)0.0, (float)0.0, Color.YELLOW, (float)0.0, (float)10.0, Color.GREEN, true); T = new Triangle(pa, 100, 50, 50, 150, 150, 150); }; public void paint(Graphics g) { T.draw((Graphics2D)g); }; public void update(Graphics g) { paint(g); }; public static void main(String args[]) { new drawtri(); }; }

その他の回答 (1)

  • HarukaV49
  • ベストアンサー率53% (48/89)
回答No.1

他の方からもアドバイスされていて認識されているかと思われますが、 もう少し基本的なJavaのプログラミングから勉強を始められることを 私としてもお勧めします。 >g.fillTriangle( ); このようなメソッドを書いておられますが、Graphics2Dクラスには、 fillTriangle(...)というメソッドはありません。 これは、ご覧になっているサンプルプログラムの中で、Graphics2Dを継承または、 コンポジション実装して、fillTriangle(...)というメソッドを 独自に実装しているのだと思われます。 ですから、このメソッドに引数として何を渡すかはその作成された(部分の) プログラム(gの実体)がここでは全く分かりませんので誰も答えようがありません。 (少し不安なのは、サンプルプログラムならば、点を保持するために、  なぜjava.awt.geom.Point2Dクラスを利用していないかというところです。  サンプルとしては、ちょっとお粗末な気がしてしまいます。私の仮定が  正しくない可能性も十分にありますのでご了承ください) まずは、私としては次の3つのことお勧めします。 (1)統合開発環境を入手してその上でプログラミングをしてください。    (今時、クラス名やメソッド命をキーボードから逐一打ち込んだり、     javacコマンド等をコマンドラインで使ったりする利点は何もありません) (2)JavaDocを読む練習を行ってください。 (3)数値計算クラスを作って、数値を渡してその数値を処理(計算)して    値が戻ってくるような基本的なメソッドの書き方の練習から行ってください。    (その際、そのクラスを使って、インターフェースを書いたり、クラスを    継承したり、コンポジション実装したり、メソッドをオーバーライド    したりする方法もマスターしてください) (1)とは、Eclipse等のソフトウェアです。これらのエディターは入力と同時に 変数名のチェックやメソッドの入力アシスタント等を行ってくれますので 少なくても現在投稿されているレベルのミスは(コンパイルするまでもなく) 入力と同時に検出してくれます。 (2)とは、JavaDocは使用するクラスの冒頭部と当該メソッドの部分には 必ず目を通す必要があるということです。また、自分で作成された クラスに関しは、自身でJavaDocを書いておく必要もあります。 (統合開発環境を使えば、簡単に該当するJavaDovは開けます。  またJavaDoc作成時のアシスタント機能も充実しています) (3)とは、グラフィックス関係のプログラミングは、初心者でなくても 簡単な分野ではありません。というのも、デバッガでプログラムを途中で 停止させても最終的に目的の図形が画面に表示されないというような 問題のバグは、(想像できるでしょうが)非常に対処が難しいからです。 数値計算等で、手計算で処理過程を追っていけるようなプログラミングにおいて デバッガを使ってプログラミングの中を自由に飛び廻ってバグフィックスが できるようになってからグラフィックスプログラミングに挑戦されることが 断然近道であると思われます。 ここから先は、ご自身が初心者を脱したと思ったときに読んで下さい。 オブジェクト指向設計的には、g.fillTriangle(...)というように Graphics2Dクラスを継承したクラスに新たなメソッドを追加するよりも g.fill( new Triangle(...) )という風にメソッドを追加することなく Shapeインターフェースを実装(するためGeneralPathクラスをコンポジション実装) したTriangleクラスを作成してネイティブメソッドで描画させる方法が 格段に優れていると思われます。 この意味が何となくでも理解できれば、初心者は卒業でしょう。

関連するQ&A

  • 初心者です。 コンパイルのエラー

    import java.awt.*; import javax.swing.*; public class R11Sample1 extends JFrame { Rect r1 = new Rect(Color.red, 100, 100, 80, 60); Rect r2 = new Rect(new Color (0.5f, 1f, 0f, 0.7f), 150, 120, 60, 90); Oval = new Oval(Color.blue, 60, 50, 10, 10); JPanel panel = new JPanel() { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; r1.draw(g2); r2.draw(g2); } }; public R11Sample1() { setSize(400, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().add(panel); } public static void main(String[] args) { new R11Sample1().setVisible (true); } } class Rect { Paint pat; int xpos, ypos, width, height; public Rect(Paint p, int x, int y, int w, int h) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillRect(xpos-width/2, ypos-height/2, width, height); } } class Oval { Paint pat; int xpos, ypos, radius; public Oval(Paint p, int x, int y, int width, int height) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillOval(xpos-width/2, ypos-height/2, width, height); } } これでコンパイルすると、 Identifierがありません といわれました。 どこを直せばいいのでしょうか。 また、全体的に間違ったところがあったら教えてください。

    • ベストアンサー
    • Java
  • 初心者です。Circle

    class Circle { Paint pat; int xpos, ypos, radius; public Circle(Paint p, int x, int y, int r) { pat = p; xpos = x; ypos = y; radius = r; } public void draw(Graphics2D g) { g.setPoint(pat); g.fillCircle(xpos-radius, ypos-radius, radius); } } というソースを作りましたが、 コンパイルの際に 以下のエラーが出ます。 シンボルを見つけられません シンボル:メソッド fill Circle(int,int,int) 場所:g.fillCircle(xpos-radius, ypos-radius, radius); どこが間違っていて、どう直せばいいのか、教えてください。 お願いします。

  • f(x)=xのf'(x)と∫f(x)dxのグラフ化

    f(x)=xのf'(x)と∫f(x)dxのグラフ化したいのですがここからどうすればいいのか分からず足踏みしてます。ご教授ください。 積分区間は0~xです。 import java.awt.*; import java.awt.event.*; public class graphics0 extends Frame { // コンストラクタ public graphics0(){ setSize(500, 500); setVisible(true); setTitle("graphics0"); // メッセージ処理 addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } // メインメソッド public static void main(String[] args){ new graphics0(); } // ペイントメソッド public void paint(Graphics g){ //ここに絵をかくコードを書く // 中心座標 int ox = 250; int oy = 250; // グラフの幅 int width = 200; int height = 200; //直線描画のための2点(x0,y0), (x1, y1) double x0, y0; double x1, y1; // 刻み幅 double d = 0.001; double n = (int) (1.0 / d); // 軸の描画 g.drawLine(50, 250, 450, 250); g.drawLine(250, 50, 250, 450); x0 = 0; y0 = 0; x1 = d; for(int i = 0; i < n; i++){ y1 = x1 * x1; g.drawLine( (int) (x0 * width) + ox, -((int)(y0 * height) -oy), (int) (x1 * width) + ox, -((int) (y1 * height) - oy)); x0 += d; x1 += d; y0 = y1; } } } よろしくお願いいたします。

  • クラスメソッドのみのクラスのオブジェクト生成は不可??

    あるテキストのjavaの問題です。 public class Draw{   static void pixel(int x,int y){     /*座標(x、y)に点を描画*/   }   static void line(int x1,int y1,int x2,int y2){     /*座標(x1、y1)~(x2、y2)に線を引く*/   } } で、これを実行するための以下のようなクラス public class TestDraw{ <ここに入力> } という問題なのですが2つまでは絞れたのですが、 (1) public static void main(String args[]){   Draw d = new Draw().line(10,10,20,30); } ↑× (2) public static void main(String args[]){   Draw.line(10,10,20,30); } ↑○ (2)はlineメソッドがstaticメソッドだからオブジェクト生成しなくても良い、ということなんですが (1)も正解のような気がするのですが・・・ 解説によると「lineはvoidなのでnew Draw().line(10,10,20,30);とすれば正解、とあります。 どうもいまいち理解できません。 クラスメソッドはオブジェクト生成しなくとも良い→オブジェクト生成できない ということなのでしょうか? それからちなみに、public classって2つ記述できないんではありませんでしたか?

    • ベストアンサー
    • Java
  • 線の太さを変える コードのエラの意味が分かりません

     java勉強中の初心者です、宜しくお願いします。  下のようなコードを書きましたが、  >DrawPanel dp = new DrawPanel( X1 , Y1 , X2 , Y2 ) ;  >g2.setStroke( の二か所でエラーが出ています。 (エラーの内容はよく理解できません。) 一体どこが間違っているのでしょうか宜しくお願いします。 ================================================================= public class drawLine extends JApplet { int X1 = 20 , Y1 = 20 , X2 = 150 , Y2 = 150 ; public void init() { DrawPanel dp = new DrawPanel( X1 , Y1 , X2 , Y2 ) ; // dp.setSize( 30 , 200 ) ; // dp.setBackground( Color.cyan ) ; this.add( dp ) ; this.setBounds( 10 , 10 , 400 , 400 ) ; this.setBackground( Color.cyan ) ; this.setVisible( true ) ; } } //======================================================= class DrawPanel extends JPanel { Float currentWidth = 20.0f ; int x1 , y1 , x2 , y2 ; public DrawPanel( int x1 , int y1 , int x2 , int y2 ) { this.x1 = x1 ; this.y1 = y1 ; this.x2 = x2 ; this.y2 = y2 ; Graphics2D g2 = (Graphics2D)this.getGraphics() ; g2.setStroke ( new BasicStroke ( currentWidth , BasicStroke.CAP_ROUND , BasicStroke.JOIN_MITER ) ) ; g2.setColor( Color.red ) ; g2.drawLine( x1 , y1 , x2 , y2 ) ; } } ==============================================================================

  • Appletのrepaint()が動作しません

    import java.applet.*; import java.awt.*; /*<applet code="zxy" width="1000" height="1000"></applet>*/ class Object extends Applet { protected int x; /* オブジェクトのx座標 */ protected int y; /* オブジェクトのy座標 */ protected int width; protected int height; Image buffer; Graphics buffer_g; Dimension d = getSize(); public Object() { x = 0; y = 0; width = 0; height = 0; } } /* 楕円クラス */ class Oval extends Object implements Runnable { public Oval() { width = (int)(Math.random() * 56 + 10); height = (int)(Math.random() * 56 + 10); } public void run() { while( true ){ try{ repaint(); Thread.sleep(50); } catch( Exception e ){} } } public void paint( Graphics g ) { if( buffer_g == null ) buffer_g = buffer.getGraphics(); Dimension d = getSize(); buffer_g.setColor( Color.white ); buffer_g.fillRect( 0, 0, d.width, d.height ); buffer_g.setColor( Color.black ); buffer_g.drawOval( x, y, width, height ); x += (int)(Math.random() * 10); y += (int)(Math.random() * 10); g.drawImage( buffer, 0, 0, this ); } } public class A extends Applet { Image buffer; Graphics buffer_g; Oval ov1 = new Oval(); Thread thOv1 = new Thread( ov1 ); public void start() { thOv1.start(); Dimension d = getSize(); buffer = createImage( d.width, d.height ); } } 上記プログラムを実行しても、paint()の中が実行されずにtryブロックを繰り返すだけになってしまいます。paint()を実行するにはどうすればいいでしょうか?

  • 「初心者です」-Xlint: deprecation

    import java.awt.*; import java.applet.*; public class ani_ball extends Applet implements Runnable { Thread th_mvball = null; int X,Y; double x1,y1; double x2,y2; double dx,dy; Graphics g; public void init() { this.setBackground(new Color(150,245,255)); this.X = 250; this.Y = 250; this.setSize(X,Y); this.x1 = this.x2 = this.X/2; this.y1 = this.y2 = this.Y/2; this.dx = 3; this.dy = 2; } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval((int)(this.x2-3),(int)(this.y2-3),6,6); this.x1 = this.x2; this.y1 = this.y2; } public void start() { if(th_mvball == null) { th_mvball = new Thread(this); th_mvball.start(); } } public void run() { while(true) { try { this.move(); this.repaint(); Thread.sleep(10); } catch(InterruptedException e ) { this.stop(); } } } public void move() { if( y2 > Y ) { y2 = 2*Y - y1 - dy; dy = -dy; } else if ( y2 < 0) { y2 = -y1 - dy; dy = -dy; } else if ( x2 > X) { x2 = 2*X - x1 -dx; dx = -dx; } else if ( x2 < 0) { x2 = -x1 - dx; dx = -dx; } if( x2 < 0 ) { x2 = -x2 ; dx = -dx; } else if ( y2 < 0) { y2 = -y2; dy = -dy; } else if ( x2 > X) { x2 = 2*X - x2; dx = -dx; } else if ( y2 > Y) { y2 = 2*Y - y2; dy = -dy; } x2 = x1 + dx; y2 = y1 + dy; } public void stop() { if(th_mvball!=null) { th_mvball stop(); th_mvball=null; } } } ↑のプログラムをコンパイルすると、「-Xlint: deprecation オプションを指定して再コンパイルしてください」とエラーが出ます。 エラーの対処法、またはプログラムの訂正すべき箇所を教えてください。 よろしくお願いします。

    • ベストアンサー
    • Java
  • Swing の実装でどうしてもエラーになります。

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

    • ベストアンサー
    • Java
  • 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); } }

    • ベストアンサー
    • Java
  • 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); } }

専門家に質問してみよう