2012-01-21 63 views
0

我试图做一个程序,将连接两个点,我点击我的JPanel。我试图用线连接两点。每当我点击使用鼠标并且数值中没有错误时,我显示my(x1,y1)和(x2,y2)坐标的值。但是当显示该行时,该行似乎不遵循我指定的坐标,而是在不同的位置输出并且失真。大多数线条似乎被某些东西切割,我认为这是由线条创建的矩形,因为我使用了setBounds()。另外,我在我的paintComponent函数中添加了一个System.out.println(“”),我注意到它多次打印(每次点击后增加1),即使它应该只打印一次。谁能帮我这个?谢谢!paintComponent()输出错误

这里有两个,其向错误的类:

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

请学习java命名约定并坚持他们 – kleopatra

回答

0

这是一个非常复杂的方法。

尝试以下方法:

  • 使一个类(例如LINECLASS)具有四个变量:X1,X2,Y1和Y2。
  • 保持你的生成线的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

得到(x1-x2)的绝对值不起作用。它打印了不同的行。我使用原始参数(x1,y1,x2,y2)并将两个类组合成一个类。现在我有一个工作代码。感谢所有回答的人。 –

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()这样的方法,这可能会让你的生活更轻松。