package game;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.*;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public Input listener;
private boolean running;
private Image green;
private CopyOnWriteArrayList<Tower1> Tower1List1;
private Image tower1Image;
private Item Tower1Icon;
private Image sidebar;
private int icon1Clicked = 0;
private int mousePressed = 0;
private int mouseUsed = 0;
private int drawTower1 = 0;
private int money = 100;
private Point mouse;
public static void main(String[] args){
new Main();
}
public Main(){
JFrame f = new JFrame();
f.setTitle("GAME!!!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024,768);
f.setResizable(false);
f.setVisible(true);
f.createBufferStrategy(2);
f.setBackground(Color.GREEN);
run();
}
public void loadVariables(){
mouse = new Point();
Tower1List1 = new CopyOnWriteArrayList<Tower1>();
tower1Image = new ImageIcon("C:\\test\\redGun.png").getImage();
Tower1Icon = new Item(tower1Image, getWidth()-240,70);
sidebar = new ImageIcon("C:\\test\\sidebar.png").getImage();
green = new ImageIcon("C:\\test\\greenB.jpg").getImage();
}
public void run(){
running = true;
loadVariables();
listener = new Input();
movieLoop();
}
public void movieLoop(){
long startingTime = System.currentTimeMillis();
long cumTime = startingTime;
while(running){
long timePassed = System.currentTimeMillis() - cumTime;
cumTime += timePassed;
update(timePassed);
BufferStrategy s = getBufferStrategy();
LINE 79 -->> Graphics2D g = (Graphics2D) s.getDrawGraphics();
draw(g);
s.show();
g.dispose();
try{
Thread.sleep(20);
}catch(Exception e){}
}
}
public void draw(Graphics g){
setFont(new Font("Arial", Font.PLAIN, 20));
g.drawImage(green,0,0,null);
g.setColor(Color.WHITE);
g.drawImage(sidebar, getWidth()-256,0, null);
g.drawString("$" + Integer.toString(money), getWidth()-200, 45);
g.drawString("$50", getWidth()-237,140);
g.drawImage(Tower1Icon.getImage(), Tower1Icon.getX(), Tower1Icon.getY(),null);
for (Tower1 tower1 : Tower1List1){
g.drawImage(tower1.getI(),tower1.getX()-25,tower1.getY()-25,null);
}
if(icon1Clicked == 1){
g.drawImage(Tower1Icon.getImage(),mouse.x-Tower1Icon.getWidth()/2,mouse.y-Tower1Icon.getHeight()/2,null);
}
}
//update position
public void update(long timePassed){
if(drawTower1 == 1){
Tower1 tower1 = new Tower1(mouse.x,mouse.y);
Tower1List1.add(tower1);
money -= 50;
drawTower1 = 0;
}
Rectangle rect1 = new Rectangle(Tower1Icon.getX(), Tower1Icon.getY(),Tower1Icon.getHeight(),Tower1Icon.getWidth());
if(mousePressed == 1 && mouseUsed == 0 ){
if( rect1.contains(mouse.x,mouse.y)){
if(!(money < 50)){
icon1Clicked = 1;
mouseUsed = 1;
}
}
}else{
icon1Clicked = 0;
}
}
//user input
public class Input implements KeyListener, MouseMotionListener, MouseListener, MouseWheelListener{
Input(){
setFocusTraversalKeysEnabled(false);
addKeyListener(this);
addMouseMotionListener(this);
addMouseListener(this);
addMouseWheelListener(this);
}
//mouse click listener
public void mousePressed(MouseEvent e){
mousePressed = 1;
}
//unused mouse functions
public void mouseReleased(MouseEvent e){
mousePressed = 0;
if(mouseUsed == 1){
drawTower1 = 1;
mouseUsed = 0;
}
}
//record mouse position
public void mouseDragged(MouseEvent e){
mouseMoved(e);
}
public void mouseMoved(MouseEvent e){
mouse.x = e.getX();
mouse.y = e.getY();
}
//unused input functions
public void keyPressed(KeyEvent e){e.consume();}
public void keyReleased(KeyEvent e){e.consume();}
public void keyTyped(KeyEvent e){e.consume();}
public void mouseClicked(MouseEvent e){e.consume();}
public void mouseEntered(MouseEvent e){e.consume();}
public void mouseExited(MouseEvent e){e.consume();}
public void mouseWheelMoved(MouseWheelEvent e){
money++;
e.consume();}
}
}