Swing Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: npoints > xpoints.length || npoints > ypoints.length

Panda99

Mitglied
Java:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

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

public class Sterne {
    
    int z = 3;
    int h = 1;
    Color farbe=Color.green;
    int x;
    int y;
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Sterne (int x, int y) {
    this.x = x;
    this.y = y;   
    }
    public void draw(Graphics g) {
    
        if(h == 1)
        {
        g.setColor(farbe);
        
        int xPoints[]= {x + 10,x + 20, x + 30 };
        int yPoints[]= {y + 10};
        int npoints = (z);
    
        g.fillPolygon(xPoints,yPoints, npoints);
    
        }
    }
    
    
}
 
Ich versuche hier mit Swing Sterne zu zeichnen, die ich in einer anderen Klasse abrufe. Ich bekomme aber die oben angegebene Fehlermeldung und weis nicht warum. Wenn ich die nPoints 0 und 1 setze geht das Programm zwar, aber die Sterne werden nicht ausgegeben.

Vielen Dank schon mal im Voruas,

Panda
 
Doku: https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillPolygon(int[], int[], int)

Dein Problem ist, ein Punkt wird durch X und Y Koordinate bestimmt.

Deine Code
Java:
        int xPoints[]= {x + 10,x + 20, x + 30 };
        int yPoints[]= {y + 10};
        int npoints = (z);
    
        g.fillPolygon(xPoints,yPoints, npoints);
Du sagst, du hast z Punkte (z=3 bei dir)

Punkt 1 hat die Koordinate (x+10, y+10)
Punkt 2 hat die Koordinate (x + 20, ????)
Punkt 3 hat die Koordinate (x + 30, ????)

Sprich für den 2ten und 3ten Punkt fehlt die Y-Koordinate.
 

Zurück
Oben