Dobrý deň,
mám problém zadávam náhodnú pozíciu pre obrázok a aj mi ho umiestni na tú náhodnú pozíciu ale keď dám prienik 2 obrázkov buď mi to dá ten prienik inde ako je obrázok alebo mi hádže problém :/ ak by niekto vedel pomôcť poprosil by som radu (z BlueJ som začal nedávno) 2 kódy v ktorých to nastavujem :
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Hra extends JPanel implements ActionListener {
private Timer mainTimer;
private Hrac hrac;
private Stena stena;
private MalaStena mala;
private Hviezdy hviezda;
private Random generator;
private int hornaHranica = 80;
private int spodnaHranica = 40;
private static int nahodnaX; //= ((int)(Math.random()*(this.hornaHranica - this.spodnaHranica))+this.spodnaHranica);
private static int nahodnaY; //= ((int)(Math.random()*(this.hornaHranica - this.spodnaHranica))+this.spodnaHranica);
public Hra() {
this.generator = new Random();
this.setFocusable(true);
this.hrac = new Hrac(400, 400);
this.stena = new Stena(1, 1);
this.mala = new MalaStena(this.spodnaHranica + this.generator.nextInt(this.hornaHranica - this.spodnaHranica + 1), this.spodnaHranica + this.generator.nextInt(this.hornaHranica - this.spodnaHranica + 1));
this.hviezda = new Hviezdy(this.hrac.getNahodnyX(), this.hrac.getNahodnyY());
this.mainTimer = new Timer(10, this);
this.mainTimer.start();
this.addKeyListener(new Tlacidla(this.getHrac()));
}
public Hrac getHrac() {
return this.hrac;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
this.hrac.nakresli(g2d);
this.stena.nakresli(g2d);
this.mala.nakresli(g2d);
this.hviezda.nakresli(g2d);
}
@Override
public void actionPerformed(ActionEvent arg0) {
this.hrac.update();
this.repaint();
}
public int nahodnaX(){
return nahodnaX = this.spodnaHranica + this.generator.nextInt(this.hornaHranica - this.spodnaHranica + 1);
}
public int nahodnaY(){
return nahodnaY = this.spodnaHranica + this.generator.nextInt(this.hornaHranica - this.spodnaHranica + 1);
//this.spodnaHranica + this.generator.nextInt(this.hornaHranica - this.spodnaHranica + 1);
}
public void vygenerujX(){
int hornaHranica = 80;
int spodnaHranica = 40;
this.nahodnaX = ((int)(Math.random()*(hornaHranica - spodnaHranica))+ spodnaHranica);
}
public static final int nahodnyX(){
return nahodnaX;
}
public void vygenerujY(){
int hornaHranica = 80;
int spodnaHranica = 40;
this.nahodnaY = ((int)(Math.random()*(hornaHranica - spodnaHranica))+ spodnaHranica);
}
public static final int nahodnyY(){
return nahodnaX;
}
// public int nahodnyY(){
// return hrac.getNahodnyY();
// }
}
druhá časť:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
public class Hrac extends Nastavenie {
private int osX = 0;
private int osY = 0;
private Hra stena;
private Hviezdy hviezda;
private int x = this.getX();
private int y = this.getY();
public Hrac(int x, int y) {
super(x, y);
}
public void update() {
this.x += this.osX;
this.y += this.osY;
}
public void nakresli(Graphics2D g2d) {
g2d.drawImage (this.getHracImg(), this.x, this.y, null);
}
public Image getHracImg() {
Image img = Toolkit.getDefaultToolkit().getImage ("C:/Users/ntb/Desktop/Bez názvu.png");
return img;
}
public void keyPressed(KeyEvent e) {
int tlacidlo = e.getKeyCode();
if ((this.x < 40) || (this.y < 40) || (this.x > 726) || (this.y > 724) || (this.x <= this.getNahodnyX() +20) && (this.y <= this.getNahodnyY() +20)) {
this.osY = 0;
this.osX = 0;
// to dáj potom koniec hra
} else {
if ((tlacidlo == KeyEvent.VK_W)) {
this.osY = -2;
} else if (tlacidlo == KeyEvent.VK_S) {
this.osY = 2;
} else if (tlacidlo == KeyEvent.VK_A) {
this.osX = -2;
} else if (tlacidlo == KeyEvent.VK_D) {
this.osX = 2;
}
}
}
public void keyReleased(KeyEvent e) {
int tlacidlo = e.getKeyCode();
if (tlacidlo == KeyEvent.VK_W) {
this.osY = 0;
} else if (tlacidlo == KeyEvent.VK_S) {
this.osY = 0;
} else if (tlacidlo == KeyEvent.VK_A) {
this.osX = 0;
} else if (tlacidlo == KeyEvent.VK_D) {
this.osX = 0;
}
}
public int getNahodnyX(){
return stena.nahodnyX();
}
public int getNahodnyY(){
return stena.nahodnyY();
}
}