Hello guys, I was hoping to find some assistance with a certain aspect of my program. So far in my program, there is the main menu where you press a start button to begin the game, then you have Link (the character) on the screen moving around. The problem Im having is that, I set an "cave entrance" in the game so that when the character collides with the entrance it will switch to another frame where the character will be in the cave. The problem Im having is that, Link will not be able to detect the entrance, or the cave image wont show up. Any advice?
Frame class:
Code:
package Game;
import javax.swing.*; //import swing class for creation of JFrame
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.*;
public class Frame extends JFrame{
Link a;
static Frame m;
Rectangle startButton = new Rectangle(440, 200, 100, 25);
int SWIDTH = 1000;
int SHEIGHT = 600;
boolean overstart;
boolean gamestart = false;
Dimension screenSize = new Dimension(SWIDTH, SHEIGHT);
Image dbImage;
Graphics dbg;
Rectangle caveentrance = new Rectangle(305, 205, 50, 50);
public Frame(){
this.setSize(screenSize);
this.setResizable(false);
this.setTitle("Modern Link");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Applet will close when exited
this.setSize(1000,600); //Set size of background based on image
this.setVisible(true);
this.setBackground(Color.BLACK);
this.addMouseListener(new MouseHandler());
this.addMouseMotionListener(new MouseHandler());
}
public static void main(String[] args){
m = new Frame();
}
public void startgame(){
gamestart = true;
JFrame frame = new JFrame("Modern Link");//Create frame for applet
frame.add(new Board()); //Add the board where the character and background are
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Applet will close when exited
frame.setSize(1000,600); //Set size of background based on image
this.setVisible(false);
frame.setVisible(true);
}
public void cave(){
JFrame cave = new JFrame("Modern Link");//Create frame for applet
cave.add(new Cave()); //Add the board where the character and background are
cave.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Applet will close when exited
cave.setSize(1000,600); //Set size of background based on image
cave.setVisible(true);
this.setVisible(false);
}
public void caveCollision(){
Rectangle r1 = a.getBounds();
if (r1.intersects(caveentrance)) {
cave();
}
}
@Override
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void draw(Graphics g){
//Menu
g.setFont(new Font("Monospaced", Font.BOLD, 80));
g.setColor(Color.WHITE);
g.drawString("Modern", 360, 90);
g.drawString("Link", 410, 150);
g.setColor(Color.GRAY);
g.fillRect(startButton.x, startButton.y, startButton.width, startButton.height);
g.setFont(new Font("Arial", Font.BOLD, 12));
g.setColor(Color.BLACK);
g.drawString("Start Game", startButton.x+20, startButton.y+17);
if(!overstart){
g.setColor(Color.GRAY);
}
else{
g.setColor(Color.WHITE);
}
repaint();
}
public class MouseHandler extends MouseAdapter {
@Override
public void mouseMoved(MouseEvent e){
int mx = e.getX();
int my = e.getY();
if(mx > startButton.x && mx < startButton.x+startButton.width && my > startButton.y && my < startButton.y+startButton.height)
overstart = true;
else
overstart = false;
}
@Override
public void mousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
if(mx > startButton.x && mx < startButton.x+startButton.width && my > startButton.y && my < startButton.y+startButton.height)
startgame();
}
}}
Cave class:
Code:
package Game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Font;
public class Cave extends JPanel implements ActionListener {
Link d;
Image cave;
Timer time;
ImageIcon c = new ImageIcon("F:/Suncoast/Senior Project/Images/Gun.jpg");
public void cave(){
d = new Link();
setFocusable(true);
ImageIcon i = new ImageIcon("F:/Suncoast/Senior Project/Images/cavei.jpg");
cave = i.getImage();
time = new Timer(5,this);
time.start(); //Related to the action performance
this.addKeyListener(d);
}
public void actionPerformed(ActionEvent e){
d.move();
repaint();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(cave,0,0,null);
g2D.drawImage(d.getImage(),d.getx(),d.gety(),null);
}
}