2011-10-10 90 views
0

我还有麻烦,我也想告诉你我的代码。如果你能运行它,那会很棒。这有点复杂。也许当你看到所有的时候,你可能会发现另一个错误,导致找到错误的线条,但最后一行是最接近的。另一个问题是,当你在创建一条线后运行程序时,jmenu会自我复制,但是我可以在第二个jmenu上画一条线。我相信这与我的编码无关。你能看看我的代码吗?感谢您提供以前的答案。对于我的帖子感到抱歉,这是我第一次提出问题。根据点查找线路

import java.awt.Color; 
    import java.awt.Point; 

    public class Lines { 

public int id; 
public Point point1; 
public Point point2; 
public int[] denklem; 

}

FIRST CLASS到此为止

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.util.ArrayList; 
    import java.util.Collections; 
    import java.util.Iterator; 


    public class Tester{ 

public Tester() { 
    cizer test = new cizer(); 
    JFrame frame = new JFrame(); 
// frame.setBackground(Color.DARK_GRAY); 
    frame.setJMenuBar(test.jmb); 
    frame.add(test.statusBar, BorderLayout.SOUTH); 
    Container c = frame.getContentPane(); 
    c.add(test); 
    c.setSize(700, 700); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(700,700); 
    frame.setVisible(true); 
} 
public static void main(String[] args) { 
    Tester a = new Tester(); 
} 
    } 
    class cizer extends JPanel implements MouseMotionListener, MouseListener { 

int mouseclicks = 0; 
boolean begin = true; 
boolean linesegmentci = false; 
boolean lineci = false; 
boolean chooser = false; 
boolean deleter = false; 
boolean digerinde = false; 
ArrayList<Lines> array = new ArrayList<Lines>(); 
ArrayList<Lines> array3 = new ArrayList<Lines>(); 
Devline devarray = new Devline(); 
ArrayList<Devline> devarray2 = new ArrayList<Devline>(); 
JMenuBar jmb = new JMenuBar(); 
JMenu menu = new JMenu("Secenekler"); 
JMenu menu2 = new JMenu("Renk"); 
JMenu menu3 = new JMenu("Kalınlık"); 
JMenu menu4 = new JMenu("Sec"); 
JMenuItem item = new JMenuItem("Linesegments drawer"); 
JMenuItem item2 = new JMenuItem("Line drawer"); 
JMenuItem item3 = new JMenuItem("Black"); 
JMenuItem item4 = new JMenuItem("Blue"); 
JMenuItem item5 = new JMenuItem("Red"); 
JMenuItem item6 = new JMenuItem("Green"); 
//JMenuItem item13 = new JMenuItem("Hepsinin rengini degistir"); 
JMenuItem item7 = new JMenuItem("3"); 
JMenuItem item8 = new JMenuItem("5"); 
JMenuItem item9 = new JMenuItem("7"); 
JMenuItem item10 = new JMenuItem("10"); 
JMenuItem item11 = new JMenuItem("Find Closest line"); 
//JMenuItem item12 = new JMenuItem("Sil"); 
JLabel statusBar = new JLabel("Mouse outside JPanel"); 
Lines l1 = new Lines(); 
int minValue = Integer.MAX_VALUE; 
Lines min = null; 
ArrayList<Integer> minimum = new ArrayList<Integer>(); 
Graphics2D g2D; 
Color color; 
BasicStroke stroke; 
int minx = 0; 
boolean deneme = false; 
public cizer(){ 
    menu.add(item); 
    menu.add(item2); 
    menu2.add(item3); 
    menu2.add(item4); 
    menu2.add(item5); 
    menu2.add(item6); 
// menu2.add(item13); 
    menu3.add(item7); 
    menu3.add(item8); 
    menu3.add(item9); 
    menu3.add(item10); 
    menu4.add(item11); 
// menu.add(item12); 
    jmb.add(menu); 
    jmb.add(menu2); 
    jmb.add(menu3); 
    jmb.add(menu4); 
    l1.point1 = new Point(); 
    l1.point2 = new Point(); 
// min.point1 = new Point(); 
// min.point2 = new Point(); 
    l1.denklem = new int[3]; 
    stroke = new BasicStroke(); 


    item.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      linesegmentci = true; 
      lineci = false; 
      chooser = false; 
      deleter = false; 
     } 
    }); 
    item2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      begin = true; 
      lineci = true; 
      linesegmentci = false; 
      chooser = false; 
      deleter = false; 
     } 
    }); 
    item3.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      color = Color.BLACK; 


     } 
    }); 
    item4.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      color = Color.BLUE; 

     } 
    }); 
    item5.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      color = Color.RED; 

     } 
    }); 
    item6.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      color = Color.GREEN; 

     } 
    }); 
    item7.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      stroke = new BasicStroke(3.f); 


     } 
    }); 
    item8.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      stroke = new BasicStroke(5.f); 

     } 
    }); 
    item9.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      stroke = new BasicStroke(7.f); 

     } 
    }); 
    item10.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      stroke = new BasicStroke(10.f); 

     } 
    }); 
    item11.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      chooser = true; 
      linesegmentci = false; 
      lineci = false; 
      deleter = false; 

     } 
    }); 
/* item12.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      deleter = true; 
      chooser = false; 

     } 
    });*/ 
/* item13.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      deneme = true; 
     } 
    });*/ 
    addMouseListener(this); 
    addMouseMotionListener(this); 

} 

public void paint(Graphics g) { 

    g2D = (Graphics2D) g; 
    devarray.segmentim.add(l1); 

    if(linesegmentci == true){ 
     Iterator<Lines> itr = devarray.segmentim.iterator(); 
      while(itr.hasNext()){ 
       Lines l = itr.next(); 
       g.setColor(color); 
       g2D.setStroke(stroke); 
       g2D.drawLine(l.point1.x,l.point1.y,l.point2.x,l.point2.y); 


      } 
      devarray2.add(devarray); 

    } 
    else if(lineci == true){ 
     array3.add(l1); 
     g2D.setPaint(color); 
     g2D.setStroke(stroke); 
     g2D.drawLine(l1.point1.x,l1.point1.y,l1.point2.x,l1.point2.y); 
    } 
} 
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) { 
    if(chooser == true){ 
     Iterator<Lines> itr2 = array.iterator(); 
     while(itr2.hasNext()){ 
      Lines l2 = itr2.next(); 
      minx = (int) (Math.abs((l2.denklem[0] * e.getX()) + (l2.denklem[1] * e.getY()) + 
        l2.denklem[2])/(Math.sqrt((l2.denklem[0] *l2.denklem[0]) + (l2.denklem[1] * l2.denklem[1])))); 

      if (minx < minValue){ 
        minValue = minx; 
        min = l2; 
       } 
     } 
     System.out.println("CLOSEST LINE COORDINATES"); 
     System.out.println(min.point1.x); 
     System.out.println(min.point1.y); 
    } 
    else if(linesegmentci == true){ 

     if(begin == true){ 
      if(mouseclicks == 0){ 
       l1.point1.x = e.getX(); 
       l1.point1.y = e.getY(); 

       statusBar.setText(String.format("Clicked at [%d, %d]", 
         e.getX(), e.getY())); 

       mouseclicks++; 
      } 
      else if(mouseclicks == 1){ 
       l1.point2.x = e.getX(); 
       l1.point2.y = e.getY(); 
       statusBar.setText(String.format("Clicked at [%d, %d]", 
         e.getX(), e.getY())); 
       mouseclicks = 0; 
       begin = false; 
       repaint(); 

      } 
      // array2.devarray.add(l1); 
      array.add(l1); 
      int a = l1.point1.y - l1.point2.y; 
      int b = l1.point2.x - l1.point1.x; 
      int c = (l1.point1.x * l1.point2.y) - (l1.point2.x * l1.point1.y); 
      l1.denklem[0] = a; 
      l1.denklem[1] = b; 
      l1.denklem[2] = c; 
     } 
     else{ 
      l1.point1.x = l1.point2.x; 
      l1.point1.y = l1.point2.y; 
      l1.point2.x = e.getX(); 
      l1.point2.y = e.getY(); 
      statusBar.setText(String.format("Clicked at [%d, %d]", 
        e.getX(), e.getY())); 
      repaint(); 
      // array2.devarray.add(l1); 
      array.add(l1); 
      int a = l1.point1.y - l1.point2.y; 
      int b = l1.point2.x - l1.point1.x; 
      int c = (l1.point1.x * l1.point2.y) - (l1.point2.x * l1.point1.y); 
      l1.denklem[0] = a; 
      l1.denklem[1] = b; 
      l1.denklem[2] = c; 
     } 
    } 
    else if(lineci == true){ 

     if(mouseclicks == 0){ 
      l1.point1.x = e.getX(); 
      l1.point1.y = e.getY(); 
      statusBar.setText(String.format("Clicked at [%d, %d]", 
        e.getX(), e.getY())); 
      mouseclicks++; 
     } 
     else if(mouseclicks == 1){ 
      l1.point2.x = e.getX(); 
      l1.point2.y = e.getY(); 
      statusBar.setText(String.format("Clicked at [%d, %d]", 
        e.getX(), e.getY())); 
      mouseclicks = 0; 
     // begin = false; 
      repaint(); 

     } 
     array.add(l1); 
     int a = l1.point1.y - l1.point2.y; 
     int b = l1.point2.x - l1.point1.x; 
     int c = (l1.point1.x * l1.point2.y) - (l1.point2.x * l1.point1.y); 
     l1.denklem[0] = a; 
     l1.denklem[1] = b; 
     l1.denklem[2] = c; 

    } 
} 

public void mouseMoved(MouseEvent e) { } 
public void mouseDragged(MouseEvent e) {} 

}

Sry基因我也忘了这一个

import java.util.ArrayList; 


    public class Devline { 
ArrayList<Lines> segmentim = new ArrayList<Lines>(); 

    } 

回答

1

我认为您的解决方案是更复杂一点比它需要吨是的。就个人而言,我会选择具有以下结构的东西:

Lines min = null; 
    double minDist = Double.MAX_VALUE; 
    for (Lines l : array) { 
    double dist = ...; // compute the distance from the point to `l' 
    if (dist < minDist) { 
     minDist = dist; 
     min = l; 
    } 
    } 

P.S.我没有检查你的距离计算。

0

为什么你不这样做?

int minValue = Integer.MAX_VALUE; 
Lines min = null; 

Iterator<Lines> itr2 = array.iterator(); 
while(itr2.hasNext()){ 
    Lines l2 = itr2.next(); 
    minx = (int) (Math.abs((l2.denklem[0] * e.getX()) + (l2.denklem[1] * e.getY()) + 
       l2.denklem[2])/(Math.sqrt((l2.denklem[0] *l2.denklem[0]) + (l2.denklem[1] * l2.denklem[1])))); 
    if (minx < minValue){ 
     minValue = minx; 
     min = l2; 
    } 
}