0
我有一個問題,我的java的程序以及這裏是程序;java swing n mouselistener
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestGraphic extends JFrame implements MouseListener{
JFrame az=new JFrame();
JPanel er=new JPanel();
Point a=new Point(220,300);
Rectangle r1;
Cercle c1;
int x,y;
Graphics g;
static void changeColor(Figure o) {
Color [] tab = Color.black, Color.blue, Color.cyan, Color.red, Color.green,
Color.magenta,Color.orange,Color.yellow};
for (int i=0; i<tab.length - 1; i++) {
if (o.getColor() == tab[i]) {
o.setColor(tab[i+1]);
break;
}
}
}
public void mouseClicked(MouseEvent e) {
if (r1.contains(e.getX(),e.getY())==true) {
changeColor(r1);
r1.draw(g);
}
if (c1.contains(e.getX(), e.getY())==true) {
}
//else{r1.setColor(Color.green);
//r1.draw(g);}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e){}
public TestGraphic() {
this.setSize(500, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
addMouseListener(this);
}
public void paint(Graphics g) {
r1=new Rectangle(a,30,70);
c1=new Cercle(150,150,200);
Cercle c2=new Cercle(185,180,40);
Cercle c3=new Cercle(265,180,40);
r1.setColor(Color.blue);
c1.setColor(Color.yellow);
c2.setColor(Color.green);
c3.setColor(Color.red);
c1.draw(g);
c2.draw(g);
c3.draw(g);
r1.draw(g);
if(r1.getColor()==Color.cyan) {
r1.draw(g);
}
}
public static void main(String[] args) {
TestGraphic aap=new TestGraphic();
}
}
的方法
mouseclicked()
當我點擊矩形應該改變我用r1.draw(g)
的顏色,但它不工作:■
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
我試圖repaint()
,但它不工作 任何幫助? :d
格式代碼正確。然後發佈完整的異常堆棧跟蹤,並告訴我們它要返回哪條線。 –
發表一個http://sscce.org/ –
我們需要一個堆棧跟蹤。 –