2014-04-04 88 views
2

我正在寫一個「漆」模擬節目(它繪製矩形和線條從HashSets),我有顏色的點和線的問題。這裏是我的代碼:無法設置顏色的paintComponent

package paint; 

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 
import java.awt.geom.Line2D; 
import java.awt.geom.Point2D; 
import java.util.HashSet; 

import javax.swing.JComponent; 

public class Draw extends JComponent { 
private HashSet<Point2D> points = new HashSet<>(); 
private HashSet<Line2D> lines = new HashSet<>(); 
private final int WIDTH = 5, HEIGHT = 5; 
private Point2D prevPoint, nextPoint; 

public Draw() { 
    addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent e) { 
      super.mousePressed(e); 
      if (e.getButton() == MouseEvent.BUTTON1) { 
       prevPoint = e.getPoint(); 
       points.add(prevPoint); 
       repaint(); 
      } 
     } 

    }); 

    addMouseMotionListener(new MouseMotionAdapter() { 
     @Override 
     public void mouseDragged(MouseEvent e) { 
      super.mouseDragged(e); 
      nextPoint = e.getPoint(); 
      Line2D line = new Line2D.Double(prevPoint, nextPoint); 
      lines.add(line); 
      prevPoint = nextPoint; 
      repaint(); 
     } 
    }); 
} 

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D) g; 
    for (Point2D p : points) { 
     int x = (int)p.getX() - (WIDTH/2); 
     int y = (int)p.getY() - (HEIGHT/2); 
     g2d.setColor(getColor()); 
     g2d.fillRect(x, y, WIDTH, HEIGHT); 
     g2d.drawRect(x, y, WIDTH, HEIGHT); 
    } 

    for (Line2D l : lines) { 
     g2d.setColor(getColor()); 
     g2d.setStroke(new BasicStroke(WIDTH)); 
     g2d.draw(l); 
    } 
} 

public Color getColor() { 
    Tools t = new Tools(); 
    return t.getColor(); 
} 

} 


package paint; 

import java.awt.Color; 
import java.awt.event.ActionEvent; 

import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JPanel; 

public class Tools extends JPanel { 
private Color color; 
private JButton button; 
private ButtonColor bc; 

public Tools() { 
    makeButton("black", Color.BLACK); 
    makeButton("green", Color.GREEN); 
} 

public void makeButton(String name, final Color color) { 
    bc = new ButtonColor(name, color); 
    add(new JButton(bc)); 
} 

private class ButtonColor extends AbstractAction { 

    public ButtonColor(String name, Color color) { 
     putValue(NAME, name); 
     putValue(SHORT_DESCRIPTION, "Choose " + name.toLowerCase() + " color"); 
     putValue("color", color); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     color = (Color) getValue("color"); 
     System.out.println(color); 
    } 

} 

public Color getColor() { 
    System.out.println(color); 
    return color; 
} 

} 

現在,當我按下打印java.awt.Color[r=0,g=0,b=0]java.awt.Color[r=0,g=255,b=0]黑色或綠色按鈕控制檯。但是,當我試圖畫線,它打印null

如何更改方法Draw.getColor()以更改顏色?

回答

2

每次調用paintComponent時,都使用最後設置的顏色。

您需要做的是將顏色與當前活動相關聯,以便在調用paintComponent時,可以查找所繪形狀的顏色。

您可以創建一個包含Line和顏色值的自定義類並將它們添加到您的列表中。當你畫的Line,你會查找要使用的顏色以及

您沃爾德需要做同樣的事情你的觀點

+0

好主意!我改變了這一點,代碼看起來更具可讀性和靈活性。 – s4bba7

+0

但問題是在類Tool中使用私有顏色。我改變了私人靜態顏色的顏色,現在它工作正常。當然,問題是:它是否安全,還是可能出現其他不好的事情? – s4bba7

+0

沒有。顏色應該與您正在繪製的當前元素相關,而不是您正在繪製的畫布 – MadProgrammer