[当我在Java Paint程序中更改颜色时,整个颜色都会更改

问题描述 投票:1回答:2

我使用JPanel和Graphics用Java创建了程序。这是一个简单的“ Paint Panel”程序,当您拖动鼠标并想用不同的颜色绘制线条时,只需在面板上按一下按钮即可绘制内容,但问题是当我在面板上按一个按钮并拖动鼠标整个组件时面板中显示的颜色变成该颜色(按钮的颜色)。

代码:

public class PaintAssign extends JPanel {
    private ArrayList<Point> points= new ArrayList<>();
    private ArrayList<Point> points2= new ArrayList<>();
    public final JButton[] panelButton=new JButton[5];
    public String[] colors={"RED","BLUE","GREEN","YELLOW","CYAN"};
    int x=0;

    public PaintAssign()
    {  addMouseMotionListener(
        new MouseMotionAdapter(){
           @Override
            public void mouseDragged(MouseEvent e)
            {

                points.add(e.getPoint());

                repaint();
            }
        });

        addMouseListener(
        new MouseAdapter(){

       });

        for (int i = 0; i < 5; i++) {
            Rectangle r = new Rectangle(22, 22);                
            panelButton[i] = new JButton();
            panelButton[i].setText(colors[i]);
            panelButton[i].setOpaque(true);
            panelButton[i].setBounds(r);
            this.add(panelButton[i]);
            this.setVisible(true);
        }

        mouseAction handle=new mouseAction();
        panelButton[0].addActionListener(handle);
        panelButton[1].addActionListener(handle);
        panelButton[2].addActionListener(handle);
        panelButton[3].addActionListener(handle);
        panelButton[4].addActionListener(handle);

   }

   private class mouseAction implements ActionListener
    {
       @Override
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==panelButton[0])
            {
                x=1;
            }
            else if(e.getSource()==panelButton[1])
            {
                x=2;
            }
            else if(e.getSource()==panelButton[2])
            {

                x=3;
            }

            else if(e.getSource()==panelButton[3])
            {
                x=4;
            }
            else if(e.getSource()==panelButton[4])
            {

                x=5;
             }
        }

    }

   public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(x==0)
        {
            g.setColor(Color.BLACK);
        }
        else if(x==1)
        {
            g.setColor(Color.RED);
        }

        else if(x==2)
        {
            g.setColor(Color.BLUE);
        }
        else if(x==3)
        {
            g.setColor(Color.GREEN);
        }
        else if(x==4)
        {
            g.setColor(Color.YELLOW);
        }else if(x==5)
        {
            g.setColor(Color.CYAN);
        }



        for(Point i:points)
        {
            g.fillOval(i.x,i.y,15,15);
        }


    }
java jpanel paintcomponent
2个回答
0
投票

您没有提供repaint()方法,但是我的猜测是,由于您将所有点记录到同一列表中,因此所有点都以相同的颜色重新绘制。您需要的是绘制的每个组件的单独列表,以一种在选定组件时可以访问该列表的方式进行管理。

虽然我不确定我是否正确理解了您的问题,也许您可​​以提供更多详细信息。


0
投票

paintComponent方法在每次面板重绘时都会被调用,这种情况经常发生。然后,当您设置颜色时,所有点都将使用新颜色重新绘制。

为了避免这种行为,您不仅必须保存点的坐标,还必须保存其颜色。

public class PaintAssign extends JPanel {
    private ArrayList<Point> points= new ArrayList<>();
    private ArrayList<Color> colors = new ArrayList<>();

   //....

  public PaintAssign(){  
      addMouseMotionListener(new MouseMotionAdapter(){
          @Override
          public void mouseDragged(MouseEvent e) {
            points.add(e.getPoint());
            if(x==0) {
                 colors.add(Color.BLACK);
            } else if(x==1) {
                 colors.add(Color.RED);
            } else if(x==2) {
                 colors.add(Color.BLUE);
            } else if(x==3) {
                 colors.add(Color.GREEN);
            } else if(x==4) {
                 colors.add(Color.YELLOW);
            }else if(x==5) {
                 colors.add(Color.CYAN);
            }

            repaint();
        }
    });

    // ...

   public void paintComponent(Graphics g) {
       super.paintComponent(g);

       for (int i = 0; i < points.size(); i++) {
           g.setColor(colors.get(i));
           g.fillOval(points.get(i).x, points.get(i).y, 15, 15);
       }
    }
}

考虑创建一个新的类Point,其中包含该点的坐标和颜色。

© www.soinside.com 2019 - 2024. All rights reserved.