Here's my code:
-------------
GameFrame class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameFrame extends JPanel implements ActionListener
{
static JButton shoot;
Graphics g2d;
JPanel cards;
public GameFrame()
{
setFocusable(true);
}
public void paint(Graphics g)
{
super.paint(g);
g2d = (Graphics)g;
}
public void actionPerformed(ActionEvent a)
{
/*if(a.getActionCommand().equals("Go to Level 1")){
CardLayout cl=(CardLayout)(cards.getLayout());
cl.show(cards, "l1");
}
else if(a.getActionCommand().equals("Go to Level 2")){
CardLayout cl=(CardLayout)(cards.getLayout());
cl.show(cards, "l2");
}
else if(a.getActionCommand().equals("Go to Level 3")){
CardLayout cl=(CardLayout)(cards.getLayout());
cl.show(cards, "l3");
}
else if(a.getActionCommand().equals("Go to Menu")){
CardLayout cl=(CardLayout)(cards.getLayout());
cl.show(cards, "m1");
}
else
System.exit(0);*/
}
public void setUp()
{
JFrame frame = new JFrame("Bull's Eye");
JPanel panel = new Level1();
JPanel menu=new JPanel();
/*JPanel level1=new JPanel();
JPanel level2=new JPanel();
JPanel level3=new JPanel();
JMenuBar menubar=new JMenuBar();
JMenu file=new JMenu("Levels");
JMenuItem eMenuItem4=new JMenuItem("Go to Menu");
JMenuItem eMenuItem= new JMenuItem("Go to Level 1");
JMenuItem eMenuItem2=new JMenuItem("Go to Level 2");
JMenuItem eMenuItem3=new JMenuItem("Go to Level 3");
JMenuItem eMenuItem5=new JMenuItem("Exit");
eMenuItem.addActionListener(this);
eMenuItem2.addActionListener(this);
eMenuItem3.addActionListener(this);
eMenuItem4.addActionListener(this);
file.add(eMenuItem);
file.add(eMenuItem2);
file.add(eMenuItem3);
file.add(eMenuItem4);
file.add(eMenuItem5);
menubar.add(file);
frame.setJMenuBar(menubar);
cards=new JPanel(new CardLayout());
cards.add(menu,"m");
cards.add(level1,"l1");
cards.add(level2,"l2");
cards.add(level3,"l3");
frame.add(cards, BorderLayout.CENTER);*/
frame.setSize(1000, 800);
shoot = new JButton("Click to shoot!");
panel.add(shoot);
shoot.addActionListener(new Level1());
shoot.addKeyListener(new Level1());
frame.add(panel);
frame.setTitle("Bull's Eye!");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); }
public static void main(String[]args)
{
GameFrame gf = new GameFrame();
gf.setUp();
}
}
Level1 class (where I draw the stuff)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Level1 extends JPanel implements ActionListener, KeyListener
{
Image background;
Image target;
Image Player_char;
Image arrow;
static int x = 530, velx = 1, arrowY = 230;
static int y = 170, vely = 1;
//Find a way for the arrow to seamlessly shoot out!
Timer mainTimer = new Timer(10, this);
public Level1()
{
ImageIcon ic = new ImageIcon("src/background.jpg");
background = ic.getImage();
ImageIcon ic3 =new ImageIcon("src/target.png");
target=ic3.getImage();
ImageIcon ic2 = new ImageIcon("src/character.png");
Player_char = ic2.getImage();
ImageIcon ic4 = new ImageIcon("src/arrow.png");
arrow = ic4.getImage();
}
public void keyPressed(KeyEvent k)
{
int key = k.getKeyCode();
if (key == KeyEvent.VK_UP)
{
arrowY = 130;
x = 530;
}
else if (key == KeyEvent.VK_DOWN)
{
arrowY = 330;
x = 530;
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
public void paintComponent(Graphics g2d)
{
g2d.drawImage(background, 0, 0, null);
g2d.drawImage(target, 4, y, null);
g2d.drawImage(Player_char, 700, 140, null);
g2d.drawImage(arrow, x, arrowY, null);
mainTimer.start();
}
public void checkCollisions()
{
//Rectangle r1 = ((Shape) arrow).getBounds();
//Rectangle r2 = ((Shape) target).getBounds();
//if (r1.intersects(r2))
//{
//
//}
//What do I do here?
}
public void actionPerformed(ActionEvent a) //Add the change of positioning of arrow!
{
if (a.getSource() == GameFrame.shoot)
{
arrow l1=new arrow();
l1.getx();
}
repaint();
if (y < 0 || y > 500) //Add a moving obstacle! And animate it like this...
vely = -vely;
y = y - vely;
repaint();
}
}
Arrow class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class arrow implements ActionListener
{
Timer timer = new Timer(5, this);
public arrow()
{
timer.start();
}
public void actionPerformed(ActionEvent e)
{
if (Level1.x <= 0 || Level1.x >= 1000)
Level1.velx = Level1.velx;
Level1.x = Level1.x - Level1.velx;
}
public int getx (){
int x=Level1.x;
return(x);
}
}
This post has been edited by macosxnerd101: 04 April 2016 - 05:30 PM
Reason for edit:: Please post your code between the code tags: [code] Your code goes here [/code]

New Topic/Question
Reply


MultiQuote

|