MouseClickEvent

Panda99

Mitglied
Hallo, ich möchte mit Swing eine Landschaft erzeugen. Der Mond soll anklickbar sein, aber momentan bekomme ich wenn er angeklickt wird nur eine Fehlermeldung, Heist er erkennt das er angeklickt wird, aber gibt mir diese Fehlermedlung : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Ich weis nicht woran das liegen könnte, schon mal vielen dank im voraus,

Panda

Java:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Mond {
 
    int x;
    int y;
    boolean sichtbar = true;
    int radius;
    Ellipse2D moon;
    public int getX() {
        return x;
    }
//    public Ellipse2D berechne () {
//       
//       
//       
//        Ellipse2D e;
//       
//        return (new Ellipse2D);
//       
//    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public boolean isSichtbar() {
        return sichtbar;
    }
    public void setSichtbar(boolean sichtbar) {
        this.sichtbar = sichtbar;
    }
    public int getRadius() {
        return radius;
    }
    public void setRadius(int radius) {
        this.radius = radius;
    }
    
    public void draw (Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        
        if (sichtbar==true) {
     //   g.setColor(Color.gray);
        g2.setColor(Color.gray);
      //  g.fillOval(50, 50, 50, 50);
        g2.fillOval(this.x, this.y, this.radius, this.radius);
        
      //  g.drawOval(50, 50, 50, 50);
        }else {
            g.setColor(Color.yellow);
            g2.setColor(Color.yellow);
        //    g.fillOval(this.x,this.y,this.radius,this.radius);
              g2.fillOval(this.x, this.y, this.radius, this.radius);
//            g.drawOval(50, 50, 50, 50);
        }
        
    }
    public Mond(int x, int y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.sichtbar=false;
        //this.moon =
        
    }
    
    public void klick(int x,int y) {
        boolean klickImMond = moon.contains(x, y);
        if (klickImMond==true) {
            System.out.println("Klick im Baum");
            sichtbar=true;
            
        }else {
            sichtbar=false;
        }
        
    }
//    public boolean switchTime (int x, int y) {
//       
//        sichtbar =
//        return sichtbar;
//    }
    
    

    }
 

Panda99

Mitglied
Java:
public void klick(int x,int y) {
        boolean klickImMond = moon.contains(x, y);
        if (klickImMond==true) {
            System.out.println("Klick im Baum");
            sichtbar=true;
            
        }else {
            sichtbar=false;
        }
das ist die Methode um die es geht
 

LimDul

Top Contributor
Nö, du hast sie nur deklariert. Du musst ja moon auch einen Wert zu weisen. Deine Zeile ist äquivalent zu
Java:
Ellipse2D moon = null;
 

Neue Themen


Oben