Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 绘制不同形状时JColorChooser颜色出现问题_Java_Swing_User Interface_Colors_Rectangles - Fatal编程技术网

Java 绘制不同形状时JColorChooser颜色出现问题

Java 绘制不同形状时JColorChooser颜色出现问题,java,swing,user-interface,colors,rectangles,Java,Swing,User Interface,Colors,Rectangles,我正在用java实现一个绘画应用程序。我在选择所绘制形状的颜色时遇到问题。我可以选择颜色并用它来画画,但是如果我画一个颜色变为蓝色,画一个蓝色的矩形,把颜色变为红色,画一个红色的矩形,它也会把蓝色的矩形变为红色,每一个形状都会发生。我想这是因为我把形状保存在一个arrayList中,它会改变所有形状的颜色,但是,我有不同形状的arrayList 我正在尝试将颜色保存为静态颜色1,并使用setPaint(颜色1)将颜色更改为所选颜色。任何帮助都将不胜感激 下面是我的部分代码: public c

我正在用java实现一个绘画应用程序。我在选择所绘制形状的颜色时遇到问题。我可以选择颜色并用它来画画,但是如果我画一个颜色变为蓝色,画一个蓝色的矩形,把颜色变为红色,画一个红色的矩形,它也会把蓝色的矩形变为红色,每一个形状都会发生。我想这是因为我把形状保存在一个arrayList中,它会改变所有形状的颜色,但是,我有不同形状的arrayList

我正在尝试将颜色保存为静态颜色1,并使用setPaint(颜色1)将颜色更改为所选颜色。任何帮助都将不胜感激

下面是我的部分代码:

  public class PaintAppFrame extends JFrame implements MouseListener,    MouseMotionListener, ActionListener {
static final long serialVersionUID = 2L;


static int flag = 0;
// -------------------------------------------------------------------------

private JButton changeColor;
private JButton rectButton;
private JButton rectfillButton;
private JButton lineButton;
static Color color1;


static Graphics2D gr;

public static ArrayList<Shape> rectStruct = new ArrayList<Shape>();
public static ArrayList<Shape> rectFillStruct = new ArrayList<Shape>();
public static ArrayList<Shape> lineStruct = new ArrayList<Shape>();

private static Point mouseStart;
private static Point mouseEnd;

    changeColor = new JButton("CHANGE COLOR");
    changeColor.setActionCommand("color");
    changeColor.addActionListener(this);


    // added the menu goes here
    image = Toolkit.getDefaultToolkit().getImage(".");
    paintPanel = new PaintPanel();
    paintPanel.addMouseMotionListener(this);
    paintPanel.addMouseListener(this);

    // make this a toolbar and images

    Icon lineIcon = new ImageIcon("icons/line.png");
    Icon rgbIcon = new ImageIcon("icons/color.png");
    Icon rectIcon = new ImageIcon("icons/rect.png");
    Icon rectfillIcon = new ImageIcon("icons/fullRect.png");

    changeColor = new JButton(rgbIcon);
    changeColor.setActionCommand("color");
    changeColor.addActionListener(this);

    rectButton = new JButton(rectIcon);
    rectButton.setActionCommand("rectangle");
    rectButton.addActionListener(this);

    rectfillButton = new JButton(rectfillIcon);
    rectfillButton.setActionCommand("rectanglefill");
    rectfillButton.addActionListener(this);

    lineButton = new JButton(lineIcon);
    lineButton.setActionCommand("line");
    lineButton.addActionListener(this);

    JPanel buttons = new JPanel(new GridLayout());
    buttons.setBorder(BorderFactory.createRaisedSoftBevelBorder());
    buttons.setLayout(new GridLayout(16, 2));

    buttons.add(changeColor);
    buttons.add(rectButton);
    buttons.add(rectfillButton);
    buttons.add(lineButton);


    paintCanvas = new JPanel(new BorderLayout());
    paintCanvas.add(paintPanel, "Center");
    paintCanvas.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    paintCanvas.setBackground(new Color(100, 100, 142));
    paintCanvas.setPreferredSize(new Dimension(650, 520));
    paintCanvas.setMaximumSize(new Dimension(2000, 1600));

}

public void mouseDragged(MouseEvent me) {


    if (flag == 1) {
        mouseEnd = new Point(me.getX(), me.getY());
        repaint();
        System.out.println("rect dragged");
    }


    else if (flag == 3) {
        mouseEnd = new Point(me.getX(), me.getY());
        repaint();
    }

    } else if (flag == 7) {
        mouseEnd = new Point(me.getX(), me.getY());
        repaint();
    }



}

public void mouseMoved(MouseEvent me) {
}

public void mouseClicked(MouseEvent me) {

}

public void mouseEntered(MouseEvent me) {
}

public void mouseExited(MouseEvent me) {
}

public void mousePressed(MouseEvent me) {


    if (flag == 1) {
        mouseStart = new Point(me.getX(), me.getY());
        mouseEnd = mouseStart;
        repaint();
    }



    else if (flag == 3) {
        mouseStart = new Point(me.getX(), me.getY());
        mouseEnd = mouseStart;
        repaint();
    }


    else if (flag == 7) {
        mouseStart = new Point(me.getX(), me.getY());
        mouseEnd = mouseStart;
        repaint();
    } 
}

public void mouseReleased(MouseEvent me) {

    } else if (flag == 1) {
        Shape r = createRect(mouseStart.x, mouseStart.y, me.getX(), me.getY());
        rectStruct.add(r);
        mouseStart = null;
        mouseEnd = null;
        repaint();
    }

    else if (flag == 3) {
        Shape r = createFillRect(mouseStart.x, mouseStart.y, me.getX(), me.getY());
        rectFillStruct.add(r);
        mouseStart = null;
        mouseEnd = null;
        repaint();
    }


    else if (flag == 7) {
        Shape r = createLine(mouseStart.x, mouseStart.y, me.getX(), me.getY());
        lineStruct.add(r);
        mouseStart = null;
        mouseEnd = null;
        repaint();
    } 
}

@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent ae) {
    String command = ae.getActionCommand();
    Object source = ae.getSource();
    // instantiate the filechooser
    switch (command) {

    case "color":
        this.setPaintColor();
        break;

    case "rectangle":
        flag = 1;
        FLAG = 0;
        this.paintRect(gr);
        break;

    case "rectanglefill":
        flag = 3;
        this.paintRectFill(gr);
        break;


    case "line":
        flag = 7;
        this.paintLine(gr);
        break;


private void setPaintColor() {
    Color color = JColorChooser.showDialog(null, "Choose Paint Color", Color.black);
    color1 = color;
}


public static void paintRect(Graphics g) {

    gr = (Graphics2D) g;

    for (Shape s : rectStruct) {
        gr.setPaint(color1);
        gr.setStroke(new BasicStroke(2));

        gr.draw(s);
        // gr.setPaint(this.LINE_COLOR);
        // gr.fill(s);
    }
    if (flag == 1) {
        if (mouseStart != null && mouseEnd != null) {

        //makes outline while dragging rectangle
            gr.setPaint(Color.RED);
            Shape r = createRect(mouseStart.x, mouseStart.y, mouseEnd.x, mouseEnd.y);
            gr.draw(r);
        }
    }
}

public static void paintRectFill(Graphics g) {
    gr = (Graphics2D) g;

  for (Shape s : rectFillStruct) {
        gr.setPaint(color1);
        gr.setStroke(new BasicStroke(2));

        gr.draw(s);
        gr.fill(s);
    }
    if (flag == 3) {

        if (mouseStart != null && mouseEnd != null) {
            //makes outline while dragging rectangle
            gr.setPaint(Color.RED);
            Shape r = createFillRect(mouseStart.x, mouseStart.y, mouseEnd.x, mouseEnd.y);
            gr.draw(r);
        }
    }
}


public static void paintLine(Graphics g) {
    gr = (Graphics2D) g;

    for (Shape s : lineStruct) {
        gr.setPaint(color1);
        gr.setStroke(new BasicStroke(2));

        gr.draw(s);
        gr.fill(s);

    }

    if (flag == 7) {
        if (mouseStart != null && mouseEnd != null) {
            //makes outline while dragging rectangle
            gr.setPaint(Color.GREEN);
            Shape r = createLine(mouseStart.x, mouseStart.y, mouseEnd.x, mouseEnd.y);
            gr.draw(r);
        }
    }
}

public static Rectangle2D.Float createRect(int x1, int y1, int x2, int y2) {
    return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}


public static Rectangle2D.Float createFillRect(int x1, int y1, int x2, int y2) {
    return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}


public static Line2D.Float createLine(int x1, int y1, int x2, int y2) {
    return new Line2D.Float(x1, y1, x2, y2);
}}
公共类PaintAppFrame扩展JFrame实现MouseListener、MouseMotionListener、ActionListener{
静态最终长SerialVersionId=2L;
静态int标志=0;
// -------------------------------------------------------------------------
私人按钮变色;
私有JButton按钮;
私有JButton-rectfillButton;
私有JButton行按钮;
静态颜色1;
静态图形2d-gr;
公共静态ArrayList rectStruct=新ArrayList();
public static ArrayList rectFillStruct=new ArrayList();
public static ArrayList lineStruct=new ArrayList();
专用静态点mouseStart;
专用静态点鼠标;
changeColor=新的JButton(“更改颜色”);
changeColor.setActionCommand(“颜色”);
changeColor.addActionListener(此);
//这里有菜单
image=Toolkit.getDefaultToolkit().getImage(“.”);
paintPanel=新的paintPanel();
paintPanel.addMouseMotionListener(此);
paintPanel.addMouseListener(本);
//使其成为工具栏和图像
Icon lineIcon=新图像图标(“icons/line.png”);
Icon rgbIcon=新图像图标(“icons/color.png”);
Icon-rectIcon=新图像图标(“icons/rect.png”);
Icon-rectfillIcon=newimageicon(“icons/fullRect.png”);
changeColor=新的JButton(rgbIcon);
changeColor.setActionCommand(“颜色”);
changeColor.addActionListener(此);
rectButton=新的JButton(rectIcon);
setActionCommand(“矩形”);
rectButton.addActionListener(这个);
rectfillButton=newjbutton(rectfillIcon);
setActionCommand(“矩形填充”);
rectfillButton.addActionListener(此);
lineButton=新的JButton(lineIcon);
lineButton.setActionCommand(“行”);
lineButton.addActionListener(此);
JPanel buttons=newjpanel(newgridlayout());
buttons.setOrder(BorderFactory.CreateRaisedSoftBevelOrder());
按钮。设置布局(新的网格布局(16,2));
按钮。添加(更改颜色);
按钮。添加(rectButton);
按钮。添加(rectfillButton);
按钮。添加(lineButton);
paintCanvas=newjpanel(newborderlayout());
paintCanvas.add(paintPanel,“中心”);
paintCanvas.setBorder(BorderFactory.createEmptyByOrder(5,5,5,5));
油画画布。背景(新颜色(100100142));
paintCanvas.setPreferredSize(新尺寸(650520));
paintCanvas.setMaximumSize(新尺寸(20001600));
}
公共无效鼠标标记(MouseEvent me){
如果(标志==1){
mouseEnd=新点(me.getX(),me.getY());
重新油漆();
System.out.println(“矩形拖动”);
}
else if(标志==3){
mouseEnd=新点(me.getX(),me.getY());
重新油漆();
}
}else if(标志==7){
mouseEnd=新点(me.getX(),me.getY());
重新油漆();
}
}
public void mouseMoved(MouseEvent me){
}
公共无效mouseClicked(MouseEvent me){
}
公共无效mouseenterned(MouseEvent me){
}
public void mouseexitted(MouseEvent me){
}
public void mousePressed(MouseEvent me){
如果(标志==1){
mouseStart=新点(me.getX(),me.getY());
mouseEnd=mouseStart;
重新油漆();
}
else if(标志==3){
mouseStart=新点(me.getX(),me.getY());
mouseEnd=mouseStart;
重新油漆();
}
else if(标志==7){
mouseStart=新点(me.getX(),me.getY());
mouseEnd=mouseStart;
重新油漆();
} 
}
公共无效MouseEvent me(MouseEvent me){
}else if(标志==1){
Shape r=createRect(mouseStart.x,mouseStart.y,me.getX(),me.getY());
rectStruct.add(r);
mouseStart=null;
mouseEnd=null;
重新油漆();
}
else if(标志==3){
Shape r=createFillRect(mouseStart.x,mouseStart.y,me.getX(),me.getY());
rectFillStruct.add(r);
mouseStart=null;
mouseEnd=null;
重新油漆();
}
else if(标志==7){
Shape r=createLine(mouseStart.x,mouseStart.y,me.getX(),me.getY());
lineStruct.add(r);
mouseStart=null;
mouseEnd=null;
重新油漆();
} 
}
@抑制警告(“静态访问”)
已执行的公共无效行动(行动事件ae){
String command=ae.getActionCommand();
Object source=ae.getSource();
//实例化filechooser
开关(命令){
案例“颜色”:
这个.setPaintColor();
打破
案例“矩形”:
flag=1;
FLAG=0;
这是paintRect(gr);
打破
案例“矩形填充”:
flag=3;
这是一种直接填充(gr);
打破
案例“行”:
flag=7;
这是漆线(gr);
打破
私有void setPaintColor(){
Color Color=JColorChooser.showDialog(null,“选择绘制颜色”,Color.black);
颜色1=颜色;
}
公共静态void paintRect(图形g){
gr=(s2d)g;
对于(形状s:rectStruct){
gr.setPaint(颜色1);
gr.设定行程(新基本行程(2));
gr.draw(s);
//gr.setPaint(该线条颜色);
//垃圾填埋场;
}
如果(标志==1){
if(mouseStart!=null&&mouseEnd!=null){
//拖动矩形时生成轮廓
gr.setPaint(颜色为红色);
形状r=createRect
import java.awt.*;

public class ColoredShape {
    private Shape shape;
    private Color color;

    public ColoredShape(Shape shape, Color color) {
        this.shape = shape;
        this.color = color;
    }

    public Shape getShape() {
        return shape;
    }

    public Color getColor() {
        return color;
    }
}
// Field definition change:
//public static ArrayList<Shape> rectStruct = new ArrayList<Shape>();
public static ArrayList<ColoredShape> rectStruct = new ArrayList<ColoredShape>();

// Method mouseReleased change:
//rectStruct.add(r);
rectStruct.add(new ColoredShape(r, color1));

// Method paintRect changes:
//for (Shape s : rectStruct) {
    //gr.setPaint(PaintPanel.LINE_COLOR);
    //[...]
    //gr.draw(s);
for (ColoredShape coloredShape : rectStruct) {
    gr.setPaint(coloredShape.getColor());
    [...]
    gr.draw(coloredShape.getShape());
// Field definition added:
private static ArrayList<ColoredShape> redoStructNew = new ArrayList<ColoredShape>();

// Method undo change:
//redoStruct.add(PaintAppFrame.rectStruct.get(i));
redoStructNew.add(PaintAppFrame.rectStruct.get(i));

// Method redo changes:
//if (!PaintAppFrame.rectStruct.isEmpty() && !redoStruct.isEmpty()) {
    //for (int i = 0; i < redoStruct.size(); i++) {
        //PaintAppFrame.rectStruct.add(redoStruct.get(i));
    //[...]
if (!PaintAppFrame.rectStruct.isEmpty() && !redoStructNew.isEmpty()) {
    for (int i = 0; i < redoStructNew.size(); i++) {
        PaintAppFrame.rectStruct.add(redoStructNew.get(i));
    [...]
import java.awt.*;
import java.awt.geom.Line2D;

public class LineSegment {
    private Line2D.Double coordinates;
    private Color color;
    private Stroke stroke;

    public LineSegment(Line2D.Double coordinates, Color color, Stroke stroke) {
        this.coordinates = coordinates;
        this.color = color;
        this.stroke = stroke;
    }

    public void draw(Graphics2D graphics2D) {
        graphics2D.setColor(color);
        graphics2D.setStroke(stroke);
        graphics2D.draw(coordinates);
    }
}
import java.awt.Graphics2D;

public abstract class GraphicalObject {
    public abstract void draw(Graphics2D graphics2D);
}
public class LineSegment extends GraphicalObject {
    // [...]

    // Each class should implement the draw method:
    public void draw(Graphics2D graphics2D) {
        // [...]
    }
}
private List<GraphicalObject> graphicalObjects = new ArrayList<>();

//public static ArrayList<ColoredShape> rectStruct = new ArrayList<ColoredShape>();
// [...]
//public static ArrayList<ColoredShape> lineStruct = new ArrayList<ColoredShape>();
paintInkStrokes(g);
paintEraser(g);
PaintAppFrame.paintRect(g);
PaintAppFrame.paintCircle(g);
PaintAppFrame.paintRectFill(g);
PaintAppFrame.paintFillCircle(g);
PaintAppFrame.paintRoundRectangle(g);
PaintAppFrame.paintRoundRectangleFill(g);
PaintAppFrame.paintLine(g);
paintEntities(g);