• 締切済み

Swing実装での図形の追加と色の指定追加

_ranco_の回答

  • _ranco_
  • ベストアンサー率58% (126/214)
回答No.3

> 同様にfillRectやLineをつかって追加をしようとしましたがエラーが出ました > どのように行い挿入すればよいでしょうか LineはFigureのサブクラスを作ればよいですが、widthとheightの意味が違ってきますね(幅や高さではなく線の終端の座標になる)。fillRectは、Figureのサブクラスを新たに作るか、または、既存のRectangleFigureクラスにbooleanのフラグパラメータを設けて、paint()の中でifすればよいでしょう。 > 赤と緑のループになってしまい青が反映されなくなってしまいました > どこを修正すればよいでしょうか これは、このドジなループを見たら幼稚園児にでも分かるでしょう。 ◎図形の種類や色数が多いときは、ボタンではなくJComboBoxを使ってください。

関連するQ&A

  • 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
  • オブジェクト指向のエラーについてです。

    オブジェクト指向のエラーについてです。 初めて質問するので、至らない点があれば、スミマセンm(_ _)m コンパイルしようすると、 DrawPanelMouseHandler.java:10: シンボルを見つけられません。 シンボル: クラス DrawPanel 場所 : DrawPanelMouseHandler の クラス private DrawPanel drawPanel; と、なります。 上記と同様のエラーが上記を含め、9つ発生します。 どうしたらよいのでしょうか? 以下ソースです。 import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class DrawPanelMouseHandler extends MouseAdapter { private DrawPanel drawPanel; private ShapeFactory factory; private int x0; private int y0; private Shape shape; public DrawPanelMouseHandler(DrawPanel dp, ShapeFactory factory) { this.drawPanel = dp; this.factory = factory; } public DrawPanelMouseHandler(DrawPanel dp) { this.drawPanel = dp; } public void mousePressed(MouseEvent me) { x0 = me.getX(); y0 = me.getY(); shape = factory.newShape(x0, y0); drawPanel.addShape(shape); } public void mouseDragged(MouseEvent me) { int x = me.getX(); int y = me.getY(); shape.setBoundary(x0, y0, x, y); drawPanel.repaint(); } public void mouseReleased(MouseEvent me) { int x = me.getX(); int y = me.getY(); shape.setBoundary(x0, y0, x, y); drawPanel.removeShape(shape); Command cmd = new CommandAddShape(drawPanel, shape); drawPanel.execute(cmd); } public void setShapeFactory(ShapeFactory sf) { this.factory = sf; } } よろしくお願いします。

    • ベストアンサー
    • Java
  • MouseEventを使った時間計測について

    これまでJavaは使ったことないのですが、卒研で使うことになり今必死になってやっております。 早速ですが、今drawLineメソッドとMouseEventを使って自由曲線を描くものを作ってみました。 これに、mousePressed時に時間計測を開始し、mouseReleased時に一旦停止。 そして、それを足し合わせて合計時間を出したいのですが調べてもよくわかりません。よろしくお願いします。 下記は、現段階ものです。 ・J2SDK1.4.2.15 class DrawPanel extends Panel implements MouseListener,MouseMotionListener { Vector shapes = new Vector(); int x1,y1; public DrawPanel() { setForeground(Color.black); setBackground(Color.white); addMouseMotionListener(this); addMouseListener(this); } public void mousePressed(MouseEvent e) { e.consume(); x1 = e.getX(); y1 = e.getY(); shapes.addElement(new Rectangle(x1,y1,x1,y1)); paint(getGraphics()); } public void mouseDragged(MouseEvent e) { e.consume(); shapes.addElement(new Rectangle(x1,y1,e.getX(),e.getY())); x1 = e.getX(); y1 = e.getY(); paint(getGraphics()); } public void paint(Graphics g) { int number = shapes.size(); for (int i = 0;i < number;i++) { Rectangle data = (Rectangle)shapes.elementAt(i); g.drawLine(data.x,data.y,data.width,data.height); } } }

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

  • java初心者です。座標取得と図形表示について

    マウスをクリックするとその座標を表示するプログラムです。 画面には常に二つの四角形が表示されていて、どこをクリックしたかによって表示が変わるというものです。 (オレンジの四角形内をクリックした場合、 X:100 オレンジ Y:100 オレンジ のように表示します。) ですが、私が組んでみたプログラムでは図形表示が一瞬だけになってしまいます。 また、ウインドウの大きさを動かしてみると一瞬移っては消えてしまうという状態です。 それから、「オレンジ」などの日本語表示がうまくいかず「□□□」のように表示されてしまいます。 これらの問題を解決するにはどこをどう修正したらよいでしょうか? 以下が初心者なりに組んでみたソースです。 import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.Toolkit; import java.awt.Graphics2D; import java.awt.Graphics; import java.awt.Font; import java.awt.Rectangle; import java.lang.String; import java.lang.System; import javax.swing.JFrame; import javax.swing.JPanel; public class kadai33 extends JFrame { private static final long serialVersionUID = 1L; public kadai33() { add(new DrawPanel()); } public static void main(String[] args) { Toolkit.getDefaultToolkit().setDynamicLayout(true); JFrame f = new kadai33(); f.setTitle("マウスの座標を表示"); f.setDefaultCloseOperation(EXIT_ON_CLOSE); f.setBackground(Color.WHITE); f.setSize(400, 750); f.setVisible(true); } class DrawPanel extends JPanel implements MouseListener { int x; int y; public DrawPanel() { setBackground(Color.white); addMouseListener(this); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } public void paint(Graphics g) { Font font=new Font("Arial",Font.PLAIN,16); super.paintComponent(g); //super.paint(g); Graphics2D g1 = (Graphics2D)this.getGraphics(); Rectangle rect = new Rectangle(); rect.setRect(50,50,200,200); g1.setColor(Color.ORANGE); g1.fill(rect); Graphics2D g2 = (Graphics2D)this.getGraphics(); Rectangle rect2 = new Rectangle(); rect2.setRect(150,350,150,150); g2.setColor(Color.GREEN); g2.fill(rect2); Graphics2D g3 = (Graphics2D)this.getGraphics(); Rectangle rect3 = new Rectangle(); rect3.setRect(50,650,200,50); g3.setColor(Color.BLACK); g3.fill(rect3); if(x>=50 && x<=250 && y>=50 && y<=250){ super.paintComponent(g); String sx = String.format("X:%d", x); String sy = String.format("Y :%d", y); g.setFont(font); g.setColor(Color.BLACK); g.drawString(sx, 65, 670); g.drawString(sy, 65, 690); Graphics2D g4 = (Graphics2D)g; g4.setFont(font); g4.setColor(Color.WHITE); g4.drawString("オレンジ",120,670); g4.drawString("オレンジ",120,690); } if(x>=150 && x<=300 && y>=350 && y<=500){ super.paintComponent(g); String sx = String.format("X:%d", x); String sy = String.format("Y :%d", y); g.setFont(font); g.setColor(Color.BLACK); g.drawString(sx, 65, 670); g.drawString(sy, 65, 690); Graphics2D g5 = (Graphics2D)g; g5.setFont(font); g5.setColor(Color.WHITE); g5.drawString("グリーン",120,670); g5.drawString("グリーン",120,690); } else{ super.paintComponent(g); String sx = String.format("X:%d", x); String sy = String.format("Y :%d", y); g.setFont(font); g.setColor(Color.BLACK); g.drawString(sx, 65, 670); g.drawString(sy, 65, 690); Graphics2D g6 = (Graphics2D)g; g6.setFont(font); g6.setColor(Color.WHITE); g6.drawString("対象外",120,670); g6.drawString("対象外",120,690); } } } } 大変困っています。 どうかよろしくお願いいたしますm(__)m

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

     アプレットで描画してもウィンドウをリサイズすると、描画が消えてしまいます。  これのどこに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; } } }

    • ベストアンサー
    • Java
  • Javaのアプレットについて質問です。

    星型の図形をアプレットビューワ上でマウスを押した場所に移動させたいのですが・・・ import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Polygon; public class Enshu1_5 extends Applet implements MouseListener { int px []={100,80,10,70,40,100,160,130,190,120}; int py []={10,75,75,113,190,140,190,113,75,75}; private Polygon poly = new Polygon(px,py,10 ); public void init() { addMouseListener(this); } 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 paint(Graphics g) { g.setColor(Color.red); g.fillPolygon(poly); g.setFont(new Font("serif",Font.BOLD,24)); g.drawString("Star",100,200); } } こんな感じでつくってみたのですが現在位置とクリック位置との差分割り出し方をpublic void mouseClicked(MouseEvent e)の所に書きたいのですが普通にpoly.translate( e.getX(), e.getY() );をしてしまうと原点からの移動になってしまうため、フィールド上にint x=0,y=0をつくりmouseClickedのところで if(e.getX()>x&&e.getY()>y) { int dx=e.getX()-x; int dy=e.getY()-y; x=dx; y=dy; poly.translate(dx,dy); repaint(); }の様な感じで考えられる条件を条件分岐していこうと思ったのですがどうもうまくいきませんしかなりかさばったものになってしまうと思います。何かいい方法はないでしょうか? 教えて下さい><

    • ベストアンサー
    • Java
  • ペイントソフトを作っているのですが・・・

    簡単な絵を書くプログラムを下のようにつくりました。 こんなようなソースが公開されているサイトをご存知のかたは教えていただけないでしょうか?? おねがいします!! import java.awt.*; import java.awt.event.*; public class mouse5 extends Frame implements MouseListener , MouseMotionListener{ //グローバル変数 int x0,y0; Color objectColor = Color.red; //コンストラクタ public mouse5(){ setSize(400,300); //マウスイベント addMouseListener(this); addMouseMotionListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } //paint public void paint(Graphics g){ } //マウスが押されたら public void mousePressed(MouseEvent e){ //始点の座標 x0 = e.getX(); y0 = e.getY(); } //マウスが放されたら public void mouseReleased(MouseEvent e){ } //マウスがクリックされたら public void mouseClicked(MouseEvent e){ } //マウスが画面に入ったら public void mouseEntered(MouseEvent e){ } //マウスが画面から出たら public void mouseExited(MouseEvent e){ } //マウスがドラックされたら public void mouseDragged(MouseEvent e){ //線を引く Graphics g = getGraphics(); g.drawLine(x0,y0,e.getX(),e.getY()); g.dispose(); //次のために始点を更新 x0 = e.getX(); y0 = e.getY(); } //マウスが動いたら public void mouseMoved(MouseEvent e){ } //main public static void main(String[] args) { Frame w = new mouse5(); w.show(); } }

    • ベストアンサー
    • Java
  • ペイントソフトでキャンバス内にだけにかけるようにしたいです。

    どうしたらいいのかわからいので、何かヒントでiいので教えてください。 import java.applet.*; import java.awt.*; import java.awt.event.*; /* <APPLET CODE ="Mous" WIDTH = 500 HEIGHT = 300> </APPLET> */ public class Mous extends Applet { Graphics g; int point_x, point_y; Button color_black,color_red,color_blue,clear; public void init() { g = getGraphics(); setLayout(null); addMouseListener(new MouseAdapter () { public void mousePressed(MouseEvent e){ point_x = e.getX(); point_y = e.getY(); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { g.drawLine(point_x,point_y,e.getX(),e.getY()); point_x = e.getX(); point_y = e.getY(); } }); color_black = new Button("黒"); color_black.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.setColor(Color.black); } }); color_red = new Button("赤"); color_red.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.setColor(Color.red); } }); color_blue = new Button("青"); color_blue.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.setColor(Color.blue); } }); clear = new Button("クリアー"); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { g.clearRect(0,0,500,300); } }); add(color_black); add(color_red); add(color_blue); add(clear); } }