Tommorow I've asked for help with collision.I've got good advice,collision detection works,but after what object collide he literally fly to nowhere-off the screen.I thought It was because I didn't call repaint()
method in ActionPerformed in animation,but Rectangle I've draw for collision split up with gif as Jlabel I setted as Icon.T
My question about collision
Pac-Man before collision after collision,Pac-Man just fly away
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class Hra extends JFrame implements ActionListener {
static ArrayList<Body> bodiky = new ArrayList<Body>(50);
Rectangle platforma;
Rectangle packRect;
Rectangle ghostRect;
Timer timer;
private JPanel contentPane;
static Body body ;
static JLabel gifLabel = new JLabel(new ImageIcon("C:\\Users\\petrb\\Downloads\\packmanGifRes.gif"));
JLabel lblNewLabel;
private Color bodyCol = Color.white;
private Color packCol = Color.YELLOW;
static PackMan packman;
private int xRychlost =1;
private int yRychlost = 1;
static int count = 0;
static Duch duch;
/**
* Launch the application.
*/
public static void main(String[] args) {
Hra frame = new Hra();
frame.setVisible(true);
frame.getContentPane().add(gifLabel);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
}
/**
* Create the frame.
*/
public Hra() {
packman = new PackMan(0, 900, 800, packCol);
packRect = new Rectangle(packman.getSouradniceX(), packman.getSouradniceY(),50, 70);
ghostRect = new Rectangle(800,800,50,70);
platforma = new Rectangle(0, 120, 50, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
getContentPane().setBackground(Color.gray);
timer = new Timer(0,this);
timer.start();
lblNewLabel = new JLabel("Score:");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(154)
.addComponent(gifLabel, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(227, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
.addComponent(gifLabel)
.addGap(73))
);
contentPane.setLayout(gl_contentPane);
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
switch(e.getKeyCode()) {
case 37: //doleva
// packman.setSouradniceX(packman.getSouradniceX()-20);
// gifLabel.setLocation(gifLabel.getX()-20, gifLabel.getY());
repaint();
count = 1;
break;
case 38: //nahorů
// packman.setSouradniceY(packman.getSouradniceY()-20);
// gifLabel.setLocation(gifLabel.getX(), gifLabel.getY()-20);
repaint();
count = 2;
// zkontrolujKolizi();
break;
case 39://doprava
// packman.setSouradniceX(packman.getSouradniceX()+20);
// gifLabel.setLocation(gifLabel.getX()+20, gifLabel.getY());
repaint();
count = 3;
// zkontrolujKolizi();
break;
case 40://dolů
// packman.setSouradniceY(packman.getSouradniceY()+20);
// gifLabel.setLocation(gifLabel.getX(), gifLabel.getY()+20);
count =4;
// zkontrolujKolizi();
repaint();
break;
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("hodnota:"+ e.getKeyCode());
System.out.println("Znak:" + e.getKeyChar());
}
});
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawRect(packRect.x, packRect.y, packRect.height, packRect.width);
g.setColor(bodyCol);
g.drawRect(ghostRect.x,ghostRect.y,ghostRect.width,ghostRect.height);
g.setColor(packCol);
g.fillRect(0, 120, 50, 800);
g.fillRect(40, 858, 200 ,62);
g.drawRect(platforma.x, platforma.y, platforma.width, platforma.height);
}
public boolean zkontrolujKolizi() {
return packRect.intersects(ghostRect) || packRect.intersects(platforma) ;
}
@Override
public void actionPerformed(ActionEvent e) {
int rollzpetX = packRect.x;
int rollzpetY = packRect.y;
switch (count) {
case 1:
packman.setSouradniceX(packman.getSouradniceX() - xRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.x = packRect.x - xRychlost;
// repaint();
break;
case 2:
packman.setSouradniceY(packman.getSouradniceY() - yRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.y = packRect.y - yRychlost;
break;
case 3:
packman.setSouradniceX(packman.getSouradniceX() + xRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.x = packRect.x + xRychlost;
break;
case 4:
packman.setSouradniceY(packman.getSouradniceY() + yRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.y = packRect.y+yRychlost;
break;
}
if (zkontrolujKolizi()) {
packRect.x = rollzpetX;
packRect.y = rollzpetY;
}else {
repaint();
}
}
}
//Pacman class
import java.awt.Color;
import java.util.ArrayList;
public class PackMan {
private double skore;
private int souradniceX;
private int souradniceY;
private Color color;
public double getSkore() {
return skore;
}
public void setSkore(double skore) {
this.skore = skore;
}
public int getSouradniceX() {
return souradniceX;
}
public void setSouradniceX(int souradniceX) {
this.souradniceX = souradniceX;
}
public int getSouradniceY() {
return souradniceY;
}
public void setSouradniceY(int souradniceY) {
this.souradniceY = souradniceY;
}
public PackMan(int skore, int souradniceX, int souradniceY,Color color) {
super();
this.skore = skore;
this.souradniceX = souradniceX;
this.souradniceY = souradniceY;
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
//Pacman class
import java.awt.Color;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Body {
private int bodyX;
private int bodyY;
private double hodnotaBodu;
private Color color;
public int getBodyX() {
return bodyX;
}
public Body(int bodyX, int bodyY, double hodnotaBodu,Color color) {
super();
this.bodyX = bodyX;
this.bodyY = bodyY;
this.hodnotaBodu = hodnotaBodu;
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void setBodyX(int bodyX) {
this.bodyX = bodyX;
}
public int getBodyY() {
return bodyY;
}
public void setBodyY(int bodyY) {
this.bodyY = bodyY;
}
public double getHodnotaBodu() {
return hodnotaBodu;
}
public void setHodnotaBodu(double hodnotaBodu) {
this.hodnotaBodu = hodnotaBodu;
}
}