_ranco_ の回答履歴

全483件中181~200件表示
  • 自作ドローエディタコンパイル不能

    http://rg550.hp.infoseek.co.jp/cgi-bin/joyful/img/252.java これをコンパイルしようとすると終盤でシンボルFigureTypeが見つけられないとでてしまいます どこを修正・追加すればコンパイル可能になるでしょうか 追加部分の詳細な場所とソースも書いてくださると嬉しいです

    • 締切済み
    • ADY
    • Java
    • 回答数3
  • 自作ドローエディタコンパイル不能

    http://rg550.hp.infoseek.co.jp/cgi-bin/joyful/img/252.java これをコンパイルしようとすると終盤でシンボルFigureTypeが見つけられないとでてしまいます どこを修正・追加すればコンパイル可能になるでしょうか 追加部分の詳細な場所とソースも書いてくださると嬉しいです

    • 締切済み
    • ADY
    • Java
    • 回答数3
  • 自作ドローエディタコンパイル不能

    http://rg550.hp.infoseek.co.jp/cgi-bin/joyful/img/252.java これをコンパイルしようとすると終盤でシンボルFigureTypeが見つけられないとでてしまいます どこを修正・追加すればコンパイル可能になるでしょうか 追加部分の詳細な場所とソースも書いてくださると嬉しいです

    • 締切済み
    • ADY
    • Java
    • 回答数3
  • 描画してもウィンドウをリサイズすると描画が消える

     アプレットで描画してもウィンドウをリサイズすると、描画が消えてしまいます。  これのどこにrepaint()、update()、paint()を入れてやれば良いのでしょうか。    本格的にプログラムの勉強をしようと考えているのですが、大阪、京都付近でjava、cとかを(出来れば個人教授のような 感じで)教えてくれる、スクールをご存知でしたら是非教えて下さい。 ____________________________________________________________ public class Q_Draw_Line extends Applet implements MouseListener , ActionListener { Graphics g; Color col; Button btnyellow; int mode; Button btncircle; Button btndot; public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void init() { g = getGraphics(); btnyellow = new Button("黄"); this.add(btnyellow); btnyellow.addActionListener(this); btncircle = new Button("円"); this.add(btncircle); btncircle.addActionListener(this); btndot = new Button("点"); this.add(btndot); btndot.addActionListener(this); this.addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int x = e.getX(); // X座標取得 int y = e.getY(); // Y座標取得 g.setColor(col); switch(mode) { case 1 : g.drawOval(x-20/2,y-20/2,20,20);break; case 2 : g.fillRect(x,y,5,5);break; } } } ); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "黄") { col = Color.yellow; } if (e.getActionCommand() == "円") { mode = 1; } if (e.getActionCommand() == "点") { mode = 2; } } }

    • ベストアンサー
    • htgo
    • Java
    • 回答数9
  • 描画してもウィンドウをリサイズすると描画が消える

     アプレットで描画してもウィンドウをリサイズすると、描画が消えてしまいます。  これのどこにrepaint()、update()、paint()を入れてやれば良いのでしょうか。    本格的にプログラムの勉強をしようと考えているのですが、大阪、京都付近でjava、cとかを(出来れば個人教授のような 感じで)教えてくれる、スクールをご存知でしたら是非教えて下さい。 ____________________________________________________________ public class Q_Draw_Line extends Applet implements MouseListener , ActionListener { Graphics g; Color col; Button btnyellow; int mode; Button btncircle; Button btndot; public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void init() { g = getGraphics(); btnyellow = new Button("黄"); this.add(btnyellow); btnyellow.addActionListener(this); btncircle = new Button("円"); this.add(btncircle); btncircle.addActionListener(this); btndot = new Button("点"); this.add(btndot); btndot.addActionListener(this); this.addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int x = e.getX(); // X座標取得 int y = e.getY(); // Y座標取得 g.setColor(col); switch(mode) { case 1 : g.drawOval(x-20/2,y-20/2,20,20);break; case 2 : g.fillRect(x,y,5,5);break; } } } ); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "黄") { col = Color.yellow; } if (e.getActionCommand() == "円") { mode = 1; } if (e.getActionCommand() == "点") { mode = 2; } } }

    • ベストアンサー
    • htgo
    • Java
    • 回答数9
  • JTableについて 急

    皆さまのお知恵をお貸頂けると助かります。  JTableクラスを使ってテーブルの列の幅の値を決めたいのと、 数字をセルに入力したいのですが9までは09のように0をつけて 表示したいのですが、2日間止まってしまって困っています。 いろいろとググったりしたのですが 汗

  • 描画してもウィンドウをリサイズすると描画が消える

     アプレットで描画してもウィンドウをリサイズすると、描画が消えてしまいます。  これのどこにrepaint()、update()、paint()を入れてやれば良いのでしょうか。    本格的にプログラムの勉強をしようと考えているのですが、大阪、京都付近でjava、cとかを(出来れば個人教授のような 感じで)教えてくれる、スクールをご存知でしたら是非教えて下さい。 ____________________________________________________________ public class Q_Draw_Line extends Applet implements MouseListener , ActionListener { Graphics g; Color col; Button btnyellow; int mode; Button btncircle; Button btndot; public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void init() { g = getGraphics(); btnyellow = new Button("黄"); this.add(btnyellow); btnyellow.addActionListener(this); btncircle = new Button("円"); this.add(btncircle); btncircle.addActionListener(this); btndot = new Button("点"); this.add(btndot); btndot.addActionListener(this); this.addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int x = e.getX(); // X座標取得 int y = e.getY(); // Y座標取得 g.setColor(col); switch(mode) { case 1 : g.drawOval(x-20/2,y-20/2,20,20);break; case 2 : g.fillRect(x,y,5,5);break; } } } ); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "黄") { col = Color.yellow; } if (e.getActionCommand() == "円") { mode = 1; } if (e.getActionCommand() == "点") { mode = 2; } } }

    • ベストアンサー
    • htgo
    • Java
    • 回答数9
  • ファイル読み込みを配列に入れる方法

    ファイルから数字を読み込んで 並び替えて出力するプログラムを作成しているのですが ファイルから一度に配列に取り込んで、その配列で並び変えたいのですが取り込みができないのです、int型の配列に一度に取り込むのは可能なのでしょうか? import java.io.*; class Sort { public static void main(String[] args) { String path = args[0]; File file = new File(path); FileReader fr = null; int[] buf = new int[(int)file.length()]; try { fr = new FileReader(file); fr.read(buf); for (int i=0; i<buf.length-1;i++) { int mini=buf[i]; int miniIndex = i; for(int j = i+1; j<buf.length;j++){ if(buf[j] < mini){ mini = buf[j]; miniIndex = j; } } buf[miniIndex] = buf[i]; buf[i] = mini; } for(int i =0; i<buf.length;i++) { System.out.print( buf[i]+","); } } catch (IOException e) { System.out.println("ファイル入力エラー"); } } } と作ってみたのですが‥ ご教授お願いいたします。

    • ベストアンサー
    • huhumu
    • Java
    • 回答数2
  • ファイル読み込みを配列に入れる方法

    ファイルから数字を読み込んで 並び替えて出力するプログラムを作成しているのですが ファイルから一度に配列に取り込んで、その配列で並び変えたいのですが取り込みができないのです、int型の配列に一度に取り込むのは可能なのでしょうか? import java.io.*; class Sort { public static void main(String[] args) { String path = args[0]; File file = new File(path); FileReader fr = null; int[] buf = new int[(int)file.length()]; try { fr = new FileReader(file); fr.read(buf); for (int i=0; i<buf.length-1;i++) { int mini=buf[i]; int miniIndex = i; for(int j = i+1; j<buf.length;j++){ if(buf[j] < mini){ mini = buf[j]; miniIndex = j; } } buf[miniIndex] = buf[i]; buf[i] = mini; } for(int i =0; i<buf.length;i++) { System.out.print( buf[i]+","); } } catch (IOException e) { System.out.println("ファイル入力エラー"); } } } と作ってみたのですが‥ ご教授お願いいたします。

    • ベストアンサー
    • huhumu
    • Java
    • 回答数2
  • 描画してもウィンドウをリサイズすると描画が消える

     アプレットで描画してもウィンドウをリサイズすると、描画が消えてしまいます。  これのどこにrepaint()、update()、paint()を入れてやれば良いのでしょうか。    本格的にプログラムの勉強をしようと考えているのですが、大阪、京都付近でjava、cとかを(出来れば個人教授のような 感じで)教えてくれる、スクールをご存知でしたら是非教えて下さい。 ____________________________________________________________ public class Q_Draw_Line extends Applet implements MouseListener , ActionListener { Graphics g; Color col; Button btnyellow; int mode; Button btncircle; Button btndot; public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void init() { g = getGraphics(); btnyellow = new Button("黄"); this.add(btnyellow); btnyellow.addActionListener(this); btncircle = new Button("円"); this.add(btncircle); btncircle.addActionListener(this); btndot = new Button("点"); this.add(btndot); btndot.addActionListener(this); this.addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int x = e.getX(); // X座標取得 int y = e.getY(); // Y座標取得 g.setColor(col); switch(mode) { case 1 : g.drawOval(x-20/2,y-20/2,20,20);break; case 2 : g.fillRect(x,y,5,5);break; } } } ); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "黄") { col = Color.yellow; } if (e.getActionCommand() == "円") { mode = 1; } if (e.getActionCommand() == "点") { mode = 2; } } }

    • ベストアンサー
    • htgo
    • Java
    • 回答数9
  • 描画してもウィンドウをリサイズすると描画が消える

     アプレットで描画してもウィンドウをリサイズすると、描画が消えてしまいます。  これのどこにrepaint()、update()、paint()を入れてやれば良いのでしょうか。    本格的にプログラムの勉強をしようと考えているのですが、大阪、京都付近でjava、cとかを(出来れば個人教授のような 感じで)教えてくれる、スクールをご存知でしたら是非教えて下さい。 ____________________________________________________________ public class Q_Draw_Line extends Applet implements MouseListener , ActionListener { Graphics g; Color col; Button btnyellow; int mode; Button btncircle; Button btndot; public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void init() { g = getGraphics(); btnyellow = new Button("黄"); this.add(btnyellow); btnyellow.addActionListener(this); btncircle = new Button("円"); this.add(btncircle); btncircle.addActionListener(this); btndot = new Button("点"); this.add(btndot); btndot.addActionListener(this); this.addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { int x = e.getX(); // X座標取得 int y = e.getY(); // Y座標取得 g.setColor(col); switch(mode) { case 1 : g.drawOval(x-20/2,y-20/2,20,20);break; case 2 : g.fillRect(x,y,5,5);break; } } } ); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "黄") { col = Color.yellow; } if (e.getActionCommand() == "円") { mode = 1; } if (e.getActionCommand() == "点") { mode = 2; } } }

    • ベストアンサー
    • htgo
    • Java
    • 回答数9
  • awtでGUIを作成しているのですがイベントリスナーのことで

    こんばんは。今awtでGUIを作成しています。 テキストフィールドを作成し、入力したあとエンタ―ボタンを押したら桁数チェックなどを行い、妥当な入力であればカーソルを次のテキストフィールドに移動させ、妥当でなければ警告のダイアログボックスをだすアクションリスナーをaddActionListenerで作っていたのですが、 入力を確定させるのは何もエンターキーだけではなく、人によってはタブキーとか次のテキストボックスを選択して確定する人もいるだろうと思い、addFocuslistenerも加えました。以下がその部分です。 field1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ae){ String gt_field1=field1.getText(); if(gt_field1.length()==5){keycheck();} else{Tyuui("無効な入力です","5桁の文字を入力してください。");} } } ); field1.addFocusListener( new FocusListener(){ public void focusLost(FocusEvent e){ String gt_field1=field1.getText(); if(gt_field1.length()==5){keycheck();} else{Tyuui("無効な入力です","5桁の文字を入力してください。");} } } ); しかしこのようにしたら、エンターキーを押した時、妥当な入力だったら次のテキストフィールドに移動させるようにしているので、Focuslistenerも反応し、もう一度同じ処理をしてしまいました。妥当な入力でないときは警告のダイアログが2回でてきました。 そこでaddActionListenerのほうにはなにも調べさせず、フォーカスをはずすだけにしたらどうかと思ったのですが、requestFocus()もありますが他のテキストフィールドにフォーカスを当てたらまたおかしな事になりそうなのでただフォーカスをはずし、どこにもフォーカスがあっていない状態にしたいのですが、いい方法とかございましたら教えてはいただけませんか。 またEnterキーを押しただけでなく、Tabキーを押したり、マウスで別のところをクリックしてフォーカスが外れた時も桁チェックなどを行い、妥当な桁なら次のテキストフィールドにフォーカスをあてるいい方法などございましたらアドバイスの方、おねがいします。

    • ベストアンサー
    • ossu
    • Java
    • 回答数1
  • appletの同時実行について

    今appletで適当なアニメーションをeclipse上で作っています。 そこで質問です。 「inu」というクラスと「neko」というクラスがあり それぞれ個別ならば、appletで動かせるのですが 二つをそれぞれ別のウインドウで動かそうとしたいのですが 以下の方法では出来ませんでした。 方法 適当に「animal」クラスを作成し(これは普通のappletではないクラス) その中のmain()の中で それぞれinu,nekoのクラスをnewしてから それぞれinu.init() neko.init()をしました。 しかしアプリケーションとしてしか動きません。 それぞれのクラスを一斉に各ウインドウでappletとして動かすには いい手はあるのでしょうか?

  • 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); } }

  • appletの同時実行について

    今appletで適当なアニメーションをeclipse上で作っています。 そこで質問です。 「inu」というクラスと「neko」というクラスがあり それぞれ個別ならば、appletで動かせるのですが 二つをそれぞれ別のウインドウで動かそうとしたいのですが 以下の方法では出来ませんでした。 方法 適当に「animal」クラスを作成し(これは普通のappletではないクラス) その中のmain()の中で それぞれinu,nekoのクラスをnewしてから それぞれinu.init() neko.init()をしました。 しかしアプリケーションとしてしか動きません。 それぞれのクラスを一斉に各ウインドウでappletとして動かすには いい手はあるのでしょうか?

  • Javaのタイムゾーン設定

    お世話になります。 現在Javaで動いているアプリがありまして、 日付の処理で異常がありました。 どうやらOSの日付よりも-9時間のようです。 グリニッジくさいと思ったので、 OSの設定を見たところ、OS側は GMT+9:00 の東京になっていました。 Javaでjava.util.TimeZoneを使って調べたところ、 GMT グリニッジ標準時 オフセット0となりました。 どうやらJava側のタイムゾーンだけずれているようなのですが、 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); } }

  • メッソドをクラスにしたときの引数等の渡し方を教えて下さい

     JAVA初心者です、よろしくお願いします。  簡単な計算を1つのクラスの中でメッソドに計算をさせ、それをメインメッソドに返してやるプログラムで、 そのメッソドをクラス化してやる方法が分かりません。  例えば「表示クラス」は、値を「計算クラス」に渡して計算をさせ、「表示クラス」はその答えだけを受け取り、表示。 「計算クラス」は計算だけをするコードを書きたいのです。  プログラム全体の流れは分かるのですが、メッソドをクラス化した場合の引数の渡し方、受け取り方、戻り値の受け 取り方等をどのように書くのかが分かりません。  何方か、簡単な足し算の例などで、宜しくお願いします。

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

  • 「car」のクラス図が描けなくて困っています。

    「car」のクラス図が描けなくて困っています。 大まかな流れは以下なのですが、間違っているみたいなので何処を直せばよいのか指摘をお願いします。 [car] ------↑-------- ↑  ↑   ↑ [body][engine][frame]