2013-02-24 19 views
1

我正在構建一個應用程序,可識別用鼠標繪製的線條並創建音樂。我是新來的Java2D因此,繼承人我遇到的問題:免費線條繪製java2d應用程序連接每個新圖形的線條

  • 當你畫,然後在另一臺繪圖,以前繪製的最後一個點連接到第一個新的。我還沒有弄清楚如何解決這個問題。以下是代碼。

  • 另一個問題是:我想將繪圖中的每個筆畫(從mousePressed到mouseReleased)存儲到Shape類型的ArrayList中,我該如何進入?

我想指出正確的方向,因爲我還沒有能夠找到有用的信息在線。謝謝!

public class DrawBoard extends JPanel implements MouseListener, 
     MouseMotionListener { 

    public JLabel status; 
    public Point pstart, pfinish; 
    private Shape currentShape = null; 
    private ArrayList<Point> points = new ArrayList<Point>(); 
    private ArrayList<Shape> lines = new ArrayList<Shape>(); 

    public DrawBoard() { 

     Dimension size = getPreferredSize(); 
     size.setSize(1024, 800); // w, h 
     setPreferredSize(size); 
     setOpaque(false); 
     status = new JLabel("default"); 
     add(status, BorderLayout.SOUTH); 

     addMouseListener(this); 
     addMouseMotionListener(this); 

    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     status.setText(String.format("Clicked at %d,%d", e.getX(), e.getY())); 
    } 

    // Where the drawing happens 
    @Override 
    public void mousePressed(MouseEvent e) { 
     status.setText("you pressed down the mouse"); 
     this.pstart = e.getPoint(); 

    } 

    @Override 
    public void mouseDragged(MouseEvent e) { 
     status.setText("you draged the mouse"); 
     points.add(e.getPoint()); 
     repaint(); 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     status.setText("you release the mouse click"); 
     pfinish = e.getPoint(); 
    } 

    // End of where the drawing happens 

    @Override 
    public void mouseEntered(MouseEvent e) { 
     status.setText("you entered the area"); 
    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     status.setText("mouse exited the area"); 
    } 

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

    public void paint(Graphics g) { 
     super.paint(g); 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setColor(Color.BLACK); 
     g2.setStroke(new BasicStroke(5)); 

     for (int i = 0; i < points.size() - 2; i++) { 

      Point p1 = points.get(i); 
      Point p2 = points.get(i + 1); 

      g2.drawLine(p1.x, p1.y, p2.x, p2.y); 
     } 

    } 
} 

我一直在使用BufferedImage的修改,它工作正常進行繪製,現在唯一的事情做n要工作是明確的方法,我已經嘗試不同的方法但都沒有奏效。我的新代碼如下:

public class DrawBoard extends JPanel implements MouseListener, MouseMotionListener{ 


public JLabel status; 
private JLabel imgLabel; 
public Point pstart, pfinish; 
private Shape currentShape = null; 
private List<Point> points = new ArrayList<Point>(); 
private List<BufferedImage> lines = new ArrayList<BufferedImage>(); 

private static final int BI_WIDTH = 1024; 
private static final int BI_HEIGHT = 800; 

private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT, 
     BufferedImage.TYPE_INT_ARGB); 

public DrawBoard(){ 

    Graphics2D g2d = bImage.createGraphics(); 
    g2d.dispose(); 

    Dimension size = getPreferredSize(); 
    size.setSize(1024,800); //w, h 
    setPreferredSize(size); 
    status = new JLabel("default"); 
    add(status, BorderLayout.SOUTH); 
    addMouseListener(this); 
    addMouseMotionListener(this); 


    imgLabel = new JLabel(new ImageIcon(bImage)) { 
    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     paintInLabel(g); 
    } 
    }; 
    imgLabel.setOpaque(false); 
    setOpaque(false); 
     add(imgLabel, BorderLayout.CENTER); 

} 

private void paintInLabel(Graphics g) { 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.setColor(Color.BLUE); // this colour is when mouse is pressed 
    g2d.setStroke(new BasicStroke(5)); 
    if (points.size() < 2) { 
    return; 
    } 
    for (int i = 1; i < points.size(); i++) { 
    int x1 = points.get(i - 1).x; 
    int y1 = points.get(i - 1).y; 
    int x2 = points.get(i).x; 
    int y2 = points.get(i).y; 
    g2d.drawLine(x1, y1, x2, y2); 
    } 
} 



@Override 
public void mouseClicked(MouseEvent e) { 
    status.setText(String.format("Clicked at %d,%d", e.getX(), e.getY())); 
} 


// Where the drawing happens 
@Override 
public void mousePressed(MouseEvent e) { 
    status.setText("you pressed down the mouse"); 
    this.pstart = e.getPoint(); 
    points.add(e.getPoint()); 

} 

@Override 
public void mouseDragged(MouseEvent e) { 
    status.setText("you draged the mouse"); 
    points.add(e.getPoint()); 
    imgLabel.repaint(); 
} 

@Override 
public void mouseReleased(MouseEvent e) { 
    status.setText("you release the mouse click"); 
    Graphics2D g2d = bImage.createGraphics(); 
    g2d.setColor(Color.blue); // this is the final colour 
    g2d.setStroke(new BasicStroke(5)); 

    if (points.size() >= 2) { 
     for (int i = 1; i < points.size(); i++) { 
      int x1 = points.get(i - 1).x; 
      int y1 = points.get(i - 1).y; 
      int x2 = points.get(i).x; 
      int y2 = points.get(i).y; 
      g2d.drawLine(x1, y1, x2, y2); 
     } 
    } 
    g2d.dispose(); 

    points.clear(); 
    imgLabel.repaint(); 

} 
// End of where the drawing happens 

@Override 
public void mouseEntered(MouseEvent e) { 
    status.setText("you entered the area"); 
} 


@Override 
public void mouseExited(MouseEvent e) { 
    status.setText("mouse exited the area"); 
} 

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

public void clearDrawBoard() { 

    imgLabel.setIcon(null); 
} 

} 

回答

3

一些對策和建議:

  • 不要從一個單一的List<Point>畫作爲有其中一個行結束和另一個開始無從得知。
  • 考慮在BufferedImage完成後將每一行繪製到BufferedImage中,並在其PaintComponent方法中的JComponent中顯示該BufferedImage。你可以把它放在mouseReleased(...)方法的BufferedImage中。
  • 或者考慮創建一個List<List<Point>>,以便您可以根據需要迭代每行。您可以將新的List<Point>添加到mouseReleased(...)方法中的原始列表中。
  • 請考慮使用您的List<Shape>並填寫可以繪製的Line2D對象。 Line2D將被添加到mouseReleased(...)方法中的List<Shape>
  • 另外:你不應該重寫paint(...),而應該使用`paintComponent(...)。
  • 另外:不要忘記@Override註釋更安全的編碼。

例如,請參閱我的代碼並回答here

+0

謝謝。我以BufferedImage的方式進行,因爲我認爲它對於我所尋找的最好,因爲我想要應用Erase。事情是我仍然對j2d的工作方式有點困惑,所以你可以舉一個例子,我將如何繼續將其寫入BufferedImage?我知道它需要'寬度,高度和類型',儘管我知道類型,但w和h對於線條和點的含義是什麼? – nullwriter 2013-02-24 17:09:35

+0

@ChristianFeo:因爲我的時間對你來說對我來說同樣重要,所以我要求你在這個網站上搜索這個你能找到的很多例子,其中很多例子都是我寫的。然後如果你仍然堅持,發佈你的嘗試作爲編輯你的原始問題。 – 2013-02-24 17:11:05

+0

夠公平!我會搜索你的答案;)。 – nullwriter 2013-02-24 17:13:32