Hallo Freunde der Java-Programmierung,
Ich habe mal wieder ein Problem, es geht dabei um ein Verhalten von einem GeneralPath-Objekt.
Es soll eigentlich ein Kreis werden, jedoch wird das Objekt eher in Augenform dargestellt und hat vor allem Ecken und erreicht die Grenze des JComponents, auf dem es gemalt wird, nicht.
Hier jetzt aber erstmal der Code:
Genauer geht es um diese Funktion:
Der untere Teil der Funktion macht irgendwie nicht das, was er soll...
Ich hoffe ihr könnt mir helfen und bedanke mich an dieser Stelle schonmal!
MfG Stefan
Ich habe mal wieder ein Problem, es geht dabei um ein Verhalten von einem GeneralPath-Objekt.
Es soll eigentlich ein Kreis werden, jedoch wird das Objekt eher in Augenform dargestellt und hat vor allem Ecken und erreicht die Grenze des JComponents, auf dem es gemalt wird, nicht.
Hier jetzt aber erstmal der Code:
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
public class Field extends JComponent implements MouseListener, MouseMotionListener{
private static final long serialVersionUID = 1L;
private int xPos;
private int yPos;
private boolean selected = false;
private boolean cleared = false;
private int lang;
private Color origColor;
private Color color;
private GeneralPath p;
private Field north;
private Field left;
private Field right;
private Field south;
private GameField gf;
public Field(int x, int y, int color, GameField gf){
this.gf = gf;
this.xPos=x;
this.yPos=y;
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setVisible(true);
switch(color){
case 0: {
this.color = new Color(255, 0, 255);
this.origColor = new Color(255, 0, 255);
break;
}
case 1: {
this.color = Color.YELLOW;
this.origColor = Color.YELLOW;
break;
}
case 2: {
this.color = Color.RED;
this.origColor = Color.RED;
break;
}
case 3: {
this.color = new Color(0, 0, 200);
this.origColor = new Color(0, 0, 200);
break;
}
case 4: {
this.color = Color.GREEN;
this.origColor = Color.GREEN;
break;
}
}
}
public void setNorth(Field f){
this.north = f;
}
public void setSouth(Field f){
this.south = f;
}
public void setLeft(Field f){
this.left = f;
}
public void setRight(Field f){
this.right = f;
}
public boolean isSelected(){
return this.selected;
}
public boolean isCleared(){
return this.cleared;
}
public int min(int i, int j){
if(i < j){
return i;
}
else{
return j;
}
}
public void reset(){
this.selected = false;
this.setColor(origColor);
repaint();
}
public void setColor(Color col){
this.color = col;
}
public Color getOrigColor(){
return this.origColor;
}
public void setOrigColor(Color c){
this.origColor = c;
}
public void setCleared(boolean cl){
this.cleared = cl;
}
public void draw(Graphics g){
if(!this.cleared){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(this.color);
p = new GeneralPath();
/* Funktion zum Zeichnen eines Karos.
p.moveTo(0, this.getHeight()/2);
p.lineTo(this.getWidth()/2, 0); p.lineTo(this.getWidth(), this.getHeight()/2);
p.lineTo(this.getWidth()/2, this.getHeight()); p.lineTo(0, this.getHeight()/2);*/
//Funktion zum Zeichnen eines Kreises, die nicht funktioniert.
p.moveTo(0, this.getHeight()/2);
p.curveTo(0, this.getHeight()/2, this.getWidth()/2, 0, this.getWidth(), this.getHeight()/2);
p.curveTo(this.getWidth(), this.getHeight()/2, this.getWidth()/2, this.getHeight(), 0, this.getHeight()/2);
g2.fill(p);
}
}
public int maxOf(int i, int j){
if(i > j){
return i;
}
else{
return j;
}
}
public void markValidNeighb(String s){
boolean more = false;
mark();
if(!s.equalsIgnoreCase("so")&&this.north!=null&&!this.north.isSelected()&&this.north.getOrigColor().equals(this.origColor)){
more = true;
north.markValidNeighb("no");
}
if(!s.equalsIgnoreCase("ri")&&this.left!=null&&!this.left.isSelected()&&this.left.getOrigColor().equals(this.origColor)){
more = true;
left.markValidNeighb("le");
}
if(!s.equalsIgnoreCase("le")&&this.right!=null&&!this.right.isSelected()&&this.right.getOrigColor().equals(this.origColor)){
more = true;
right.markValidNeighb("ri");
}
if(!s.equalsIgnoreCase("no")&&this.south!=null&&!this.south.isSelected()&&this.south.getOrigColor().equals(this.origColor)){
more = true;
south.markValidNeighb("so");
}
if(!more&&s.equalsIgnoreCase("")){
this.reset();
}
}
public void mark(){
this.selected = true;
int red = maxOf(this.origColor.getRed()-50, 0);
int green = maxOf(this.origColor.getGreen()-50, 0);
int blue = maxOf(this.origColor.getBlue()-50, 0);
this.setColor(new Color(red, green, blue));
repaint();
}
public void resetValidNeighb(){
this.reset();
if(this.north!=null&&this.north.isSelected()&&this.north.getOrigColor().equals(this.origColor)){
north.resetValidNeighb();
}
if(this.left!=null&&this.left.isSelected()&&this.left.getOrigColor().equals(this.origColor)){
left.resetValidNeighb();
}
if(this.right!=null&&this.right.isSelected()&&this.right.getOrigColor().equals(this.origColor)){
right.resetValidNeighb();
}
if(this.south!=null&&this.south.isSelected()&&this.south.getOrigColor().equals(this.origColor)){
south.resetValidNeighb();
}
}
@Override
public void mouseClicked(MouseEvent e){
if(!this.cleared){
if(p.contains(e.getPoint())&&!this.selected){
gf.resetAllBefore();
this.markValidNeighb("");
}
else if(p.contains(e.getPoint())&&this.selected){
gf.clearSelected();
}
}
repaint();
}
public void clear(){
this.selected = false;
this.cleared = true;
repaint();
}
@Override
public void paint(Graphics g){
draw(g);
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e){
}
@Override
public void mousePressed(MouseEvent e){
}
@Override
public void mouseReleased(MouseEvent e){
}
public boolean isIn(Point p){
if(p.getX()<this.xPos+lang&&p.getX()>this.xPos){
if(p.getY()<this.yPos+lang&&p.getY()>this.yPos){
return true;
}
}
return false;
}
public void setXPos(int xPos) {
this.xPos = xPos;
}
public int getXPos() {
return xPos;
}
public void setYPos(int yPos) {
this.yPos = yPos;
}
public int getYPos() {
return yPos;
}
public void setLang(int lang) {
this.lang = lang;
}
public int getLang() {
return lang;
}
public void setNeighbours(Field north, Field left, Field right, Field south){
this.north = north;
this.left = left;
this.right = right;
this.south = south;
}
@Override
public void mouseDragged(MouseEvent e){
}
@Override
public void mouseMoved(MouseEvent e){
}
}
Genauer geht es um diese Funktion:
Java:
public void draw(Graphics g){
if(!this.cleared){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(this.color);
p = new GeneralPath();
/* Funktion zum Zeichnen eines Karos.
p.moveTo(0, this.getHeight()/2);
p.lineTo(this.getWidth()/2, 0); p.lineTo(this.getWidth(), this.getHeight()/2);
p.lineTo(this.getWidth()/2, this.getHeight()); p.lineTo(0, this.getHeight()/2);*/
//Funktion zum Zeichnen eines Kreises, die nicht funktioniert.
p.moveTo(0, this.getHeight()/2);
p.curveTo(0, this.getHeight()/2, this.getWidth()/2, 0, this.getWidth(), this.getHeight()/2);
p.curveTo(this.getWidth(), this.getHeight()/2, this.getWidth()/2, this.getHeight(), 0, this.getHeight()/2);
g2.fill(p);
}
}
Der untere Teil der Funktion macht irgendwie nicht das, was er soll...
Ich hoffe ihr könnt mir helfen und bedanke mich an dieser Stelle schonmal!
MfG Stefan