2012-09-25 51 views
0

我正在編寫一個Java小程序。Java,創建線對象擴展JComponent

在這個小程序中,我需要在圖像上繪製一些標記(如紅色圓圈)和一些線條。

我已經成功實現了標記,作爲JComponent的擴展,並且我還將這些鼠標放在了一些監聽器上。

我遇到了線對象的大問題。我創建了另一個擴展JComponent的對象,除此之外,我在座標系中遇到了一些問題,setDimension會產生麻煩。例如,它攔截所有標記的點擊。

這是不是讓對象「維」越緊到線路的方法,因爲我不能只畫出中古立式或臥式線...

感謝你們所有的人。

編輯

public class Path extends JComponent { 
... 
    // stroke of the line 
    private Stroke spessore = new BasicStroke(SPESSORE); 

    // coordinates 
    private double x, y, x_2, y_2; 

// ZoomManager is an object. In this project I can zoom in and zoom out the 
    // image, so this object convert coordinates get on the superior JPanel in 
    // coordinates on the image real-sized. 
    public Path(double x, double y, ZoomManager zoom) {//, double x_2, double y_2, ZoomManager zoom) { 
      super(); 

      // this function return the coordinates on the real-sized image 
      Point a = DrawableObjects.getScaledCoordinates(x, y, zoom); 
      this.x = a.x; 
      this.y = a.y; 

      this.x_2 = a.x; 
      this.y_2 = a.y; 

      updateBoundsAndSize(zoom); 

      // this was only for test... 
      this.addMouseListener(new MouseListener(){ 

     @Override 
     public void mouseClicked(MouseEvent arg0) { 
      System.out.println("CLICK!"); 
        } 
        ... 
      }); 
    } 

    // this function is called during the mouse dragging for drow the line. 
    // it gets the coordinates, convert them, save them and update the bounds and 
    // size of the object 
    public void setArrivePoint(Point a, ZoomManager zoom) { 
      Point p = DrawableObjects.getScaledCoordinates(a.x, a.y, zoom); 
      this.x_2 = p.x; 
      this.y_2 = p.y; 
      updateBoundsAndSize(zoom); 
    } 

    // update the bounds of the object, the origin point of the rectangle is the 
    // top-left coordinate build with the original coordinates. The width and height of the rectangle are obtained by subtraction. 
    private void updateBoundsAndSize(ZoomManager zoom) { 

      Point p = DrawableObjects.getPanelCoordinates(x, y, zoom); 
      Point a = DrawableObjects.getPanelCoordinates(x_2, y_2, zoom); 

      int min_x = (int)Math.min(p.x, a.x) - SPESSORE; 
      int min_y = (int)Math.min(p.y, a.y) - SPESSORE; 

      if (min_x < 0) 
        min_x =0; 

      if (min_y < 0) 
        min_y = 0; 

      int w = (int) (Math.max(a.x, p.x) - min_x) + SPESSORE; 
      int h = (int) (Math.max(a.y, p.y) - min_y) + SPESSORE; 

      setBounds(new Rectangle(min_x, min_y, w, h)); 
      repaint(); 
    } 

    // drawing function 
    @Override 
    protected void paintComponent(Graphics g) { 

      super.paintComponent(g); 

      Graphics2D antiAlias = (Graphics2D) g; 
      antiAlias.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

      // get ZoomManager from the superior object 
      ZoomManager zoom = ((JPanelImmagine)this.getParent()).zoom; 

      antiAlias.setColor(DEFAULT_COLOR); 
      antiAlias.setStroke(spessore); 

      Point[] coordinates = updateCoordinates(zoom); 

      Line2D line = new Line2D.Double(coordinates[0], coordinates[1]); 

      antiAlias.draw(line); 

    } 

    // translate coordinates from superior jpanel to this object 
    private Point[] updateCoordinates(ZoomManager zoom) { 

      Point[] output = new Point[2]; 

      Point p = DrawableObjects.getScaledCoordinates(x, y, zoom); 
      Point a = DrawableObjects.getScaledCoordinates(x_2, y_2, zoom); 

      double o_x = this.getBounds().getCenterX(); 
      double o_y = this.getBounds().getCenterY(); 
      Point origin = new Point ((int)o_x, (int)o_y); 

      output[0] = calculateCoordinates(p, origin); 
      output[1] = calculateCoordinates(a, origin); 

      return output; 
    } 

    private Point calculateCoordinates(Point p, Point origin) { 

      double new_x = p.x - origin.x; 
      double new_y = p.y - origin.y; 

      return new Point((int)new_x, (int)new_y); 
    } 
+0

你的代碼看起來如何?這聽起來像你沒有使用一個JComponent的onDraw方法,而是將子視圖添加到JComponent? –

+0

問題是我想將這個組件添加到JPanel中,我不明白爲什麼要繪製45度線,我必須在線的周圍預留一個方形區域,而不是線條周圍的緊密矩形。 –

+0

或者,如果我必須保留正方形區域,如果鼠標不在繪製線上,如何讓mouseevents傳遞給其他組件。 –

回答

0

使用this method解決!我不得不改變完全解決問題的方法。

+0

然後你應該接受你自己的答案作爲正確的答案,以便這個問題被標記爲已回答。很高興你解決了這個問題,當然它是'paintComponent',而不是onDraw我的意思是當我說onDraw :) –