Graphics.drawOval处的NullPointerException [英] NullPointerException at Graphics.drawOval

查看:278
本文介绍了Graphics.drawOval处的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单。我创建了一个名为handler的类,在其构造函数中它包含一个c参数,一个JComponent。在某个JComponent(最好是JPanel)上调用此构造函数时,将在鼠标的当前坐标处绘制一个椭圆。这是源代码:

  import javax.swing。*; 
import java.awt。*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

公共类Handler实现MouseListener,MouseMotionListener {

图形g;

public Handler(){}

public Handler(JComponent c){

if(c instanceof JPanel){
g = c .getGraphics();
g.drawOval(mx,my,5,5);
}

if(c!= null){
c.addMouseListener(this);
c.addMouseMotionListener(this);
}
}

int mx,my;


public void mouseClicked(MouseEvent e){
mx = e.getX();
my = e.getY();
}

public void mousePressed(MouseEvent e){
mx = e.getX();
my = e.getY();
}

public void mouseReleased(MouseEvent e){
mx = e.getX();
my = e.getY();
}

public void mouseExited(MouseEvent e){}
$ b public void mouseEntered(MouseEvent e){}
$ b $ public void mouseMoved (MouseEvent e){
mx = e.getX();
my = e.getY();
}

public void mouseDragged(MouseEvent e){}
}

然而,抛出了这个错误:

 线程中的异常AWT-EventQueue-0java.lang .NullPointerException 

你们有什么想法吗?如果是这样,请发布解决方案。



编辑1

我已经做了一些新的事情。这是我的全部新代码:

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

public class Handler扩展JPanel {

int mx = MouseInfo.getPointerInfo()。getLocation()。x;
int my = MouseInfo.getPointerInfo()。getLocation()。y;


public Handler(BorderLayout bl){
this.setLayout(bl);


public void paintComponent(Graphics g23){
Graphics2D g2 =(Graphics2D)g23;
g2.drawOval(mx,my,30,30);
}
}

我重新编写了代码。现在,它扩展了JPanel并作为JPanel的替代品。因此,我没有实例化一个新的JPanel,而是调用Handler的构造函数。它也实现了paintComponent,但椭圆还没有被绘制。然而,它不会产生任何错误。

解决方案


我已经彻底研究过这个问题, ,

阅读 Custom Painting。



你不应该使用getGraphics()方法。这种绘画不是永久的。如果您需要更多帮助,请发布您的 SSCCE 。我们没有时间通过​​在网络上追踪代码片段来猜测您的代码是什么。


The question is simple. I have created a class named "handler" and within its constructor it contains a parameter for "c", a JComponent. When this constructor is called on a certain JComponent, preferably a JPanel, an oval is drawn at the mouse's current coordinates. This is the source code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class Handler implements MouseListener, MouseMotionListener {

Graphics g;

public Handler() {}

public Handler(JComponent c) {

    if (c instanceof JPanel) {
        g = c.getGraphics();
        g.drawOval(mx, my, 5, 5);
    }

    if (c != null) {
        c.addMouseListener(this);
        c.addMouseMotionListener(this);
    }
}

int mx, my;


public void mouseClicked(MouseEvent e) {
    mx = e.getX();
    my = e.getY();
}

public void mousePressed(MouseEvent e) {
    mx = e.getX();
    my = e.getY();
}

public void mouseReleased(MouseEvent e) {
    mx = e.getX();
    my = e.getY();
}

public void mouseExited(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {
    mx = e.getX();
    my = e.getY();
}

public void mouseDragged(MouseEvent e) {}
}

However, this error is thrown:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Do you guys have any idea about this? If so, please post a solution.

Edit 1
I've done something new. This is my entire new code:

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

public class Handler extends JPanel {

   int mx = MouseInfo.getPointerInfo().getLocation().x;
   int my = MouseInfo.getPointerInfo().getLocation().y;


public Handler(BorderLayout bl) {
    this.setLayout(bl);
}

public void paintComponent(Graphics g23) {
    Graphics2D g2 = (Graphics2D) g23;
    g2.drawOval(mx, my, 30, 30);
    }
}

I've reworked the code greatly. Now, it extends JPanel and serves as a replacement to JPanel. So instead of instantiating a new JPanel, I'd call Handler's constructor. It also implements paintComponent, yet the oval still isn't being drawn. It yields no errors, however.

解决方案

I have thoroughly researched this question and have found no solution,

Read the section from the Swing tutorial on Custom Painting..

You should not be using the getGraphics() method. This kind of painting is NOT permanent. As soon a Swing determines the component needs to be repainted you will lose the painting.

If you need more help then post your SSCCE here. We dpm't have time to guess what you code is like by chasing code snippets all over the web.

这篇关于Graphics.drawOval处的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆