RauchenderDachs
Cadet 4th Year
- Registriert
- Dez. 2012
- Beiträge
- 70
Hi Leute,
ich bins schon wieder
Mein Problem dieses mal ist das ich ein Objekt( Kreis) auf ein anderes Objekt zu bewegen lassen möchte. Hab schon gegoogelt allerdings konnt ich keinen Lösungsanatz umsetzen. Ein weiteres Problem wird denk ich sein das sich Objekt zwei bewegt.
Hier mal mein Quellcode:
In der letzen Klasse (Bullet) hab ich schon mal eine Lösung ausprobiert hat allerdings nicht funktioniert.
MFG RauchenderDachs
ich bins schon wieder
Mein Problem dieses mal ist das ich ein Objekt( Kreis) auf ein anderes Objekt zu bewegen lassen möchte. Hab schon gegoogelt allerdings konnt ich keinen Lösungsanatz umsetzen. Ein weiteres Problem wird denk ich sein das sich Objekt zwei bewegt.
Hier mal mein Quellcode:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package towerdefencev4;
import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*/
public class GUI extends JFrame implements Runnable{
/**
* @param args the command line arguments
*/
private Image dbImage;
private Graphics dbGraphics;
static Creep dasCreep[] = new Creep[5];
int verschiebung=25;//abstand zwischen den einzelnen Creeps
static Tower turm ;
static Bullet kugel ;
JButton go = new JButton("go");
public GUI(String title){
super(title);
for(int n=0 ;n<dasCreep.length;n++){
dasCreep[n]= new Creep(40,verschiebung,this);
verschiebung-=60;
}
turm=new Tower(150,150,this);
}
public static void main(final String[] args) {
final GUI dieOberfläche = new GUI("TowerDefencev4");;
dieOberfläche.setSize(400, 600);
dieOberfläche.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(dieOberfläche).start();
dieOberfläche.setVisible(true);
dieOberfläche.add(new JPanel() {
@Override
public void paint(final Graphics g) {
super.paint(g);
//Hintergrund
g.drawImage(Toolkit.getDefaultToolkit().getImage("../img/ground.gif"), 0, 0, this);
//Creep
for(int n=0 ;n<dasCreep.length;n++){
dasCreep[n].paintcreep(g);
}
turm.paintTower(g);
kugel.paintB(g);
}
});
}
@Override
public void run() {
while(true){
for(int n=0;n<dasCreep.length;n++){
dasCreep[n].move();
}
//Bullet abfeuern
kugel= new Bullet(Color.red,0);
kugel.moveBullet();
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void update(Graphics g)
{
//Double-Buffer initialisieren
if (dbImage == null) {
dbImage = createImage(
this.getSize().width,
this.getSize().height
);
dbGraphics = dbImage.getGraphics();
}
//Hintergrund löschen
dbGraphics.setColor(getBackground());
dbGraphics.fillRect(
0,
0,
this.getSize().width,
this.getSize().height
);
//Vordergrund zeichnen
dbGraphics.setColor(getForeground());
paint(dbGraphics);
//Offscreen anzeigen
g.drawImage(dbImage,0,0,this);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package towerdefencev4;
import java.awt.*;
import javax.swing.*;
/**
*
*/
public class Creep {
int x, y;
Component comp;
boolean perreicht [] = new boolean [5];
public Creep(int x, int y, Component comp){
this.x=x;
this.y=y;
this.comp=comp;
}
public void paintcreep(Graphics gr){
gr.drawImage(Toolkit.getDefaultToolkit().getImage("../img/creep.gif"), x, y, comp);
}
public void move(){
boolean lr=false;
boolean hr=false;
if(x!=40 && perreicht[0]==false || y!=83 && perreicht[0]==false){
movev(hr);
}
if(x==40 && y==83){
perreicht[0]=true;
}
if(perreicht[0]==true && y==83){
if(x!=237){
moveh(lr);
}
}
if(x==237 && y==83){
perreicht[1]=true;
}
if(perreicht[1]==true && x==237){
if(y!=206){
movev(hr);
}
}
if(x==237 && y==206){
perreicht[2]=true;
}
if(perreicht[2]==true && y==206){
if(x!=79){
lr=true;
moveh(lr);
}
}
if(x==79 && y==206){
perreicht[3]=true;
}
if(perreicht[3]==true){
if(y!=351){
movev(hr);
}
}
if(x==79 && y==350){
perreicht[4]=true;
}
if(perreicht[4]==true){
}
}
public void moveh(boolean lr){//lr=links oder rechts
if(lr==false)
x++;
if(lr==true)
x--;
}
public void movev(boolean hr){//hr = hoch oder runter
if(hr==false)
y++;
if(hr==true)
y--;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package towerdefencev4;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Toolkit;
/**
*
* @author Holger
*/
public class Tower {
int x,y;
Component comp;
public Tower(int x ,int y, Component comp){
this.x=x;
this.y=y;
this.comp=comp;
}
public void paintTower(Graphics gr){
gr.drawImage(Toolkit.getDefaultToolkit().getImage("../img/tower.gif"), x, y, comp);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package towerdefencev4;
import java.awt.*;
/**
*
* @author Holger
*/
public class Bullet {
int x,y;
int speedx,speedy,n;
Color color;
public Bullet(Color color,int n){
x=GUI.turm.x;
y=GUI.turm.y;
this.color=color;
this.n=n;
speedx=5;
speedy=5;
}
public void paintB(Graphics gr){
gr.setColor(color);
gr.fillOval(x,y, 10, 10);
}
public void moveBullet(){
x+=speedx;
y+=speedy;
/*y=(GUI.dasCreep[n].y-GUI.turm.y)/(GUI.dasCreep[n].x-GUI.turm.x)*(x*GUI.turm.x)+GUI.turm.y;
if(GUI.dasCreep[n].x<x)
x--;
if(GUI.dasCreep[n].x>x)
x++;
if(GUI.dasCreep[n].x==x)
x=x;*/
}
}
In der letzen Klasse (Bullet) hab ich schon mal eine Lösung ausprobiert hat allerdings nicht funktioniert.
MFG RauchenderDachs