• 締切済み

線の太さを変える コードのエラの意味が分かりません

Tacosanの回答

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.2

本当にそれだけしか表示されないのですか? その前に何かありませんか?

関連する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
  • 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); } }

  • 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
  • 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; } } } よろしくお願いいたします。

  • オブジェクト指向のエラーについてです。

    オブジェクト指向のエラーについてです。 初めて質問するので、至らない点があれば、スミマセン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
  • 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
  • 「初心者です」-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
  • javaの描画の取り消し(消去)に関する質問

    毎度、お世話になります。 Q1)下記は、描画のサブルーチンですが、この中で、3個の描画(各々、A,B,C)を 行なっていますが、この内の1この描画(例えば、C)を取り消す事は可能ですか? static void drawCanvas_sub(Graphics2D g2){ BasicStroke wideStroke = new BasicStroke(3.0f); BasicStroke normalStroke = new BasicStroke(1.0f); g2.setStroke(wideStroke); g2.draw(new Line2D.Double(30.0d, 120.0d, 250.0d, 140.0d)); //A ********* g2.setStroke(normalStroke); g2.drawLine(0,100,520,100); //B *********** g2.drawRect(0, 0, 520, 380); //C ********** } 以上

    • ベストアンサー
    • Java
  • javaアプレットでのy=x^2のグラフ

    javaアプレットでy=x^2のグラフを描写したいのですが、現在の状態ではグラフの右側しか画面に現れません。軸の移動をすればよいと思うのですが、どのように修正すればよいのでしょうか? import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; public class test1 extends Applet{ public void paint(Graphics g){ g.setColor(Color.red); int x,yp=100,xp=0; for(x=0;x<200;x++){ int y=(int)(x*x)*(-1)+100; g.drawLine(xp,yp,x,y); xp = x; yp = y; } } }

  • javaで太さのない線を描きたい

    javaプログラム内で、太さ0pxの線を描画するにはどうすればいいのでしょうか。 つまり’描画されない’透明な線を描画したいのですが・・・ setStrokeを用いて ((Graphics2D)g).setStroke(new BasicStroke(0)); で設定してみたのですが、どうも気を利かせて(?)細い線を描画してしまうようです。 解る方回答お願いします。