【问题标题】:Whole Colors change when I change Colors in my Java Paint Program当我在我的 Java Paint 程序中更改颜色时,整个颜色都会改变
【发布时间】:2020-03-26 05:08:32
【问题描述】:

我使用 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 swing jpanel paintcomponent


【解决方案1】:

每次重绘面板时都会调用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。

【讨论】:

  • @AndrewThompson 你是对的,创建一个封装这两个属性的类会更好。我只是在答案的最后指出了这一点,但没有给出代码。我会更新我的答案或随时更新。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2018-12-30
    • 2012-07-05
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多