2012-01-21 13 views
0

私はJPanelでクリックした2点を接続するプログラムを作ろうとしています。私は2つの点を線で結びつけようとしています。マウスをクリックするたびにmy(x1、y1)と(x2、y2)の値が表示され、値にエラーはありません。行が表示されているとき、行は指定した座標に従わず、代わりに異なる場所に出力され、歪んで表示されます。私はsetBounds()を使用しているので、行のほとんどは何かによってカットされているように見えます。また、私はpaintComponent関数内にSystem.out.println( "")を追加し、1度だけ印刷する必要があるにもかかわらず、複数回(クリックごとに1ずつ増加する)印刷されていることに気付きました。誰もがこれで私を助けることができますか?ありがとう!ここpaintComponent()出力エラー

はエラーに貢献したクラスの2つです:

CLASS#1:

import java.awt.event.MouseEvent; 
import javax.swing.JPanel; 


/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author Arch. Don Saborrido 
*/ 
public class twixtBoard extends JPanel implements java.awt.event.MouseListener{ 
    int x1 = 0, x2, y1 = 0, y2; 
    DrawLine line; 
    //DrawLine line; 


    public twixtBoard(){ 
     //requestFocus(); 
     setLayout(null); 
     setVisible(false); 
     setBounds(0,0,600,450); 
     setOpaque(false); 
     setFocusable(false); 
     addListener(); 
    } 

    public void addListener(){ 
     addMouseListener(this); 
    } 

    public void mouseClicked(MouseEvent e) { 
     if (x1 == 0 || y1 == 0){ 
        x1 = e.getX(); 
        y1 = e.getY(); 
        System.out.println(x1 + " " + y1); 
     } 
     else{ 
      x2 = e.getX(); 
      y2 = e.getY(); 
      //System.out.println(x2 + " " + y2); 
      line = new DrawLine(x1, y1, x2, y2); 
      line.setBounds(x1, y1, x2, y2); 
      System.out.println("" + line.getLocation()); 
      //line.setOpaque(false); 
      add(line); 
      x1 = x2; 
      y1 = y2; 
      repaint(); 
     } 
    } 

    public void mousePressed(MouseEvent e) { 
     //throw new UnsupportedOperationException("Not supported yet."); 
    } 

    public void mouseReleased(MouseEvent e) { 
     ///throw new UnsupportedOperationException("Not supported yet."); 
    } 

    public void mouseEntered(MouseEvent e) { 
     //throw new UnsupportedOperationException("Not supported yet."); 
    } 

    public void mouseExited(MouseEvent e) { 
     //throw new UnsupportedOperationException("Not supported yet."); 
    } 
} 

CLASS#2(クラスペイント):

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author Arch. Don Saborrido 
*/ 
import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Stroke; 
import java.awt.geom.GeneralPath; 
//import java.awt.geom.Line2D; 
import javax.swing.JPanel; 

public class DrawLine extends JPanel{ 
    int x1 = 0, x2 = 0, y1 = 0, y2 = 0; 
    //Line2D line; 
    Stroke[] s = new Stroke[] {new BasicStroke(10.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)}; 
    //new BasicStroke(25.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL), 
    //new BasicStroke(25.0f, BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER) 
    GeneralPath path = new GeneralPath(); 

    public DrawLine(int start_x, int start_y, int end_x, int end_y){ 
     x1 = start_x; 
     y1 = start_y; 
     x2 = end_x; 
     y2 = end_y; 
     System.out.println(x1+ " " + y1+ " " + x2+ " " + y2+ " "); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     System.out.println("entered paint"); 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setColor(Color.BLACK); 
     g2.setStroke(s[0]); 
     path.moveTo(x1,y1); 
     System.out.println("x1 = " + x1 + " y1 = " + y1); 
     path.lineTo(x2,y2); 
     System.out.println("x2 = " + x2 + " y2 = " + y2); 
     System.out.println("" + path.getBounds2D()); 
     g2.draw(path); 
     //line = new Line2D.Float(x1, y1, x2, y2); 
     //if(x1 != x2 && y1 != y2) 
      //g2.draw(line); 
    } 
} 
+1

crossposted http://www.daniweb.com/software-development/java/threads/407781 – mKorbel

+0

(x1-x2)の絶対値を取得すると、 – kleopatra

答えて

0

これは非常に複雑なアプローチです。

次のことを試してみてください:X1、X2、Y1とY2:

  • は、4つの変数を持つクラス(例えばLineClass)を作ります。
  • 生成された行のArrayListを保持する
  • g.drawline(line.x1、line.y1、line.x2、line.y2)を使用してJPanelのArrayListの各行を描画します。 ArrayListの
class MainClass extends JPanel implements MouseListener { 
    boolean clickedOnce = false; 
    ArrayList<Line> lines = new ArrayList<Line>(); 
    int x, y; 
    MainClass() { 
    // init... 
    addMouseListener(this); 
    } 
    // more MouseEvent methods 
    public void mouseClicked(MouseEvent e) { 
    if (!clickedOnce){ 
     x=e.getX(); 
     y = e.getY(); 
     clickedOnce = true; 
    } 
    else{ 
     lines.add(new Line(x,y,e.getX(), e.getY()); 
     clickedOnce = false; 
     repaint(); 
    } 
    } 

    public void paintComponent (Graphics g) { 
    super.paintComponent(g); 
    for (Line l: lines) 
     g.drawLine(l.x1, l.y2, l.x2, l.y2); 
    } 

    private class Line { 
    public int x1, y1, x2, y2; 
    Line (int x1, int y1, int x2, int y2) { 
     this.x1 = x1; 
     this.x2 = x2; 
     this.y1 = y1; 
     this.y2 = y2; 
    } 
    } 
} 
+0

Java名義規則を学び、それらに固執してください。それは別の行を印刷しました。私は元のパラメータ(x1、y1、x2、y2)を使用し、2つのクラスを1つのクラスに結合しました。今私は働くコードを持っていた。答えた人のおかげです。 –

0
line = new DrawLine(x1, y1, x2, y2); 
ine.setBounds(x1, y1, x2, y2); 
add(line); 

基本的なロジックは私には間違って見えます。あなたはサイズと場所の4つの値を使うことはできません。

場所は、x1/x2とy1/y2の最小値になります。

サイズ(幅、高さ)は(x1 - x2)と(y1 - y2)の絶対値になります。

次に、線を描くと(0、0)から(幅、高さ)まで線が引かれます。

コード例がCustom Painting Approachesの場合、多少異なる解決策が役立つかもしれません。

0

問題の一つは、あなたが間違った幅と高さを計算しているということです。それらはx1とx2、y1とy2の違いになります。

javaのGraphicsクラスを使用すると、これを簡単に行うこともできます。それにはおそらくあなたの人生を楽にするdrawLine()のようなメソッドがあります。