2013-04-04 51 views
0

我对java很陌生,一直在试图制作一个简单的绘画程序,除了画笔的颜色,我已经完成了所有工作。 Rigth现在我将颜色设置为蓝色,但我想使颜料刷的颜色与颜色滑块选择的颜色相同。制作绘画程序的问题

这里是我到目前为止

import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Paint extends JFrame implements ChangeListener{ 
    PaintPanel color; 
    PaintPanel2 paint; 
    JSlider red; 
    JSlider green; 
    JSlider blue; 

    public Paint(){ 
     super("Paint"); 
     setSize(300,290); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 

     color = new PaintPanel(); 
     paint = new PaintPanel2(); 
     red = new JSlider(0,255,255); 
     green = new JSlider(0,255,0); 
     blue = new JSlider(0,255,0); 

     red.setMajorTickSpacing(50); 
     red.setMinorTickSpacing(10); 
     red.setPaintTicks(true); 
     red.setPaintLabels(true); 
     red.addChangeListener(this); 

     green.setMajorTickSpacing(50); 
     green.setMinorTickSpacing(10); 
     green.setPaintTicks(true); 
     green.setPaintLabels(true); 
     green.addChangeListener(this); 

     blue.setMajorTickSpacing(50); 
     blue.setMinorTickSpacing(10); 
     blue.setPaintTicks(true); 
     blue.setPaintLabels(true); 
     blue.addChangeListener(this); 

     JLabel redLabel = new JLabel("Red: "); 
     JLabel greenLabel = new JLabel("Green: "); 
     JLabel blueLabel = new JLabel("Blue: "); 
     GridLayout grid = new GridLayout(5,1); 
     FlowLayout flow = new FlowLayout(FlowLayout.RIGHT); 
     setLayout(grid); 

     JPanel redPanel = new JPanel(); 
     redPanel.setLayout(flow); 
     redPanel.add(redLabel); 
     redPanel.add(red); 
     add(redPanel); 

     JPanel greenPanel = new JPanel(); 
     greenPanel.setLayout(flow); 
     greenPanel.add(greenLabel); 
     greenPanel.add(green); 
     add(greenPanel); 

     JPanel bluePanel = new JPanel(); 
     bluePanel.setLayout(flow); 
     bluePanel.add(blueLabel); 
     bluePanel.add(blue); 
     add(bluePanel);  
     add(color); 
     add(paint); 

     setVisible(true); 
    } 

    public void stateChanged(ChangeEvent e){ 
     JSlider source = (JSlider) e.getSource(); 
     if(source.getValueIsAdjusting() != true){ 
      Color mainColor = new Color(red.getValue(), 
       green.getValue(), 
       blue.getValue()); 
      color.changeColor(mainColor); 
      color.repaint(); 
     } 
    } 

    public static void main(String[] args){ 
     Paint p = new Paint(); 
    } 
} 

class PaintPanel extends JPanel{ 
    Color background; 

    public PaintPanel(){ 
     background = Color.red; 
    } 

    public void paintComponent(Graphics comp){ 
     Graphics2D comp2D = (Graphics2D) comp; 
     comp2D.setColor(background); 
     comp2D.fillRect(0,0,getSize().width,getSize().height); 
    } 

    void changeColor(Color newBackground){ 
     background = newBackground; 
    } 
} 

class PaintPanel2 extends JPanel{ 
    Image image; 
    Graphics2D comp2D; 
    int currentX, currentY, oldX, oldY; 
    PaintPanel color; 

    public PaintPanel2(){ 
     color = new PaintPanel();  

     addMouseListener(new MouseAdapter(){ 
      public void mousePressed(MouseEvent e){ 
       oldX = e.getX(); 
       oldY = e.getY(); 
      } 
     }); 

     addMouseMotionListener(new MouseMotionAdapter(){ 
      public void mouseDragged(MouseEvent e){ 
       currentX = e.getX(); 
       currentY = e.getY(); 
       if(comp2D != null) 
       comp2D.drawLine(oldX, oldY, currentX, currentY); 
       repaint(); 
       oldX = currentX; 
       oldY = currentY; 
      } 
     }); 
    } 

    public void paintComponent(Graphics comp){ 
     if(image == null){ 
      image = createImage(getSize().width, getSize().height); 
      comp2D = (Graphics2D)image.getGraphics(); 
      comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      comp2D.setPaint(Color.white); 
      comp2D.fillRect(0, 0, getSize().width, getSize().height); 
      comp2D.setPaint(Color.blue); 
      repaint(); 
     } 
     comp.drawImage(image, 0, 0, null); 
    } 
} 
+0

究竟是什么问题? – Mordechai 2013-04-04 19:07:03

回答

1

的问题是,你没有设置在PaintPanel2选定颜色的代码。我改变了stateChanged方法和PaintPanel2如下,现在它工作,因为我假设你打算:

public void stateChanged(ChangeEvent e) { 
     JSlider source = (JSlider) e.getSource(); 
     if (source.getValueIsAdjusting() != true) { 
      Color mainColor = new Color(red.getValue(), 
        green.getValue(), 
        blue.getValue()); 
      color.changeColor(mainColor); 
      paint.setPaintColor(mainColor); 
      color.repaint(); 
     } 
    } 

class PaintPanel2 extends JPanel { 

    Image image; 
    Graphics2D comp2D; 
    int currentX, currentY, oldX, oldY; 

    public PaintPanel2() { 

     addMouseListener(new MouseAdapter() { 
      public void mousePressed(MouseEvent e) { 
       oldX = e.getX(); 
       oldY = e.getY(); 
      } 
     }); 

     addMouseMotionListener(new MouseMotionAdapter() { 
      public void mouseDragged(MouseEvent e) { 
       currentX = e.getX(); 
       currentY = e.getY(); 
       if (comp2D != null) { 
        comp2D.drawLine(oldX, oldY, currentX, currentY); 
       } 
       repaint(); 
       oldX = currentX; 
       oldY = currentY; 
      } 
     }); 
    } 

    public void paintComponent(Graphics comp) { 
     if (image == null) { 
      image = createImage(getSize().width, getSize().height); 
      comp2D = (Graphics2D) image.getGraphics(); 
      comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      comp2D.setPaint(Color.white); 
      comp2D.fillRect(0, 0, getSize().width, getSize().height); 
      comp2D.setPaint(Color.blue); 
      repaint(); 
     } 
     comp.drawImage(image, 0, 0, null); 
    } 

    public void setPaintColor(Color color) { 
     comp2D.setColor(color); 
    } 
}