Srry, ze tady takhle furt se ptam, ale dochazi mi napady a fantazie :) Uz umim vkladat lode do hraciho pole a umim tuto pozici ulozit do nejakyho pole. Ale poradte mi nekdo jak zaridit, abych do mrizky skladal ruzne typy lodi? Zatim to vypada asi tak:
public class Test{
byte[][] ships = new byte[12][12];
public Test(){
byte a = 2;
ship = new Submarine(600, 100, a, ships);
JButton ok = new JButton("Ok");
ok.setBounds(700, 400, 50, 50);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ships = ship.Coords();
remove(ship);
}
});
add(ship);
setTitle("Vlozeni lodi");
setSize(800, 500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}
public class Submarine extends Ships {
int x;
int y;
byte[][] p = null;
int drags = 0;
byte number;
public Submarine(int x, int y, byte num, byte[][] pole) {
this.x = x;
this.y = y;
this.number = num;
this.p = pole;
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int mx = ((e.getX() - 40) / 30) * 30 + 40;
int my = ((e.getY() - 40) / 30) * 30 + 40;
try {
p[((getShipY() - 40) / 30) + 1][((getShipX() - 40) / 30) + 1] = 0;
} catch (Exception ex) {
}
moveShip(mx, my);
System.out.println((((getShipY() - 40) / 30) + 1) + " " + (((getShipX() - 40) / 30) + 1));
}
@Override
public void mouseReleased(MouseEvent e) {
try {
p[(getShipY() - 40) / 30 + 1][(getShipX() - 40) / 30 + 1] = number;
} catch (Exception ex) {
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
System.out.print(p[i][j] + " ");
}
System.out.println();
}
}
});
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int mx = ((e.getX() - 40) / 30) * 30 + 40;
int my = ((e.getY() - 40) / 30) * 30 + 40;
moveShip(mx, my);
}
});
}
void moveShip(int x, int y) {
int OFFSET = 1;
repaint(this.x, this.y, 30 + OFFSET, 30 + OFFSET);
this.x = x;
this.y = y;
repaint(this.x, this.y, 30 + OFFSET, 30 + OFFSET);
}
@Override
public void shipShape(Graphics g) {
g.setColor(Color.red);
g.fillRect(this.x, this.y, 30, 30);
g.setColor(Color.BLACK);
g.drawRect(this.x, this.y, 30, 30);
}
public void shipShape(Graphics g, int x, int y) {
g.setColor(Color.red);
g.fillRect(x, y, 30, 30);
g.setColor(Color.BLACK);
g.drawRect(x, y, 30, 30);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
g.drawRect(30 * j + 40, 30 * i + 40, 30, 30);
}
}
shipShape(g);
shipShape(g, 650, 150);
}
@Override
public void rotateShip() {
}
@Override
public int getShipX() {
return this.x;
}
@Override
public int getShipY() {
return this.y;
}
@Override
public byte[][] Coords() {
return p;
}
}
a me jde o to, ze kdyz budu mit vice lodi nez jednu tak nvm jak zaridit abych tam skladal ty jednotlive lode :/ umim jen pridat jednu lod a to jeste jednoho typu
Fórum › Java
Lode - vkladani lodi do hraciho pole


Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 31 hostů
Podobná vlákna
Lode - kontrola vsech lodi — založil Vavat9
Vytvoreni hraciho pole pro Piskvorky — založil Comedian
Vkládání do pole, problém s ukazateli — založil Jackob
Naprogramování lodí v Pythonu — založil programovací nešika
Scématický obrázek kosmické lodi — založil Lolo24
Moderátoři diskuze