0

基本上我正在制作一个你用鼠标绘制的画板;有一组颜色按钮可供选择,它们是在自定义类 ColoredButton 中制作的,颜色参数由 setColor(int r,g,b) 设置。

因此,在 ColourToolbar 类中,当您单击其中一个 ColoredButtons 时,它会将全局变量 selectedCOlor 设置为该按钮中设置的颜色(如上所述),然后 DrawBoard 类使用 getColor() 获取“selectedCOlor”中的颜色ColourToolbar 类中的变量。

  • 我遇到的问题是我在 DrawBoard 中得到了 NullPointerException,所以颜色参数永远不会到达那里,我现在尝试了代码中显示的类似方法,但都显示 NullPointerException。

这是课程(我取出了不需要的代码):

public class ColoredButton extends JButton {

   public Color color;


   public ColoredButton(ImageIcon img){

       super(img);
   }

   public void setColor(int r, int g, int b){

       this.color = new Color(r,g,b);
   }

   public Color getColor(){

       return this.color;
   }

}

下一节课:

public class ColourToolbar extends JPanel implements ActionListener{

public Color selectedColor;
public boolean colorSelected = false;

public ColoredButton pink,black,blue,green,orange,yellow,darkp,red,white;

public ColourToolbar(){

    setBackground(Color.DARK_GRAY);

    setLayout(new GridBagLayout());

    Dimension size = getPreferredSize();
    size.setSize(1024,80); //w, h
    setPreferredSize(size);

    // NOTE: There are 8 more blocks of codes like the one below, left for example.

    ImageIcon blackicon = new ImageIcon(getClass().getResource("Icons/Colours/Black.png"));
        black = new ColoredButton(new ImageIcon(blackicon.getImage().getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH)));
        gbc.gridx = 7;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 0.5;    
        black.setColor(255, 255, 255);
        black.setOpaque(false);
        black.setContentAreaFilled(false);
        black.setBorderPainted(false);
        black.setActionCommand("orange");
        black.addActionListener(this);
        gbc.insets = new Insets(0,0,0,0); //top, left, bottom, right
        add(black, gbc);


}

@Override
public void actionPerformed(ActionEvent e){

    // all colors: black, blue, green, orange, *pink, yellow, darkpink, *red, white

    if("black".equalsIgnoreCase(e.getActionCommand())){

        this.selectedColor = black.getColor();
        System.out.println("black= " + selectedColor.toString());
        this.colorSelected = true;
    }      
}

public Color getCurrColor(){

    return this.selectedColor;
}


}

我得到 NullPointerException 的最后一堂课:

public class DrawBoard extends JPanel implements MouseListener, MouseMotionListener{

public JLabel status;
private JLabel imgLabel; // this is where the drawing happens
private List<Point> points = new ArrayList<Point>();
private List<BufferedImage> lines = new ArrayList<BufferedImage>();

private static final int BI_WIDTH = 1024;
private static final int BI_HEIGHT = 800;

private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT,
        BufferedImage.TYPE_INT_ARGB);

public Color currentColor;

public DrawBoard(){  

   Graphics2D g2d = bImage.createGraphics();
   g2d.dispose();

    Dimension size = getPreferredSize();
    size.setSize(1024,800); //w, h
    setPreferredSize(size);
    status =  new JLabel("default");
    add(status, BorderLayout.SOUTH);
    addMouseListener(this);
    addMouseMotionListener(this);  


     imgLabel = new JLabel(new ImageIcon(bImage)) {
     @Override
     protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        paintInLabel(g);
     }
   };
     imgLabel.setOpaque(false);
     setOpaque(false);
      add(imgLabel, BorderLayout.CENTER);

}

private void paintInLabel(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.setColor(getColor()); // this colour is when mouse is pressed
  g2d.setStroke(new BasicStroke(5));
  if (points.size() < 2) {
     return;
  }
  for (int i = 1; i < points.size(); i++) {
     int x1 = points.get(i - 1).x;
     int y1 = points.get(i - 1).y;
     int x2 = points.get(i).x;
     int y2 = points.get(i).y;
     g2d.drawLine(x1, y1, x2, y2);
  }
}

// Where the drawing happens
@Override
public void mousePressed(MouseEvent e) {
    status.setText("you pressed down the mouse");
    this.pstart = e.getPoint();
    points.add(e.getPoint());

}

@Override
public void mouseDragged(MouseEvent e) {
  status.setText("you draged the mouse");
  points.add(e.getPoint());
  imgLabel.repaint();
}

@Override
public void mouseReleased(MouseEvent e) {
    status.setText("you release the mouse click");
    Graphics2D g2d = bImage.createGraphics();
    g2d.setColor(getColor()); // this is the final colour - 
    g2d.setStroke(new BasicStroke(5));

     if (points.size() >= 2) {
        for (int i = 1; i < points.size(); i++) {
           int x1 = points.get(i - 1).x;
           int y1 = points.get(i - 1).y;
           int x2 = points.get(i).x;
           int y2 = points.get(i).y;
           g2d.drawLine(x1, y1, x2, y2);
        }
     }
     g2d.dispose();

     points.clear();
     imgLabel.repaint();

}
// End of where the drawing happens


// BELOW IN THIS METHOD IS WHERE it gets the NullPointerException
private Color getColor() {
    ColourToolbar ct = new ColourToolbar();
    System.out.println(ct.getCurrColor().toString());

    return  ct.getCurrColor();
}

private void setColor(Color col){

    this.currentColor = col;
}

}

错误堆栈跟踪在这里:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at gui.DrawBoard.getColor(DrawBoard.java:162)
    at gui.DrawBoard.paintInLabel(DrawBoard.java:78)
    at gui.DrawBoard.access$000(DrawBoard.java:32)
    at gui.DrawBoard$1.paintComponent(DrawBoard.java:67)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3877)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
4

1 回答 1

5

好的,这就是问题所在:

ColourToolbar ct = new ColourToolbar();
System.out.println(ct.getCurrColor().toString());

getCurrColor()selectedColor在 ... 内返回,ColourToolbar但您没有... 构造函数没有将其设置为任何内容selectedColor所以值为null,当你打电话时toString()你会得到异常。

在您的描述中,您说:

它设置了一个全局变量 selectedCOlor

selectedColor不是“全局”变量——它是一个实例变量;每个ColourToolbar实例都有一个单独的selectedColor变量。

你为什么要在新 的上做这个ColourToolbar?您真的不想从现有工具栏中获取所选颜色吗?或者,如果您真的希望它是全局的(ick),它应该是一个静态变量,由静态方法获取,而您根本不需要创建新实例。

于 2013-02-26T19:11:57.300 回答