Ich bin ne ziemliche java Niete, aber gezwungenermassen bin ich daran ein towerdefense Game zu programmieren...
Nur komm ich mit der Thread Implementierung net klar, irgendwie schnall ich auch nach dem ich in 2 Büchern die Beispiele durchgearbeitet habe nicht ganz wie ich das Zeugs implementieren soll.
Kann sich jemand der sich auskennt mal kurz mein chaos anschauen?
ja ich weiss... programmier
Aufgebaut ist es in eclipse, wenn da gescheite Vorschläge sind wie ich das ganze viel einfacher aufbauen könnte nur zu.
Java
package gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyGui extends JFrame implements ActionListener,MouseListener,Runnable{
public static int tester=0;
public static int showpath=0;
public static int wayPointCount;
public static int mobCount;
public static int distance=0;
public static int[][] waypoints = new int[11][2];
public static int[][] checkPoints;
private PathPlot pathPlot;
private JButton btnOk = null;
private JButton btnOk2 = null;
private JButton btnCancel = null;
private static int zweiklick=0;
// MyGui th = new MyGui();
Thread t = new Thread();
public MyGui(){
super("TowerDefense");
this.setBounds(100, 100, 400, 400);
pathPlot = new PathPlot();
this.setLayout(new BorderLayout());;
this.add(pathPlot, BorderLayout.CENTER);
this.add(this.getBtnOk(), BorderLayout.NORTH);
this.add(this.getBtnOk2(), BorderLayout.SOUTH);
this.setVisible(true);
}
public static int[][] getPosition(int distance){
int x = distance;
int lastCheckPoint=0;
int[][] position = new int[1][2];
for(int i=0;i<wayPointCount;i++){
if(x>=0){
x=x-Math.abs(checkPoints[i][0])-Math.abs(checkPoints[i][1]);
}
else{
lastCheckPoint=i-1;
x=x+Math.abs(checkPoints[i-1][0])+Math.abs(checkPoints[i-1][1]);
break;
}
}
if(checkPoints[lastCheckPoint][0]==0){
if(checkPoints[lastCheckPoint][1]>=0){
position[0][0]=waypoints[lastCheckPoint][0];
position[0][1]=waypoints[lastCheckPoint][1]-x;
return position;
}
else{
position[0][0]=waypoints[lastCheckPoint][0];
position[0][1]=waypoints[lastCheckPoint][1]+x;
return position;
}
}
else{
if(checkPoints[lastCheckPoint][0]>=0){
position[0][0]=waypoints[lastCheckPoint][0]-x;
position[0][1]=waypoints[lastCheckPoint][1];
return position;
}
else{
position[0][0]=waypoints[lastCheckPoint][0]+x;
position[0][1]=waypoints[lastCheckPoint][1];
return position;
}
}
}
public void run()
{
while (true) {
pathPlot.paintImmediately(0, 0, this.getWidth(), this.getHeight());
t.interrupt();
if (t.isInterrupted()) {
break;
}
}
}
public JButton getBtnOk() {
if (btnOk == null) {
btnOk = new JButton("Start Path");
btnOk.addActionListener(this);
}
return btnOk;
}
public JButton getBtnOk2() {
if (btnOk2 == null) {
btnOk2 = new JButton("Start Path 2");
btnOk2.addActionListener(this);
}
return btnOk2;
}
public JButton getBtnCancel() {
if (btnCancel == null) {
btnCancel = new JButton("Plot Path");
btnCancel.addActionListener(this);
}
return btnCancel;
}
public void neumalen(){
pathPlot.repaint();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(getBtnOk())) {
wayPointCount = 11;
waypoints[0][0] = 900; waypoints[0][1] = 150;
waypoints[1][0] = 750; waypoints[1][1] = 150;
waypoints[2][0] = 750; waypoints[2][1] = 750;
waypoints[3][0] = 550; waypoints[3][1] = 750;
waypoints[4][0] = 550; waypoints[4][1] = 150;
waypoints[5][0] = 150; waypoints[5][1] = 150;
waypoints[6][0] = 150; waypoints[6][1] = 350;
waypoints[7][0] = 350; waypoints[7][1] = 350;
waypoints[8][0] = 350; waypoints[8][1] = 550;
waypoints[9][0] = 150; waypoints[9][1] = 550;
waypoints[10][0] = 150; waypoints[10][1] = 900;
showpath=1;
pathPlot.repaint();
if(zweiklick<=0)zweiklick=0;
zweiklick = zweiklick +1;
if (zweiklick ==2){
btnOk.setVisible(false);
btnOk2.setVisible(false);
this.remove(this.getBtnOk());
this.remove(this.getBtnOk2());
this.add(this.getBtnCancel(), BorderLayout.SOUTH);
btnCancel.setVisible(true);
}
}
if (e.getSource().equals(getBtnOk2())) {
wayPointCount = 10;
waypoints[0][0] = 900; waypoints[0][1] = 150;
waypoints[1][0] = 150; waypoints[1][1] = 150;
waypoints[2][0] = 150; waypoints[2][1] = 350;
waypoints[3][0] = 850; waypoints[3][1] = 350;
waypoints[4][0] = 850; waypoints[4][1] = 550;
waypoints[5][0] = 150; waypoints[5][1] = 550;
waypoints[6][0] = 150; waypoints[6][1] = 750;
waypoints[7][0] = 850; waypoints[7][1] = 750;
waypoints[8][0] = 850; waypoints[8][1] = 950;
waypoints[9][0] = 200; waypoints[9][1] = 950;
// waypoints[10][0] = 150; waypoints[10][1] = 900;
showpath=1;
pathPlot.repaint();
if(zweiklick>=0)zweiklick=0;
zweiklick=zweiklick -1;
if(zweiklick==-2){
btnOk.setVisible(false);
btnOk2.setVisible(false);
this.remove(this.getBtnOk());
this.remove(this.getBtnOk2());
this.add(this.getBtnCancel(), BorderLayout.SOUTH);
btnCancel.setVisible(true);
}
}
if (e.getSource().equals(getBtnCancel())){
System.out.println("PathPlot");
distance=0;
for(int i=0;i<MyGui.wayPointCount-1;i++){
distance=distance+ Math.abs(MyGui.waypoints[i+1][1]-MyGui.waypoints[i][1]);
distance=distance+ Math.abs(MyGui.waypoints[i+1][0]-MyGui.waypoints[i][0]);
}
System.out.println("Phase2");
checkPoints = new int[wayPointCount][2];
for(int k=0;k<2;k++){
for(int i=0;i<(wayPointCount-1);i++){
checkPoints[i][k]=waypoints[i][k]-waypoints[i+1][k];
}
}
tester=0;
System.out.println("Phase3");
mobCount=4;
for(int i=0;i<distance+(mobCount*200);i++){
//pathPlot.paintImmediately(0, 0, this.getWidth(), this.getHeight());
t.start();
tester=tester+1;
try{
Thread.currentThread().sleep(100);
}
catch(InterruptedException ie){
}
}
}
}
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
Alles anzeigen
Java
package gui;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JComponent;
public class PathPlot extends JComponent{
// private int id; //identification
// private Thread th=new Thread(this);
// private Thread time=new Thread(this);
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
// Define Math System
int w= this.getWidth();
int h= this.getHeight();
double a = 1000;
double b = 1000;
double xScale = w/a;
double yScale = h/b;
if(MyGui.showpath==1){
for(int i=0;i<MyGui.wayPointCount-1;i++){
g2.drawLine((int)(xScale*MyGui.waypoints[i][0]),(int)(yScale*MyGui.waypoints[i][1]),(int)(xScale*MyGui.waypoints[i+1][0]),(int)(yScale*MyGui.waypoints[i+1][1]));
}
MyGui.showpath=0;
}
g2.drawRect((int)(xScale*205), (int)(yScale*205),(int)(xScale*90),(int)(yScale*90));
//Rectangle2D turm = new Rectangle2D()
Ellipse2D.Double ellipse = new Ellipse2D.Double();
Ellipse2D.Double ellipse2 = new Ellipse2D.Double();
Ellipse2D.Double ellipse3 = new Ellipse2D.Double();
Ellipse2D.Double ellipse4 = new Ellipse2D.Double();
Ellipse2D.Double ellipse5 = new Ellipse2D.Double();
//draw Path
int vonHinten=0;
if (MyGui.tester>10){
vonHinten=MyGui.tester-2;
}
else {
vonHinten=0;
}
for(int i=vonHinten;i<MyGui.tester-1;i++){
//g2.drawLine((int)(xScale*MyGui.getPosition(i)[0][0]),(int)(yScale*MyGui.getPosition(i)[0][1]),(int)(xScale*MyGui.getPosition(i+1)[0][0]),(int)(yScale*MyGui.getPosition(i+1)[0][1]));
ellipse = new Ellipse2D.Double((int)(xScale*MyGui.getPosition(i)[0][0])-10,(int)(yScale*MyGui.getPosition(i)[0][1])-10,20,20);
if(MyGui.tester>200){
ellipse2 = new Ellipse2D.Double((int)(xScale*MyGui.getPosition(i-195)[0][0]-10),(int)(yScale*MyGui.getPosition(i-195)[0][1]-10),20,20);
}
if(MyGui.tester>400){
ellipse3 = new Ellipse2D.Double((int)(xScale*MyGui.getPosition(i-395)[0][0]-10),(int)(yScale*MyGui.getPosition(i-395)[0][1]-10),20,20);
}
if(MyGui.tester>600){
ellipse4 = new Ellipse2D.Double((int)(xScale*MyGui.getPosition(i-595)[0][0]-10),(int)(yScale*MyGui.getPosition(i-595)[0][1]-10),20,20);
}
if(MyGui.tester>800){
ellipse5 = new Ellipse2D.Double((int)(xScale*MyGui.getPosition(i-795)[0][0]-10),(int)(yScale*MyGui.getPosition(i-795)[0][1]-10),20,20);
}
g2.setColor(Color.RED);
if(MyGui.tester<=MyGui.distance){
g2.draw(ellipse);}
if(MyGui.tester<=MyGui.distance+199){
g2.draw(ellipse2);}
if(MyGui.tester<=MyGui.distance+399){
g2.draw(ellipse3);}
if(MyGui.tester<=MyGui.distance+599){
g2.draw(ellipse4);}
if(MyGui.tester<=MyGui.distance+799){
g2.draw(ellipse5);}
}
//draw Playfield
for(int i=0;i<10;i++){
g2.drawLine(0, i*h/10, w, i*h/10);
g2.drawLine(i*w/10, 0, i*w/10, h);
g2.setColor(Color.BLUE);
}
}
}
Alles anzeigen