Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import javax.swing.*;
- import java.util.Random;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.ArrayList;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.util.TimerTask;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author justi
- */
- public class test extends javax.swing.JFrame implements KeyListener, MouseListener, MouseMotionListener, Comparable, ActionListener {
- //Commands Guide: Press any key then type in commands. /create window to create a new window and /printwindows to print the properties of the windows.
- public ArrayList<Window> list = new ArrayList<Window>();
- public Random rand = new Random();
- public ArrayList<Window> clicked = new ArrayList<Window>();
- public ArrayList<String> numbers = new ArrayList<String>();
- public boolean windowClicked;
- public int x = 250;
- public int y = 250;
- public int xSpeed = 0;
- public int ySpeed = 0;
- public Window w2;
- public Scanner sc = new Scanner(System.in);
- Timer t = new Timer(5, this);
- Timer t1 = new Timer(5, this);
- int score = 0;
- public Rectangle coin = new Rectangle(50, 50, 20, 20);
- public ArrayList<Rectangle> b = new ArrayList<Rectangle>();
- public boolean dead = false;
- /**
- * Creates new form MainFrame
- */
- public test() {
- t.start();
- initComponents();
- addKeyListener(this);
- addMouseListener(this);
- addMouseMotionListener(this);
- setFocusable(true);
- setFocusTraversalKeysEnabled(false);
- this.setLayout(null);
- Dimension d = new Dimension();
- d.setSize(500, 500);
- this.setSize(d);
- b.add(new Rectangle(50, 50, 50, 50));
- b.add(new Rectangle(50, 150, 50, 50));
- b.add(new Rectangle(50, 250, 50, 50));
- b.add(new Rectangle(50, 350, 50, 50));
- b.add(new Rectangle(50, 450, 50, 50));
- }
- @Override
- public void paint(Graphics g) {
- Robot r = null;
- try {
- r = new Robot();
- } catch (AWTException e) {
- e.printStackTrace();
- }
- Color c = r.getPixelColor(x, y);
- super.paint(g);
- Graphics2D g2 = (Graphics2D) g;
- g2.setColor(Color.BLUE);
- g2.fillRect(x, y, 20, 20);
- g2.setColor(Color.YELLOW);
- g2.fillRect((int) coin.getX(),(int) coin.getY(), (int)(coin.getWidth()), (int)(coin.getHeight()));
- t1.start();
- for (int j = 0; j < b.size(); j++) {
- g2.setColor(Color.GREEN);
- g2.fillRect((int) b.get(j).getX(), (int) b.get(j).getY(), (int) b.get(j).getWidth(), (int) b.get(j).getHeight());
- }
- if(overlaps(coin)){
- coin.setLocation((int) (Math.random() * 450) + 1, (int) (Math.random() * 450) + 1);
- System.out.println(coin.getX());
- System.out.println(coin.getY());
- score++;
- System.out.println(score);
- }
- for(int l = 0; l < b.size(); l++){
- if(overlaps(b.get(l))){
- System.out.println("Game Over!");
- System.out.println("Score:" +score);
- System.exit(0);
- }
- }
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 400, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 300, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new test().setVisible(true);
- }
- });
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- x += xSpeed;
- y += ySpeed;
- int random = (int) (Math.random() * 5);
- for(int k = 0; k < b.size(); k++) {
- if(b.get(k).getX() >= 500){
- b.get(k).setLocation(0, (int)b.get(k).getY());
- random = (int) Math.random() * 5;
- }
- if(random != k) {
- b.get(k).setLocation((int) b.get(k).getX() + 4, (int) b.get(k).getY());
- }
- repaint();
- }
- repaint();
- }
- public void up(){
- xSpeed = 0;
- ySpeed = -1;
- }
- public void down(){
- xSpeed = 0;
- ySpeed = 1;
- }
- public void left(){
- xSpeed = -1;
- ySpeed = 0;
- }
- public void right(){
- xSpeed = 1;
- ySpeed = 0;
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_UP){
- up();
- }
- if(e.getKeyCode() == KeyEvent.VK_DOWN){
- down();
- }
- if(e.getKeyCode() == KeyEvent.VK_LEFT){
- left();
- }
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- right();
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- if(e.getKeyCode() == KeyEvent.VK_UP){
- xSpeed = 0;
- ySpeed = 0;
- }
- if(e.getKeyCode() == KeyEvent.VK_DOWN){
- xSpeed = 0;
- ySpeed = 0;
- }
- if(e.getKeyCode() == KeyEvent.VK_LEFT){
- xSpeed = 0;
- ySpeed = 0;
- }
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- xSpeed = 0;
- ySpeed = 0;
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // TODO Auto-generated method stub
- // System.out.println("mouse clicked");
- }
- @Override
- public void mouseEntered(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent arg0) {
- // TODO Auto-generated method stub
- windowClicked = false;
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- }
- @Override
- public int compareTo(Object String) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- public boolean overlaps (Rectangle r) {
- return x < r.x + r.width && x + 20 > r.x && y < r.y + r.height && y + 20 > r.y;
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- // End of variables declaration//GEN-END:variables
- }
Advertisement
Add Comment
Please, Sign In to add comment