2016-02-29 29 views
1

我正在用mouseListener和mouseMotionListener製作一個gui程序。我有以下線類如何在類和方法之間傳遞Graphics對象來繪製線

public class Line { 

private int x1, x2, y1, y2; 
private Color color; 

public Line(int x1, int x2, int y1, int y2, Color color) 
{ 
    this.x1 = x1; 
    this.x2 = x2; 
    this.y1 = y1; 
    this.y2 = y2; 
    this.color = color; 
} 

public void draw(Graphics page) 
{ 
    page.drawLine(x1, y1, x2, y2); 
    page.setColor(color); 
} 

}

這是我在那裏的mouseReleased我獲得所需的線的最後一站。

public void mouseReleased (MouseEvent event) 
    { // ending points 
    moving = false; 
    Point p2 = event.getPoint(); 
    x2 = p2.x; 
    y2 = p2.y; 
    line = new Line(x1,x2,y1,y2,currentColor); 
    lineList.add(line); 
    canvas.paintComponent(??????????); 

這是應該在數組列表「lineList」中繪製所有這些行的畫布方法。畫布

private class CanvasPanel extends JPanel 
{ 
    //this method draws all shapes specified by a user 
    public void paintComponent(Graphics page) 
    { 
    super.paintComponent(page); 
    setBackground(Color.WHITE); 
    for(int i = 0; i <lineList.size()-1;i++) 
    { 
     line.draw(page); 
    } 

但是我不知道如何通過圖形對象到畫布類,以實際繪製我在JPanel的線路。假設我有所有其他信息正確(初始線條點,JPanel設置正確,按鈕設置),我如何通過這些實際使它畫線到畫布。謝謝!

+0

嗯,這取決於如果你已經有了Canvas類裏面的圖形對象。你的Graphics對象在哪裏? – PsyCode

回答

2

不,你不想在任何地方傳遞一個Graphics對象,事實上你不會從MouseListener或MouseMotionListener中繪製。而是改變這些類中的字段,調用repaint(),然後在paintComponent方法中使用字段結果。

事實上,在你的代碼中,如果CanvasPanel可以訪問lineList,你需要做的就是在lineList集合中添加一個新行後調用repaint()。而已。

此外,不要在paintComponent中設置背景,而是在構造函數中設置背景。你也需要在Line的draw方法中交換你的方法調用。您需要在之前設置顏色

例如,

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class Drawing extends JPanel { 
    public static final Color BG = Color.WHITE; 
    public static final Color LINE_COLOR = Color.RED; 
    public static final Color CURRENT_LINE_COLOR = Color.LIGHT_GRAY; 
    public static final int PREF_W = 800; 
    public static final int PREF_H = PREF_W; 
    private List<Line> lineList = new ArrayList<>(); 
    private Line currentLine = null; 
    private CanvasPanel canvasPanel = new CanvasPanel(); 

    public Drawing() { 
     MyMouse myMouse = new MyMouse(); 
     canvasPanel.addMouseListener(myMouse); 
     canvasPanel.addMouseMotionListener(myMouse); 

     setLayout(new BorderLayout()); 
     add(canvasPanel); 
    } 

    private class CanvasPanel extends JPanel { 
     public CanvasPanel() { 
      setBackground(BG); 
     } 

     public void paintComponent(Graphics page) { 
      super.paintComponent(page); 
      // setBackground(Color.WHITE); // !! no, not here 

      for (Line line : lineList) { 
       line.draw(page); 
      } 

      if (currentLine != null) { 
       currentLine.draw(page); 
      } 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      if (isPreferredSizeSet()) { 
       return super.getPreferredSize(); 
      } 
      return new Dimension(PREF_W, PREF_H); 
     } 
    } 

    private class MyMouse extends MouseAdapter { 
     private int x1; 
     private int y1; 

     @Override 
     public void mousePressed(MouseEvent e) { 
      if (e.getButton() != MouseEvent.BUTTON1) { 
       return; 
      } 
      x1 = e.getX(); 
      y1 = e.getY(); 
      currentLine = null; 
      canvasPanel.repaint(); 
     } 

     @Override 
     public void mouseReleased(MouseEvent e) { 
      if (e.getButton() != MouseEvent.BUTTON1) { 
       return; 
      } 
      Line line = createLine(e, LINE_COLOR); 
      lineList.add(line); 
      currentLine = null; 
      canvasPanel.repaint(); 
     } 

     @Override 
     public void mouseDragged(MouseEvent e) { 
      currentLine = createLine(e, CURRENT_LINE_COLOR); 
      repaint(); 
     } 

     private Line createLine(MouseEvent e, Color currentColor) { 
      int x2 = e.getX(); 
      int y2 = e.getY(); 
      return new Line(x1, x2, y1, y2, currentColor); 
     } 


    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("Drawing"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new Drawing()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 

} 

class Line { 

    private int x1, x2, y1, y2; 
    private Color color; 

    public Line(int x1, int x2, int y1, int y2, Color color) { 
     this.x1 = x1; 
     this.x2 = x2; 
     this.y1 = y1; 
     this.y2 = y2; 
     this.color = color; 
    } 

    public void draw(Graphics page) { 

     // swap these calls! 
     page.setColor(color); //!! This first! 
     page.drawLine(x1, y1, x2, y2); // **Then** this 
     // !! page.setColor(color); 
    } 
}