My end game for this panel is that I have an img icon be able to move around the screen and when they land on one of my what are currently buttons the new panel opens up and you get a mini game, i.e. true/false, maze, or word find.
Where I am currently at... I made a basic null layout and put buttons as place holders where the players icon will go to to open the next panel.
I was working on putting a simple rectangle on the screen that would use arrow keyboard listener to move around. I watched tutorials online about creating this as well as searched this data base.
My current code still shows my null layout with with my map img background and buttons with img icons on those buttons. It will not show my rectangle.
Yes I am a student and this is a project from school, my hope is that you give me guidance in the right direction for the 3 main things I am trying to do here. A. Get rectangle on screen and move it. B. Get image icon on rectangle. C. Where should I start with looking into making it so when the object that moves hits a certain spot JLable,Jbutton,Janything I can't think of how to bring up my new panel that I already have made.
Thank you for any help you all can provide.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class map extends JPanel implements ActionListener, KeyListener{
Timer t = new Timer(5,this);
int x = 0, y = 0, velX = 0, velY = 0;
JButton mapButton, worldCampusB, universityParkB, fayetteB, erieB, yorkB,
hazeltonB;
JLabel background;
ImageIcon img;
public map(){
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
setBackground(new Color(9, 49, 98));
setLayout(new BorderLayout());
ImageIcon oldmain = new ImageIcon("images/oldmain.jpg");
ImageIcon hazelton = new ImageIcon("images/hazelton.jpeg");
ImageIcon york = new ImageIcon("images/york.jpg");
ImageIcon erie = new ImageIcon("images/erie.jpg");
ImageIcon fayette = new ImageIcon("images/fayette.jpg");
ImageIcon worldcampus = new ImageIcon("images/worldcampus.png");
background = new JLabel(new ImageIcon("images/pennmap.jpg"));
add (background);
background.setLayout(null);
mapButton = new JButton("Map Menu: Click to return to main menu.");
mapButton.setBounds(new Rectangle(300,20,300,50));
worldCampusB = new JButton("World Campus");
worldCampusB.setIcon(worldcampus);
universityParkB = new JButton("University Park");
universityParkB.setIcon(oldmain);
fayetteB = new JButton("Fayette");
fayetteB.setIcon(fayette);
erieB = new JButton ("Erie");
erieB.setIcon(erie);
yorkB = new JButton ("York");
yorkB.setIcon(york);
hazeltonB = new JButton ("Hazelton");
hazeltonB.setIcon(hazelton);
background.add(mapButton);
background.add(worldCampusB);
background.add(universityParkB);
background.add(fayetteB);
background.add(erieB);
background.add(yorkB);
background.add(hazeltonB);
//adjusted the button locations on the map - jpk5816
worldCampusB.setBounds(new Rectangle (750,20,195,150));
worldCampusB.setHorizontalTextPosition(JButton.CENTER);
worldCampusB.setVerticalTextPosition(JButton.BOTTOM);
universityParkB.setBounds(new Rectangle(380,250,175,140));
universityParkB.setHorizontalTextPosition(JButton.CENTER);
universityParkB.setVerticalTextPosition(JButton.BOTTOM);
fayetteB.setBounds(new Rectangle(40,445,200,150));
fayetteB.setHorizontalTextPosition(JButton.CENTER);
fayetteB.setVerticalTextPosition(JButton.BOTTOM);
erieB.setBounds(new Rectangle(50,100,175,170));
erieB.setHorizontalTextPosition(JButton.CENTER);
erieB.setVerticalTextPosition(JButton.BOTTOM);
yorkB.setBounds(new Rectangle(625,460,185,130));
yorkB.setHorizontalTextPosition(JButton.CENTER);
yorkB.setVerticalTextPosition(JButton.BOTTOM);
hazeltonB.setBounds(new Rectangle(690,190,170,140));
hazeltonB.setHorizontalTextPosition(JButton.CENTER);
hazeltonB.setVerticalTextPosition(JButton.BOTTOM);
}
public void paintCompent(Graphics g){
super.paintComponent(g);
g.setColor(new Color(9, 49, 98));
g.fillRect(x, y, 50, 30);
}
public void actionPerformed(ActionEvent e){
repaint();
x += velX;
y += velY;
}
public void up(){
velY = -1;
velX = 0;
}
public void down(){
velY = 1;
velX = 0;
}
public void left(){
velX = -1;
velY = 0;
}
public void right(){
velX = 1;
velY = 0;
}
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP){
up();
}
if (code == KeyEvent.VK_DOWN){
down();
}
if (code == KeyEvent.VK_LEFT){
left();
}
if (code == KeyEvent.VK_RIGHT){
right();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
Found a way to draw the image that isn't the odd idea for a beginner in the other answer.
in myJPanel
public class myJPanel extends JPanel implements ActionListener {
ImageIcon img; //declare
public myJPanel(){
super();
setBackground(Color.white);
setLayout (new BorderLayout());
credits = new credits();
instructions = new instructions();
characterTheme = new characterTheme();
img = new ImageIcon("images/pennmap.jpg");//grab from images foler.
in map.java which is where I wanted the img drawn.
public class map extends JPanel {
ImageIcon img;
public map (ImageIcon img){
this.img = img;
Dimension size = new Dimension(getWidth(),getHeight());
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g){
g.drawImage(img.getImage(), 0, 0, null);
}
There was much more to the question but it appears there isn't much interest in it so I will close it at this. As this was my first hurdle in this mini project of making this game.