如何使用java .awt更改基本Paint程序中的颜色

时间:2016-03-02 17:31:14

标签: java awt

所以我正在研究这个简单的绘画程序,记录鼠标事件,如输入退出,拖动等...程序记录所有这些点,并将其存储在"行"的数组列表中;(A我为此任务定义的类)然后程序将列表绘制到屏幕上。我的问题是我应该如何改变我绘画的东西的颜色,改变我已经绘制的颜色,任何帮助,谢谢你。

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class MyNewDrawFrame extends Frame{

     public MyNewDrawFrame(){
        setSize(500,500);
        setTitle("Drawing is fun");
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });
        MyNewDrawPanel mdp = new MyNewDrawPanel();
        add(mdp);
        setVisible(true);
     }
     public static void main(String[] args){
         MyNewDrawFrame mdf = new MyNewDrawFrame();
     }
}

class MyNewDrawPanel extends DoubleBuffer implements MouseListener, MouseMotionListener,ActionListener{

    int lastX=0, lastY=0;
    ArrayList<Line>lines;
    Color color;
    Button red, green, black, blue;

    public MyNewDrawPanel() {

        setLayout(new BorderLayout());
        red = new Button("Red");
        green = new Button("Green");
        blue = new Button("Blue");
        black = new Button("Black");
        red.addActionListener(this);
        green.addActionListener(this);
        blue.addActionListener(this);
        black.addActionListener(this);
        red.setBackground(Color.red);
        green.setBackground(Color.green);
        blue.setBackground(Color.blue);
        black.setBackground(Color.black);
        Panel p = new Panel();
        p.add(red);
        p.add(green);
        p.add(blue);
        p.add(black);
        add(p, BorderLayout.SOUTH);
        lines = new ArrayList<Line>();
        setBackground(Color.white);
        color = Color.black;
        setForeground(color);
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    public Color getColor(){
        return color;
    }
    public void setColor(Color color){
        this.color = color;
    }

    public void mouseExited(MouseEvent me){}
    public void mouseClicked(MouseEvent me){}
    public void mouseReleased(MouseEvent me){
    }
    public void mouseEntered(MouseEvent me){
        record(me.getX(), me.getY());
    }
    public void mousePressed(MouseEvent me){
        record(me.getX(), me.getY());
    }
    public void mouseMoved(MouseEvent me){}
    public void mouseDragged(MouseEvent me){
        int x = me.getX();
        int y = me.getY();
        lines.add(new Line(lastX, lastY, x, y));
        record(x, y);
        repaint();
    }
    public void paint(Graphics g){
        for(Line line: lines){
                g.setColor(color);
                g.drawLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY());
        }
    }
    public void record(int x, int y) {
        lastX = x;
        lastY = y;
    }
    public void actionPerformed(ActionEvent ae){

        if (ae.getSource() == red){
            setColor(Color.red);
        }
        else if (ae.getSource() == blue){
            setColor(Color.blue);
        }
        else if (ae.getSource() == green){
            setColor(Color.green);
        }
        else if (ae.getSource() == black){
            setColor(Color.black);
        }
    }
}

class Line{

    int startx, starty, endx, endy;


    public Line(){}
    public Line(int startx, int starty, int endx, int endy){
        setStartX(startx);
        setStartY(starty);
        setEndX(endx);
        setEndY(endy);

    }
    public void setStartX(int startx){
        this.startx = startx;
    }
    public void setStartY(int starty){
        this.starty = starty;
    }
    public void setEndX(int endx){
        this.endx = endx;
    }
    public void setEndY(int endy){
        this.endy = endy;
    }
    public int getStartX(){
        return startx;
    }
    public int getStartY(){
        return starty;
    }
    public int getEndX(){
        return endx;
    }
    public int getEndY(){
        return endy;
    }
}

    import java.awt.*;

public class DoubleBuffer extends Panel{

    private int bufferWidth;
    private int bufferHeight;
    private Image bufferImage;
    private Graphics bufferGraphics;




    public DoubleBuffer(){

        super();
    }

    public void update(Graphics g){
            paintBuffer(g);
    }

    public void paintBuffer(Graphics g){
        //    checks the buffersize with the current panelsize
        //    or initialises the image with the first paint
    if(bufferWidth!=getSize().width || bufferHeight!=getSize().height ||bufferImage==null || bufferGraphics==null)
            resetBuffer();
        if(bufferGraphics!=null){
            //this clears the offscreen image, not the onscreen one
            bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);

            //calls the paintbuffer method with
            //the offscreen graphics as a param
            paint(bufferGraphics);

            //we finaly paint the offscreen image onto the onscreen image
            g.drawImage(bufferImage,0,0,this);
        }

    }

    public void paint(Graphics g){

            //in classes extended from this one, add something to paint here!
            //always remember, g is the offscreen graphics
    }
    private void resetBuffer(){
        // always keep track of the image size
        bufferWidth=getSize().width;
        bufferHeight=getSize().height;

        //    clean up the previous image
        if(bufferGraphics!=null){
            bufferGraphics.dispose();
            bufferGraphics=null;
        }
        if(bufferImage!=null){
            bufferImage.flush();
            bufferImage=null;
        }
        System.gc();

        //    create the new image with the size of the panel
        bufferImage=createImage(bufferWidth,bufferHeight);
        bufferGraphics=bufferImage.getGraphics();
    }
}

1 个答案:

答案 0 :(得分:0)

您的Line课程应该存储颜色。填写mouseDragged并像这样使用

public void paint(Graphics g){
    for(Line line: lines){
            g.setColor(line.getColor());
            g.drawLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY());
    }
}