【问题标题】:Changing the color in my java paint program更改我的 java 绘画程序中的颜色
【发布时间】:2012-07-05 23:33:46
【问题描述】:

我可以让程序启动并使用一种颜色进行绘制,但如果我单击其中一个按钮,它不会改变颜色。我不断收到空指针异常。我认为这可能是因为我没有初始化图形、图像或两者。如果是那个问题,我不太确定如何解决这个问题。我对编程很陌生,任何帮助都将不胜感激。

代码:

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


    public class Paint extends JFrame{
        //Class Variables
        private JFrame frame;
        PaintPanel paint_panel;
        private JPanel btn_panel;
        private JButton red_btn, green_btn, blue_btn, clear_btn, erase_btn ;



        //class to paint the panel in the frame
        public class PaintPanel extends JComponent{     
            //Class Variables
            private Graphics2D g;
            private int x1, y1, x2, y2;
            private Cursor paint_cursor, select_cursor;
            private Image image;

            //Constructor with Mouse listeners in it  
            public PaintPanel(){

                 addMouseListener(new MouseAdapter(){
                     public void mousePressed(MouseEvent e){
                             x1 = e.getX();
                             y1 = e.getY();
                             }
                    });
                 addMouseMotionListener(new MouseMotionAdapter(){

                     public void mouseDragged(MouseEvent e){
                             x2 = e.getX();
                             y2 = e.getY();

                             if(g != null)
                                 g.drawLine(x1, y1, x2, y2);
                             repaint();
                             x1 = x2;
                             y1 = y2;
                     }
             });
             }
            //PaintPanel Method that sets the cursor and does the drawing
            public void paintComponent(Graphics gr){
                super.paintComponent(gr);
                gr.setColor(Color.black);
                gr.fillRect(0, 0, this.getWidth(), this.getHeight());
                paint_cursor=new Cursor(Cursor.CROSSHAIR_CURSOR);
                select_cursor=new Cursor(Cursor.HAND_CURSOR);
                paint_panel.setCursor(paint_cursor);
                btn_panel.setCursor(select_cursor);
                if(image == null){
                    image = createImage(this.getWidth(), this.getHeight());
                    g = (Graphics2D)image.getGraphics();
                    clear1();
                 }
                 gr.drawImage(image, 0, 0, null);
         }
             public void clear1(){
                g.setPaint(Color.black);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                g.setPaint(Color.blue);
                repaint();  
             }
             public void red(){ 
                 g.drawLine(x1, y1, x2, y2);
                 g.setPaint(Color.red);

                 x1 = x2;
                 y1 = y2;
                 repaint();
             }
             public void blue(){    
                 g.drawLine(x1, y1, x2, y2);
                 g.setPaint(Color.blue);

                 x1 = x2;
                 y1 = y2;
                 repaint();
             }
             public void green(){   
                g.drawLine(x1, y1, x2, y2);
                g.setPaint(Color.green);

                 x1 = x2;
                 y1 = y2;
                 repaint();
             }
            }

        //Constructor
        public Paint(){

            frame = new JFrame("Paint Program");
            frame.setSize(500,500);
            frame.setLocation(500,100);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



            red_btn = new JButton("RED");
            green_btn = new JButton("GREEN");
            blue_btn = new JButton("BLUE");
            clear_btn = new JButton("CLEAR");
            erase_btn = new JButton("ERASE");

            btn_panel = new JPanel();
            paint_panel = new PaintPanel();

            btn_panel.setLayout(new GridLayout(5,1));

            btn_panel.add(red_btn);
            btn_panel.add(green_btn);
            btn_panel.add(blue_btn);
            btn_panel.add(clear_btn);
            btn_panel.add(erase_btn);
            frame.add(BorderLayout.CENTER, paint_panel);
            frame.add(BorderLayout.EAST, btn_panel);

            final PaintPanel pp1 = new PaintPanel();

            red_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.red();
                }   
                });
            blue_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.blue();
                }   
                });
            green_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.green();
                }   
                });
            clear_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.clear1();
                }   
                });
            erase_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.green();
                }   
                });
        }
        //Makes the frame visible and working
        public void launch(){
            frame.setVisible(true);
        }

        //Main Method
        public static void main(String[] args){
            Paint p1 = new Paint();
            p1.launch();
        }
    }

【问题讨论】:

  • NPE 被抛出哪一行?
  • 欢迎来到 Stack Overflow!请将您的代码示例限制在相关部分。理想的样本是short, self-contained and compilable。通读整个源代码很费时间,而且你得到的答案也会更少。
  • private Graphics2D g; 问题从这里开始,我怀疑.. 图形对象通常是瞬态的——一去不复返。
  • 我点击红色按钮,例如它在红色方法中的第 69 行 --> g.drawLine(x1, y1, x2, y2);
  • 和 red_btn 动作监听器中的第 127 行

标签: java swing


【解决方案1】:

您正在从按钮侦听器调用 red() 方法,并且在调用 drawLine 时使用 x1、x2、y1 和 y2 值。你在哪里初始化这些变量?

你所依赖的那些变量应该是在用鼠标点击的时候写的,但那可能不会发生。尝试在构造函数中初始化它们。

【讨论】:

    【解决方案2】:

    存储图形对象会给您带来麻烦。简单的答案是所有绘画都需要通过 JComponent.paintComponent(...) 完成,因此您的 red() green() blue() 应该由 paintComponent 使用来自 paintComponent 的 Graphics 对象调用

    从根本上说,java 绘画的工作方式是通过对paint()/paintcomponent() 的回调,在同一个循环中绘制所有内容。该机制以这种方式工作,以便组件在不同时间看起来不同,具有不同的鼠标/单击状态等

    【讨论】:

      猜你喜欢
      • 2014-08-30
      • 2020-03-26
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多