2016-02-29 86 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); 
    } 
}