Hallo Community,
versuche mich zZ an einem kleinen "Spiel" im Steckkastenprinzip.
per zufall erezugte Shapes müssem dem jeweils deckgleichen Shape zugeordnet werden.
Nun bin ich soweit, dass 4 statische Shapes erzeugt werden (kreis, rechtecht, vieleck und dreieck)
und ein Shape, das man in das passende der 4 o.g. Shapes ziehen soll.
Soweit so gut, nun bleibt leider das Problem, dass das Verschieben des ZufallsShapes nicht richtig aussieht.
Wenn ich zB das Dreieck an einer Ecke ziehe, so springt die Maus in die Mitte des Dreiecks, sobald ich es in eine andere Position gebracht habe.
Es fällt mir leider sehr schwer, das zu beschreiben
Wer es versuchen will, wird feststellen, was ich meine.
Zur Klärung:
Damit steuere ich, in welchem 9. der Bildschirmbreite etwas dargestellt wird.
9 Teile: ABSTAND - KREIS - ABSTAND - RECHTECK - ABSTAND - VIELECK - ABSTAND - DREIECK - ABSTAND
TEST.java
RegularPolygon.java (um vielecke zu erzeugen)
Primarfrage: Wie komme ich zu einer Lösung für mein Problem?
Sekundärfrage: Verbesserungsvorschläge willkommen. Wie erwähnt: erster Versuch mit Graphics bzw. Graphics2D
versuche mich zZ an einem kleinen "Spiel" im Steckkastenprinzip.
per zufall erezugte Shapes müssem dem jeweils deckgleichen Shape zugeordnet werden.
Nun bin ich soweit, dass 4 statische Shapes erzeugt werden (kreis, rechtecht, vieleck und dreieck)
und ein Shape, das man in das passende der 4 o.g. Shapes ziehen soll.
Soweit so gut, nun bleibt leider das Problem, dass das Verschieben des ZufallsShapes nicht richtig aussieht.
Wenn ich zB das Dreieck an einer Ecke ziehe, so springt die Maus in die Mitte des Dreiecks, sobald ich es in eine andere Position gebracht habe.
Es fällt mir leider sehr schwer, das zu beschreiben
Wer es versuchen will, wird feststellen, was ich meine.
Zur Klärung:
Java:
width/9*<irgendwas>
9 Teile: ABSTAND - KREIS - ABSTAND - RECHTECK - ABSTAND - VIELECK - ABSTAND - DREIECK - ABSTAND
TEST.java
Java:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class TEST extends JPanel {
Dimension dim;
Ellipse2D.Double circle;
int circleWidth;
int circleHeight;
int circleX;
int circleY;
int currentX;
int currentY;
int height;
int lastX;
int lastY;
int rectWidth;
int rectHeight;
int rectX;
int rectY;
int polyRadius;
int polyX;
int polyY;
int polyvertex;
int[] starX;
int[] starY;
int width;
JFrame mainFrame;
Polygon triangle;
Rectangle2D.Double rectangle;
RegularPolygon polygon;
String shapeType;
Shape test;
public TEST() {
initGUI();
initListeners();
initVariables();
shapeType = createRandomShape();
}
public void updateLocation() {
repaint();
}
public void initGUI() {
this.setBackground(Color.LIGHT_GRAY);
mainFrame = new JFrame();
dim = Toolkit.getDefaultToolkit().getScreenSize();
mainFrame.setTitle("Nele's Spiel");
mainFrame.setDefaultCloseOperation(3);
mainFrame.setUndecorated(true);
mainFrame.setVisible(true);
mainFrame.setSize((int) dim.getWidth(), (int) dim.getHeight());
mainFrame.add(this);
}
public void initVariables() {
height = (int) dim.getHeight();
width = (int) dim.getWidth();
currentX = width / 9 * 4 - width/9/2;
currentY = 200;
lastX = 0;
lastY = 0;
circleWidth = width / 9;
circleHeight = circleWidth;
circleX = width / 9;
circleY = height - circleHeight - 100;
rectWidth = width / 9;
rectHeight = rectWidth;
rectX = width / 9 * 3;
rectY = height - rectHeight - 100;
polyRadius = width / 9 / 2 + 15;
polyX = (int) (width / 9 * 5.5);
polyY = height - polyRadius - 90;
polyvertex = 6;
starX = new int[3];
starY = new int[3];
starX[0] = width / 9 * 7;
starY[0] = height - 100;
starX[1] = (int) (width / 9 * 7.5);
starY[1] = (int) (height - 100 - width/9);
starX[2] = width / 9 * 8;
starY[2] = height - 100;
}
public void initListeners() {
int delay = 2; // milliseconds
this.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
currentX = e.getX()-width/9/2;
currentY = e.getY()-width/9/2;
}
});
this.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
System.out.println("lastX: " + lastX);
System.out.println("lastY: " + lastY);
System.out.println("X: " + currentX);
System.out.println("Y: " + currentY);
if(true) {shapeType = createRandomShape();}
}
public void mousePressed(MouseEvent e) {
}
});
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// refresh
updateLocation();
}
};
new Timer(delay, taskPerformer).start();
}
public String createRandomShape()
{
shapeType = null;
int zahl = (int)(Math.random() * 4 + 1);
switch(zahl) {
case 1:
return "circle";
case 2:
return "rectangle";
case 3:
return "polygon";
case 4:
return "triangle";
default:
return "";
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
circle = new Ellipse2D.Double(circleX, circleY, circleWidth, circleHeight);
rectangle = new Rectangle2D.Double(rectX, rectY, rectWidth, rectHeight);
polygon = new RegularPolygon(polyX, polyY, polyRadius, polyvertex);
triangle = new Polygon();
triangle.addPoint(starX[0], starY[0]);
triangle.addPoint(starX[1], starY[1]);
triangle.addPoint(starX[2], starY[2]);
((Graphics2D) g).setColor(Color.RED);
((Graphics2D) g).fill(circle);
((Graphics2D) g).fill(rectangle);
((Graphics2D) g).fill(polygon);
((Graphics2D) g).fill(triangle);
((Graphics2D) g).setColor(Color.BLACK);
((Graphics2D) g).setStroke(new BasicStroke(3.0f));
((Graphics2D) g).draw(circle);
((Graphics2D) g).draw(rectangle);
((Graphics2D) g).draw(polygon);
((Graphics2D) g).draw(triangle);
if(shapeType.equals("circle")){Ellipse2D.Double circ = new Ellipse2D.Double(currentX, currentY, circleWidth, circleHeight);
((Graphics2D) g).draw(circ);}
if(shapeType.equals("rectangle")){Rectangle2D.Double rect = new Rectangle2D.Double(currentX, currentY, rectWidth, rectHeight);
((Graphics2D) g).draw(rect);}
if(shapeType.equals("polygon")){RegularPolygon poly = new RegularPolygon(currentX+width/9/2, currentY+width/9/2, polyRadius, polyvertex);
((Graphics2D) g).draw(poly);}
if(shapeType.equals("triangle")){Polygon tri = new Polygon(new int[]{currentX,currentX + width/9/2,currentX+width/9}, new int[]{currentY+width/9,currentY,currentY+width/9}, 3);
((Graphics2D) g).draw(tri);}
}
public static void main(String[] args) {
new TEST();
}
}
RegularPolygon.java (um vielecke zu erzeugen)
Java:
import java.awt.*;
public class RegularPolygon extends Polygon {
public RegularPolygon(int x, int y, int r, int vertexCount) {
this(x, y, r, vertexCount, 0);
}
public RegularPolygon(int x, int y, int r, int vertexCount, double startAngle) {
super(getXCoordinates(x, y, r, vertexCount, startAngle)
,getYCoordinates(x, y, r, vertexCount, startAngle)
,vertexCount);
}
protected static int[] getXCoordinates(int x, int y, int r, int vertexCount, double startAngle) {
int res[]=new int[vertexCount];
double addAngle=2*Math.PI/vertexCount;
double angle=startAngle;
for (int i=0; i<vertexCount; i++) {
res[i]=(int)Math.round(r*Math.cos(angle))+x;
angle+=addAngle;
}
return res;
}
protected static int[] getYCoordinates(int x, int y, int r, int vertexCount, double startAngle) {
int res[]=new int[vertexCount];
double addAngle=2*Math.PI/vertexCount;
double angle=startAngle;
for (int i=0; i<vertexCount; i++) {
res[i]=(int)Math.round(r*Math.sin(angle))+y;
angle+=addAngle;
}
return res;
}
}
Primarfrage: Wie komme ich zu einer Lösung für mein Problem?
Sekundärfrage: Verbesserungsvorschläge willkommen. Wie erwähnt: erster Versuch mit Graphics bzw. Graphics2D
Zuletzt bearbeitet: