Can i add JPanels to a JFrame with Graphics already on it? - java

So I recently started playing around with java graphics and have encountered an issue. I have drawn images onto a JFrame, but did not learn that the images' bounds cannot overlap each other if they're all in one JFrame (setting bounds over where another image is makes the image disappear). I heard something about using multiple JPanels to fix this. I do not know how to do this since I used a JFrame and not a JPanel in my program, so some help would be appreciated on this issue. Thank you, and I apologize if this question is dumb. (My other question that others seem to think was the same as this one was an issue of setting bounds, this question regards bounds overlapping and covering each other)
Here are my classes:
Window Class-
package game.thirdTry;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window extends JFrame {
private static Window instance;
public static Window getInstance() {
if(instance == null) {
instance = new Window("Game");
}
return instance;
}
private Window(String name) {
super(name);
setSize(1200, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
//setUndecorated(true);
addKeyListener(new UserInput());
//this.getContentPane().getSize();
WindowStructure banner = new WindowStructure("Beatles Logo.jpg", 0, 0, getWidth(), 75);
//WindowStructure fball = new WindowStructure("fireball.100x100.png", 100, 100, 100, 100);
WindowStructure fball = WindowStructure.getInstanceF();
System.out.println("Fball.xSize: " + fball.xSize + ", Fball.ySize: " + fball.ySize);
System.out.println("Fball.xLoc: " + fball.xLoc + ", Fball.yLoc: " + fball.yLoc);
//banner.setBounds(banner.xLoc, banner.yLoc, banner.xSize, banner.ySize);
//fball.setBounds(fball.xLoc, fball.yLoc, fball.xLoc + fball.xSize, fball.ySize + fball.ySize);
banner.setBounds(0, 0, getWidth(), getHeight());
fball.setBounds(0, 75, getWidth(), getHeight());
add(fball, null);
add(banner, null);
setVisible(true);
while(true){
System.out.println("Fball.xLoc: " + fball.xLoc + ", Fball.yLoc: " + fball.yLoc);
repaint();
try{
Thread.sleep(10);
}catch (Exception e){
}
}
}
/*
public void paint(Graphics g) {
super.paintComponents(g);
}
*/
}
Image Class-
package game.thirdTry;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class WindowStructure extends JPanel {
private static WindowStructure fball;
public static WindowStructure getInstanceF(){
if(fball == null){
fball = new WindowStructure("fireball.100x100.png", 0, 75, 100, 100);
}
return fball;
}
ImageIcon imageIcon;
int xLoc, yLoc, xSize, ySize;
public WindowStructure(String bannerImg, int xLoc, int yLoc, int xSize, int ySize){
URL bannerImgURL = getClass().getResource(bannerImg);
imageIcon = new ImageIcon(bannerImgURL);
this.xLoc = xLoc;
this.yLoc = yLoc;
this.xSize = xSize;
this.ySize = ySize;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(imageIcon.getImage(), xLoc, yLoc, xSize, ySize, null);
}
}
KeyListener / User Input Class-
package game.thirdTry;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class UserInput implements KeyListener {
// Window window = Window.getInstance();
WindowStructure fball = WindowStructure.getInstanceF();
boolean goUp = false;
boolean goDown = false;
boolean goLeft = false;
boolean goRight = false;
public void moveUpDown() {
if (goUp && goDown) {
if (fball.yLoc > 0) {
fball.yLoc -= 10;
}
} else if (goUp) {
if (fball.yLoc > -5) {
fball.yLoc -= 10;
}
} else if (goDown) {
if (fball.yLoc < 480) {
fball.yLoc += 10;
}
}
System.out.println("moveUpDown() method called");
}
public void moveLeftRight() {
if (goLeft && goRight) {
if (fball.xLoc < 1100) {
fball.xLoc += 10;
}
} else if (goRight) {
if (fball.xLoc < 1110) {
fball.xLoc += 10;
}
} else if (goLeft) {
if (fball.xLoc > 0) {
fball.xLoc -= 10;
}
}
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
goUp = true;
goDown = false;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
goDown = true;
goUp = false;
}
if (e.getKeyCode() == KeyEvent.VK_A) {
goLeft = true;
goRight = false;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
goRight = true;
goLeft = false;
}
moveUpDown();
moveLeftRight();
System.out.println("keyPressed() was called");
}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
goUp = false;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
goDown = false;
}
if (e.getKeyCode() == KeyEvent.VK_A) {
goLeft = false;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
goRight = false;
}
}
#Override
public void keyTyped(KeyEvent e) {
}
}

//g2d.drawImage(imageIcon.getImage(), xLoc, yLoc, xSize, ySize, null);
The problem is that you are scaling the image as you paint the image. Therefore the image takes up the entire window space and so yes it paints over top of the image painted first.
Just paint the image at its real size:
g2d.drawImage(imageIcon.getImage(), xLoc, yLoc, this);
This is another reason to use a JLabel. You don't need to worry about the size. You just position the label at a specific location and the image will be painted. The code is much simpler. And using this approach you don't need to worry about making the panel non-opaque.
The problem with your code now is that you need to make the panel a large size because you are painting the image relative to your WindowStructure panel. So if your image is (25 x 25) and you want the image painted at (100, 100), you need to make the panel (125, 125), which means the panel need to be transparent so the part of the panel that does not contain the image is not painted. Using a JLable, the size of the label will always be (25, 25) so you simple set the location of the label and only (25, 25) pixels will be painted.
I'll look more into JLabels later
Look at it now. There is no time like the present!
Also, your WindowStructure class has a terrible design. There is no need for the static methods and variables. Each image should be independent of one another.

Related

Java 'Background' opens to small to see anything

I am making a game and the game window will only open to the size of the title. Please let me know what changes I need to make the "b.setSize(900,885)" work. I will change or add any amount of code if needed.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Background extends JFrame implements KeyListener, ActionListener{
int platform1X = 300, platform1Y = 555, platform2X, platform2Y;
Image image;
public boolean isJumping = false;
int playerx = 100, playery = 585, velX = 0, velY = 0;
public boolean[] keyDown = new boolean[4];
private int fallingSpeed = 0;
private int gravity = 5;
private int jumpPower = -30;
public Background(){
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
keyDown[0] = false;
keyDown[1] = false;
keyDown[2] = false;
keyDown[3] = false;
}
public void paint(Graphics g){
if(!isOnGround()){
fall();
}
ImageIcon i = new ImageIcon("/Users/kairotieremorton/Documents/java code/Kairo and Enoch Game/src/images/Backgroundsquareman.png");
image = i.getImage();
g.drawImage(image, 0, 0, 900, 700, null);
g.setColor(Color.BLUE);
g.fillRect(platform1X, platform1Y, 120, 20);
g.setColor(Color.RED);
g.fillRect(playerx, playery, 30, 30);
try {
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
playery = playery + velY;
playerx = playerx + velX;
}
public static void main(String[] args){
Background b = new Background();
b.setTitle("Game");
b.setSize(900,885);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(isOnGround() && key == KeyEvent.VK_SPACE){
keyDown[0] = true;
jump();
}
//if(key == KeyEvent.VK_DOWN){ setvelY(5); keyDown[1] = true;}
if(key == KeyEvent.VK_LEFT) { setvelX(-5); keyDown[2] = true;}
if(key == KeyEvent.VK_RIGHT) { setvelX(5); keyDown[3] = true;}
System.out.println(key);
System.out.println(playerx);
System.out.println(playery);
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE){ keyDown[0] = false;} //tempObject.setVelY(0);
//if(key == KeyEvent.VK_DOWN) keyDown[1] = false;//tempObject.setVelY(0);
if(key == KeyEvent.VK_LEFT) keyDown[2] = false;//tempObject.setVelX(0);
if(key == KeyEvent.VK_RIGHT) keyDown[3] = false; //tempObject.setVelX(0);
//vertical movement
if(!keyDown[0]) setvelY(0); System.out.println(isOnGround());
//horizontal movement
if(!keyDown[2] && !keyDown[3]) setvelX(0);
}
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {
//playery = playery + velY;
//playerx = playerx + velX;
//repaint();
}
public boolean isOnGround(){
if(playery + (30 /2) >= 585) {
return true;
}
return false;
}
public void fall(){
playery = playery + fallingSpeed;
fallingSpeed = fallingSpeed + gravity;
}
public void jump(){
fallingSpeed = jumpPower;
fall();
}
public void setvelX(int velX) {
this.velX = velX;
}
public void setvelY(int velY) {
this.velY = velY;
}
public float getvelX(){
return velX;
}
public float getvelY(){
return velY;
}
public int getplayerx(){
return playerx;
}
public int getplayery(){
return playery;
}
public void setplayerx(int playerx) {
this.playerx = playerx;
}
public void setplayery(int playery) {
this.playery = playery;
}
public Rectangle getBounds(){
return new Rectangle(getplatform1X(), platform1Y(), 120, 20);
}
public int getplatform1X(){
return platform1X;
}
public int platform1Y(){
return platform1Y;
}
public Rectangle getBoundsTop(){
return new Rectangle(getplayerx() + 10, getplayery(), 30 - 20, 5);
}
public Rectangle getBoundsBottom(){
return new Rectangle(getplayerx() + 10, getplayery() + 30 - 5, 30 - 20, 5);
}
public Rectangle getBoundsLeft(){
return new Rectangle(getplayerx(), getplayery()+10, 5, 30 -20);
}
public Rectangle getBoundsRigth(){
return new Rectangle(getplayerx() + 30 - 5, getplayery()+10, 5, 30 - 20);
}
}
Okay, that took a lot of looking, but...
public Rectangle getBounds() {
return new Rectangle(getplatform1X(), platform1Y(), 120, 20);
}
is your problem. JFrame#getBounds is a method used by JFrame to return the position and the size of the component and since you've overridden it, it will now ignore any values you pass to setSize (directly or indirectly)
Have a read of the following for reasons why you shouldn't override paint of top level containers like JFrame
How can I set in the midst?
Graphics rendering in title bar
Java JFrame .setSize(x, y) not working?
How to get the EXACT middle of a screen, even when re-sized
As a general rule of thumb, you should avoid extending from JFrame, as you're not really adding any new functionality to the class, it locks you into a single use case and, based on your experience, can cause other problems.
You should start with something like JPanel and override its paintComponent method instead (and make sure you call super.paintComponent before you do any custom painting)
Take a look at Painting in AWT and Swing and Performing Custom Painting
You paint methods should run as fast as possible, so you should avoid loading resources like loading images, within the paint method
Swing is single threaded, so calling Thread.sleep in the paint method will cause the entire program to stop, including it's ability to respond to new events from the user.
I'd also discourage you from using KeyListener, using the key bindings API will be more reliable. Take a look at How to Use Key Bindings for more details
Edit:
A better way to achieve the same result is to call b.setPreferredSize(dimension); and then pack() your frame.
See this answer for more information about JFrame.pack() :
https://stackoverflow.com/a/22982334/5224040
Try this in your main method:
public static void main(String[] args) {
Background b = new Background();
Dimension d = new Dimension(900, 885); //Create a new Dimension
b.setTitle("Game");
b.setPreferredSize(d); //Set the PreferredSize;
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.pack(); //Pack the frame
b.setVisible(true); //Setting Visible
}
You can also use the following methods to reposition your frame as necessary:
b.setLocationRelativeTo(null); //center the location on the screen.
b.setLocation(0, 0); //Set location explicitly
Call either of these methods below b.pack(); to reposition your frame.

JAVA setColor in paintComponent stays blank

I'm trying to change the background of my little programm as soon as someone hits space (32). It just won't work and I have been trying different things and everything I could find on the internet like putting the g.setColor(Color.BLUE);
at the beginning of the public void paintComponent(Graphics g)block.
Here I got the following code.
My Screen.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Screen extends JPanel implements Runnable{
Thread thread = new Thread(this);
Frame frame;
private int fps = 0;
public int scene = 0;
public boolean running = false;
public Screen(Frame frame){
this.frame = frame;
this.frame.addKeyListener(new KeyHandler(this));
thread.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
super.paint(g);
g.clearRect(0, 0, this.frame.getWidth(),this.frame.getHeight());
g.drawString(fps + "", 10, 10);
if (scene == 0) {
g.setColor(Color.BLUE);
} else if (scene == 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.white);
}
g.fillRect(0, 0, getWidth(), getHeight());
}
public void run() {
System.out.println("[Success] Frame Created!");
long lastFrame = System.currentTimeMillis();
int frames = 0;
running = true;
scene = 0;
while(running){
repaint();
frames++;
if(System.currentTimeMillis() - 1000 >= lastFrame){
fps = frames;
frames = 0;
lastFrame = System.currentTimeMillis();
}
try {
Thread.sleep(2);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.exit(0);
}
public class KeyTyped{
public void keySPACE() {
scene = 1;
}
public void keyESC(){
running = false;
}
}
}
KeyHandler.java
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener {
public Screen screen;
public Screen.KeyTyped keyTyped;
public KeyHandler(Screen screen){
this.screen = screen;
this.keyTyped = this.screen.new KeyTyped();
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode == 27){
this.keyTyped.keyESC();
}
if(keyCode == 32){
this.keyTyped.keySPACE();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
I don't know why the code isn't in one block. Seems like I'm doing something wrong with it?
You should use InputMap / ActionMap instead of a KeyListener
You need to make sure the JComponent is set to isFocusable(true), and you do component.requestFocus() when first shown to make sure that your action will trigger.
please do the corrections:
1. use JFrame instead of Frame
2. implemement paintComponent like this:
g.clearRect(0, 0, this.frame.getWidth(),this.frame.getHeight());
if (scene == 0) {
g.setColor(Color.BLUE);
} else if (scene == 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.white);
}
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawString(fps + "", 10, 10);
3. make sure Screen is added JFrame
4. make sure to issue setVisible(true) on JFrame

Trying to fire a bullet and detect a rectangle for collision

I created an oval which shoots a bullet(rectangle) that if it hits the red rectangle, the collision would detect the bullet hitting the rectangle. However, when I run my program, the collision has already been done and I haven't even pressed the space bar yet to shoot. What I wanted to happen is that, when I shoot and if the bullet hits the rectangle, that's when it detects the collision. I tried to analyze it and do something but I just couldn't get it. here is my source code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaGame2 extends JPanel implements KeyListener,Runnable{
//variables
JFrame frame;
int x, y, xDir, yDir,bx,by;
Rectangle bullet;
boolean readyTofire, shot = false;
//constructor for game
public JavaGame2(){
frame = new JFrame("Java Game");
int x=150;
int y=150;
}
//drawings
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
Rectangle rec = new Rectangle(50, 20, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(x, y, 55, 55);
g.fillRect(x+23, y-15, 10, 20);
g.setColor(Color.RED);
g.fillRect(rec.x, rec.y, rec.width, rec.height);
bullet = new Rectangle(bx, by, 5, 3);
if(shot){
g.setColor(Color.BLACK);
g.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
if(bullet.intersects(rec));
g.drawString("collision!", 50, 20);
repaint();
}
public void setxDir(int xdir){
xDir = xdir;
}
public void setyDir(int ydir){
yDir = ydir;
}
//key event listener keypressed
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_UP){
setyDir(-1);
}
if(code == KeyEvent.VK_DOWN){
setyDir(+1);
}
if(code == KeyEvent.VK_LEFT){
setxDir(-1);
}
if(code == KeyEvent.VK_RIGHT){
setxDir(+1);
}
if(code == KeyEvent.VK_SPACE){
if(bullet == null ){
readyTofire = true;
if(readyTofire){
bx = x+26;
by = y-15;
bullet = new Rectangle(bx, by, 5, 3);
shot = true;
}
}
}
}
//key event listener for key released
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_UP){
setyDir(0);
}
if(code == KeyEvent.VK_DOWN){
setyDir(0);
}
if(code == KeyEvent.VK_LEFT){
setxDir(0);
}
if(code == KeyEvent.VK_RIGHT){
setxDir(0);
}
if(code == KeyEvent.VK_SPACE){
readyTofire = false;
if(bullet.y <= -5){
bullet = new Rectangle(0, 0, 0, 0);
shot = false;
readyTofire = true;
}
}
}
public void keyTyped(KeyEvent e) {
}
//shot of bullet
public void shoot(){
if(shot){
bullet.y--;
}
}
//movement of the oval
public void move(){
x += xDir;
y += yDir;
if(x <= 0){
x = 0;
}
else if(x >= 500){
x = 500;
}
else if(y <= 0){
y = 0;
}
else if(y >= 500){
y = 500;
}
}
//thread
public void run() {
try{
while(true){
shoot();
move();
Thread.sleep(5);
}
}
catch(Exception e){
System.out.println("error!");
}
}
}
Take a moment to have a look at you collision detection statement...
if (bullet.intersects(rec));
g.drawString("collision!", 50, 20);
The problem is, you've ended your if statement with a ;
if (bullet.intersects(rec));
^---
This means that the statement is effectively ignored, it would be the same as...
if (bullet.intersects(rec)) {
}
g.drawString("collision!", 50, 20);
Which would have alerted you to the problem in the first place
Instead, try using
if (bullet.intersects(rec)) {
g.drawString("collision!", 50, 20);
}
Now, don't call repaint or any method that might call repaint (like setBackground) from any paint method. This will set up a cycle of repeated paint events that will eventually consume your CPU.
I don't think you really need 200fps and Thread.sleep of roughly 40 would give you 25fps and would typically be more then adequate for your purposes.
You should also consider using key bindings, which will allow you to over come the focus related issues of KeyListener.
I'd also encourage you to explore the use of a Swing Timer which will reduce the possibility of un-synchorised updates between the model and the view, which could cause random and difficult to solve artificates or other issues...
Shooting Problems
There's a bunch of logic issues and mis-use of variables...
To start with...
Remove bx and by, you don't need them. You actually don't really need x and y either, but that might be come a little more apparent as we go...
Don't create bullet in paintComponent, this is causing some confusion...
You may not need the keyReleased check of VK_SPACE but if you did, you should set shot to false and bullet to null...
Setting readyToFire to true right before you check to see if it's true seems weird...however, when you detect a VK_SPACE in keyPressed check to see if bullet is null or not, if it's not, then a bullet already exist, don't know if this is an issue, but if you were to use a List, you could have multiple bullets firing at the same time...any way, create a new Rectangle and assign to to bullet
if (code == KeyEvent.VK_SPACE) {
readyTofire = true;
if (readyTofire) {
int bx = x + 26;
int by = y - 15;
bullet = new Rectangle(bx, by, 5, 3);
shot = true;
}
}
In you shoot method, do forget to do a bounds check...
public void shoot() {
if (shot) {
bullet.y--;
// Have we past the edge of the screen
if (bullet.y < 0) {
shot = false;
bullet = null;
}
}
}
And finally, paint it all...Here you should be taking advantage of the 2D Graphics API
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Rectangle rec = new Rectangle(50, 20, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(x, y, 55, 55);
g.fillRect(x + 23, y - 15, 10, 20);
g.setColor(Color.RED);
g.fillRect(rec.x, rec.y, rec.width, rec.height);
if (shot && bullet != null) {
g2d.setColor(Color.BLACK);
g2d.fill(bullet);
if (bullet.intersects(rec)) {
g2d.drawString("collision!", 50, 20);
}
}
g2d.dispose();
}
And in case I missed anything, here's you the code I tested with...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class JavaGame2 extends JPanel implements KeyListener, Runnable {
//variables
int x, y, xDir, yDir;
Rectangle bullet;
boolean readyTofire, shot = false;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JavaGame2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
//constructor for game
public JavaGame2() {
int x = 150;
int y = 150;
this.setBackground(Color.WHITE);
setFocusable(true);
addKeyListener(this);
requestFocusInWindow();
Thread t = new Thread(this);
t.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
//drawings
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Rectangle rec = new Rectangle(50, 20, 50, 50);
g.setColor(Color.BLUE);
g.fillOval(x, y, 55, 55);
g.fillRect(x + 23, y - 15, 10, 20);
g.setColor(Color.RED);
g.fillRect(rec.x, rec.y, rec.width, rec.height);
if (shot && bullet != null) {
g2d.setColor(Color.BLACK);
g2d.fill(bullet);
if (bullet.intersects(rec)) {
g2d.drawString("collision!", 50, 20);
}
}
g2d.dispose();
}
public void setxDir(int xdir) {
xDir = xdir;
}
public void setyDir(int ydir) {
yDir = ydir;
}
//key event listener keypressed
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
setyDir(-1);
}
if (code == KeyEvent.VK_DOWN) {
setyDir(+1);
}
if (code == KeyEvent.VK_LEFT) {
setxDir(-1);
}
if (code == KeyEvent.VK_RIGHT) {
setxDir(+1);
}
if (code == KeyEvent.VK_SPACE) {
readyTofire = true;
if (readyTofire) {
int bx = x + 26;
int by = y - 15;
bullet = new Rectangle(bx, by, 5, 3);
shot = true;
}
}
}
//key event listener for key released
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
setyDir(0);
}
if (code == KeyEvent.VK_DOWN) {
setyDir(0);
}
if (code == KeyEvent.VK_LEFT) {
setxDir(0);
}
if (code == KeyEvent.VK_RIGHT) {
setxDir(0);
}
// if (code == KeyEvent.VK_SPACE) {
// readyTofire = false;
// if (bullet.y <= -5) {
// bullet = new Rectangle(0, 0, 0, 0);
// shot = false;
// readyTofire = true;
//
// }
// }
}
public void keyTyped(KeyEvent e) {
}
//shot of bullet
public void shoot() {
if (shot) {
bullet.y--;
if (bullet.y < 0) {
shot = false;
bullet = null;
}
}
}
//movement of the oval
public void move() {
x += xDir;
y += yDir;
if (x <= 0) {
x = 0;
} else if (x >= 500) {
x = 500;
} else if (y <= 0) {
y = 0;
} else if (y >= 500) {
y = 500;
}
}
//thread
public void run() {
try {
while (true) {
shoot();
move();
repaint();
Thread.sleep(40);
}
} catch (Exception e) {
System.out.println("error!");
}
}
}
No offense, but you approach is a little primitive. Instead of containing the entire game model within the same class that is trying to display it, you should separate your game "logic" into a model and update the model state and then simply have your view paint the model...IMHO

Cant understand how to get this collision detection to work what am i doing wrong?

I need help trying to sort how this collision detection in my code.
Here is my code and I have had a little assistance from various sources online but nothing with a definite explanation to it yet, but all in all I have made a lot of progress. So basically a quick thank you internet.
What I have to do is make a game where I control an object, in this case a blue circle and then touch another on screen object, in this case a yellow circle and when they touch the yellow circle will dissapear and the word "YUM" will take its place then the yellow circle will move randomly to a new location.
This is a lot like the game snake but I can't get the collision to work right and also if I did have it working I have no idea how I would update the location to a new location randomly on the screen.
here is the code:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.JFrame;
import org.w3c.dom.css.Rect;
public class javagame2 extends JFrame {
private Image dbimage;
private Graphics dbg;
Font font = new Font("Arial", Font.BOLD, 30);
static Random generator = new Random();
static int randomIndex0 = generator.nextInt(900);
static int randomIndex1 = generator.nextInt(400);
public static class player
{
static int x = 150;
static int y = 150;
static final int Width = 50;
static final int Height = 50;
}
public static class pizza
{
static final int x1 = randomIndex0;
static final int y1 = randomIndex1;
int Width = 50;
int Height = 50;
public boolean intercepts(player p) {
// TODO Auto-generated method stub
return false;
}
}
public class AL extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == e.VK_LEFT){
if(player.x <= 0)
{
player.x = 0;
}
else
{
player.x += -10;
}
}
if(keycode == e.VK_RIGHT){
if(player.x >= 950)
{
player.x = 950;
}
else
{
player.x += 10;
}
}
if(keycode == e.VK_UP){
if(player.y <= 20)
{
player.y = 20;
}
else
{
player.y += -10;
}
}
if(keycode == e.VK_DOWN){
if(player.y >= 450)
{
player.y = 450;
}
else
{
player.y += 10;
}
}
}
public void keyReleased(KeyEvent e){}
}
public int getX()
{
return player.x;
}
public int getY()
{
return player.y;
}
/*
public boolean collide(pizza f)
{
if(f.X <= (player.x+40) && f.Y >= (player.y+40))
{
System.out.println("they collided");
return true;
}
else
{
return false;
}
}
*/
public boolean collide(pizza f, player p)
{
if(f.x1 <= p.x && f.y1 >= p.y)
{
System.out.println("they collided");
return true;
}
else
{
return false;
}
}
public javagame2()
{
addKeyListener(new AL());
setTitle("Ballon Popper");
setSize(1000, 500);
setResizable(false);
setVisible(true);
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paintcomponent(Graphics g)
{
g.setColor(Color.WHITE);
g.drawRect(pizza.x1, pizza.y1, 50, 50);
g.setFont(font);
g.setColor(Color.RED);
g.drawString("Eat the Pizza!", 350, 50);
g.setColor(Color.BLUE);
g.drawRect(getX(), getY(), 50, 50);
g.fillOval(getX(), getY(), 50, 50);
g.setColor(Color.YELLOW);
g.fillOval(pizza.x1, pizza.y1, 50, 50);
repaint();
}
public void paint(Graphics g)
{
dbimage = createImage(getWidth(), getHeight());
dbg = dbimage.getGraphics();
paintcomponent(dbg);
g.drawImage(dbimage, 0, 0, this);
}
public static void main(String arg[])
{
new javagame2();
}
}
Here is the updated code I just worked out a little bit, it does run but I'm a bit confused on how to go about removing the yellow circle once I have touched or even what to do, I tried using .intersects but that didn't seem to do anything.
I don't know java, but this seems straightforward.
Two circles collide when the distance between their centers is less than the sum of their radii. (And if the circles are the same size, we needn't worry about whether their "locations" are actually at their centers or not.)
public boolean collide(pizza f, player p)
{
if(Math.pow(f.x1-p.x, 2) + Math.pow(f.y1-p.y, 2) <= Math.pow(f.Width/2 + p.Width/2, 2))
{
System.out.println("they collided");
return true;
}
else
{
return false;
}
}

Running Game in a Java Applet

I have written a small 2d scroller with the assistance of different snippets of code I have found on-line. The original package run as a JFrame application but I am trying to convert it into an applet. When I Run the program in Eclipse I do not receive any debugging errors just a blank applet viewer... I don't think I am missing anything from what I have read from different applet creation sources but maybe it is something very simple.
Frame class
package OurGame;
import java.awt.*;
import javax.swing.*;
public class Frame extends JApplet {
public Frame() {
JPanel frame = new JPanel();
frame.add(new Board());
// frame.setTitle("2D PLATFORMER");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,365);
frame.setVisible(true);
//frame.setLocationRelativeTo(null);
//setContentPane(frame);
}
// public static void main(String[] args){
public void init() {
new Frame();
}
}
Ive commented out the containers that were only workable in Jframe.
Dude class
package OurGame;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Dude {
int x, dx, y,nx,nx2,left, dy;
Image still,jump,reverse;
ImageIcon s = new ImageIcon("redirect.png");
ImageIcon j= new ImageIcon("redirect.png");
ImageIcon l = new ImageIcon("redirect.png");
public Dude() {
x = 75;
left = 150;
nx = 0;
nx2= 685;
y = 172;
still = s.getImage();
}
public void move() {
if (dx != -1){
if (left + dx <= 150)
left+=dx;
else{
x = x + dx;
nx2= nx2+dx;
nx = nx + dx;
}}
else
{
if (left+dx >0)
left = left + dx;
}
}
public int getX() {
return x;
}
public int getnX() {
return nx;
}
public int getnX2() {
return nx2;
}
public int getdx() {
return dx;
}
public Image getImage() {
return still;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{ dx = -1;
still = l.getImage(); }
if (key == KeyEvent.VK_RIGHT)
{dx = 1;
still = s.getImage();
}
if (key == KeyEvent.VK_UP)
{dy = 1;
still = j.getImage();
} }
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
dx = 0;
if (key == KeyEvent.VK_RIGHT)
dx = 0;
if (key == KeyEvent.VK_UP)
{dy = 0;
still = s.getImage();}
}
}
Board Class
package OurGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener, Runnable {
Dude p;
public Image img;
Timer time;
int v = 172;
Thread animator;
boolean a = false;
boolean done2 = false;
public Board() {
p = new Dude();
addKeyListener(new AL());
setFocusable(true);
ImageIcon i = new ImageIcon("redirect.jpg");
img = i.getImage();
time = new Timer(5, this);
time.start();
}
public void actionPerformed(ActionEvent e) {
p.move();
repaint();
}
public void paint(Graphics g) {
if (p.dy == 1 && done2 == false) {
done2 = true;
animator = new Thread(this);
animator.start();
}
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
if ((p.getX() - 590) % 2400 == 0)// p.getX() == 590 || p.getX() == 2990)
p.nx = 0;
if ((p.getX() - 1790) % 2400 == 0)// p.getX() == 1790 || p.getX() == 4190)
p.nx2 = 0;
g2d.drawImage(img, 685 - p.getnX2(), 0, null);
if (p.getX() > 590) {
g2d.drawImage(img, 685 - p.getnX(), 0, null);
}
g2d.drawImage(p.getImage(), p.left, v, null);
if (p.getdx() == -1) {
g2d.drawImage(img, 685 - p.getnX2(), 0, null);
g2d.drawImage(p.getImage(), p.left, v, null);
}
}
private class AL extends KeyAdapter {
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}
}
boolean h = false;
boolean done = false;
public void cycle() {
if (h == false)
v--;
if (v == 125)
h = true;
if (h == true && v <= 172) {
v++;
if (v == 172) {
done = true;
}
}
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (done == false) {
cycle();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = 10 - timeDiff;
if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
done = false;
h = false;
done2 = false;
}
}
I am a little stumbled after doing a fair amount of research. I am thinking Eclipse might not recognise that I have multiple class files but I kind of proved that theory wrong by writing a html page to display my applet that runs fine but is completely empty.
why is the applet viewer even in eclipse blank when running it..
At no point is anything added to the applet container. To add something to the applet would require overriding the applet init() method and calling add(new Board());. (That could also be done in the constructor, but it is more common to build an applet GUI within the init() method.)
Other Notes
paint(Graphics)
Since Board is a Swing class that is not a top-level container, custom painting should be done in the paintComponent(Graphics) method, rather than paint(Graphics).
Nomenclature
JPanel frame = new JPanel();
Wow! Poorly chosen attribute name. What do you call your JFrame instances, panel?
Application resources
ImageIcon s = new ImageIcon("redirect.png");
This will not work for an applet, and would not work for a deployed app. It is necessary to access images by URL. The Applet class has a specific method for loading images.
Remaining lines of constructor
JPanel frame = new JPanel();
frame.add(new Board());
frame.setSize(700,365);
frame.setVisible(true);
The first line of the constructor is not needed, the Board created in the next line can be added directly to the applet. That leaves two more lines not commented out.
frame.setSize(700,365);
The size of an applet should be set by the HTML.
frame.setVisible(true);
Anything added to a component that is visible will itself become visible. As such, this is also redundant.
Swing Timer
Since I pointed out so many faults in the code, just thought I should add that the animation seems to be done correctly - using a Swing Timer. :)

Categories

Resources