• 締切済み

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

 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
  • 回答数8
  • ありがとう数5

みんなの回答

  • tekebon
  • ベストアンサー率62% (36/58)
回答No.8

#5の回答にありますがinit()メソッドはアプレットが起動する前に初期化するタイミングとして 呼び出されるメソッドです。この段階でGraphicsの情報は取得できません。 アプレットを描画するタイミングではpublic void paint(Graphics g)が呼び出されます。 このpaint()メソッドは「何もしない」という実装になっているので、drawLineクラスで paint()メソッドをオーバーライドし、DrawPanelの表示を指示します。 public void paint(Graphics g){ dp.paint(g); } ※DalwPanelのオブジェクトdpが必要になりますのでinit()メソッドのローカル変数でなく DrawLineクラスのメンバ変数として宣言する必要があります。 またアプレットではpaint()メソッドを直接呼び出すのではなく、repaint()メソッドを 呼びます。repaint()メソッドがpaint()メソッドを呼び出し、適切なGraphicsオブジェクトを 渡してくれます。

dikon_007
質問者

お礼

 tekebonさん、回答有難うございました。  別の方法により、線を太くすることが出来ましたが、 ある特定の線だけに 「g2.drawLine( x , 200-100 , 350 , 150-100 ) ;」としてやりましたが、 全ての線に適用され全てが同じ線の太さになってしましました。 各線毎に太さを変えるためにはどのようにコードを書いてやれば 良いのでしょうか、宜しくお願いします。 ================================================================================= public class shachou extends Applet { MyCanvas mc ; public void init() { this.setBackground( Color.yellow ) ; this.setLayout( null ) ; this.setSize( 1000,400 ) ; this.setLocation( 150,150 ) ; mc = new MyCanvas(); mc.setLocation( 50 , 50 ) ; this.add(mc); this.setVisible(true); } } class MyCanvas extends Canvas { Graphics2D g2; MyCanvas() { this.setBackground( Color.cyan ) ; this.setSize( 500 , 300-125 ) ; } public void paint( Graphics g ) { int x ; g2 = (Graphics2D)g ; g2.setStroke(new BasicStroke(3.0f)) ; for( x = 50 ; x <= 250 ; x += 20 ) { g.setColor(Color.yellow); //scue g.drawLine( x , 200-100 , 150 , 150-100 ) ; } for( x = 250 ; x <= 450 ; x += 20 ) { g.setColor(Color.black); //scue g2.drawLine( x , 200-100 , 350 , 150-100 ) ; } g.setColor(Color.black); g.drawLine( 40 , 205-100 , 460 , 205-100 ) ; //base g.setColor(Color.magenta); //base g.drawLine( 40 , 198-100 , 460 , 198-100 ) ; for( x = 148 ; x <= 152 ; ++ x ) { g.setColor(Color.red); //virtical g.drawLine( x , 230-100 , x , 135-100 ) ; } for( x = 348 ; x <= 352 ; ++ x ) { g.setColor(Color.magenta); //virtical g.drawLine( x , 230-100 , x , 135-100 ) ; } } } ================================================================

  • askaaska
  • ベストアンサー率35% (1455/4149)
回答No.7

Graphics2D g2 = (Graphics2D)this.getGraphics() ; が悪い これが理解できるようになると 描画の仕組みが分かるかもしれないわ

  • askaaska
  • ベストアンサー率35% (1455/4149)
回答No.6

んー ここは教えてあげないと厳しいかしらね 次のメソッドをHINTにするといいわ @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(currentWidth, BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER)); g2.setColor(Color.red); g2.drawLine(x1, y1, x2, y2); } このpaintというメソッドのオーバーライドが何を意味するか理解できると どんどん作れるようになるかも?

dikon_007
質問者

お礼

色々試してみましたが、どうしてもうまく表示されません。 //=========================================================== public class drawLine extends JApplet { int X1 = 20 , Y1 = 20 , X2 = 50 , Y2 = 50 ; Graphics g ; 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 ) ; // dp.paint( g ); // this.add( dp ) ; // dp.paint( g ); this.setVisible( true ) ; // dp.makeLine( g ); this.add( dp ) ; dp.paint(g); // this.add( dp ) ; } } //======================================================= class DrawPanel extends JPanel { Float currentWidth = 20.0f ; int x1 , y1 , x2 , y2 ; Graphics g ; public DrawPanel( int x1 , int y1 , int x2 , int y2 ) { this.x1 = x1 ; this.y1 = y1 ; this.x2 = x2 ; this.y2 = y2 ; } // public void makeLine( Graphics g ) public void paint(Graphics g) { // super.paint(g); 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 ) ; } } //=====================================================================

  • askaaska
  • ベストアンサー率35% (1455/4149)
回答No.5

と思ったけどよく見ると明らかね 悪いのはここ Graphics2D g2 = (Graphics2D) this.getGraphics(); thisは現在初期化中(コンストラクタ中)なので まだ描画されていないから Graphicsオブジェクトは取れないわ nullが返るのよ Graphics2D g2 = (Graphics2D)this.getGraphics() ; 以降を別のメソッドに記述して 最低でも描画処理対象になる this.add( dp ) ; か this.setVisible( true ) ; より後ろで実行しないとダメよ どっちが正解かは試してみて

dikon_007
質問者

お礼

回答有り難うございます。 this.add( dp ); dp.makeLine( ); を作成して、色々と位置を変えてみましたが、線はかけません。 エラーは出ていません。 ========================================================== 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.add( dp ) ; // dp.makeLine( ); this.setVisible( true ) ; // dp.makeLine( ); this.add( dp ) ; dp.makeLine( ); } } //======================================================= 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 ; } public void makeLine( ) { 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 ) ; } } ===========================================================================

  • askaaska
  • ベストアンサー率35% (1455/4149)
回答No.4

drawLine.java の 46行目はどの処理かしら?

  • Picosoft
  • ベストアンサー率70% (274/391)
回答No.3

それはエラーの「場所」ですよね……。 こんな形ででてきませんか?  Exception in thread "~" java.~Exception:(説明)   at ……   at …… > 単純なエラーだろうと思い、分かる人が見ればすぐに発見できるだろうと思ってしまいました。 内容によってはすぐに発見できることもありますが、 ソースを隅から隅まで確認するのが正直めんどくさいので(私だけかもしれませんが) 出せる情報は小出しにせず全部出していただけると回答しやすいです。

dikon_007
質問者

お礼

すみません、一番上の行のエラーコメントがコピペ出来ていませんでした。 出し惜しみではないです。 宜しくお願いします。 =========================================================== java.lang.NullPointerException at DrawPanel.<init>(drawLine.java:46) at drawLine.init(drawLine.java:20) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) ===========================================================

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

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

  • askaaska
  • ベストアンサー率35% (1455/4149)
回答No.1

そのエラーの内容を書いてほしいわ あなたが理解できなくても 私が理解できなくても 誰かが理解できるかもしれないもの

dikon_007
質問者

お礼

 すみません、エラーの内容を張ります。  単純なエラーだろうと思い、分かる人が見ればすぐに発見できるだろうと思ってしまいました。  ========================================================  at DrawPanel.<init>(drawLine.java:46) at drawLine.init(drawLine.java:20) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)  ========================================================

関連する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)); で設定してみたのですが、どうも気を利かせて(?)細い線を描画してしまうようです。 解る方回答お願いします。

専門家に質問してみよう