Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.geom.Line2D;
- import java.util.Random;
- public class Drawing extends JPanel implements KeyListener,Runnable
- {
- public static JFrame frame;
- public static JLabel label;
- public static int[] x=new int[20];
- public static int[] y=new int[20];
- public static int a;
- public static Random random;
- public static int p_x=0;
- public static int p_y=0;
- public static int enemy_stop=0;
- public static boolean isin=false;
- public static Thread move;
- public static Rectangle active;
- public static Rectangle[] generated=new Rectangle[20];
- public static boolean ready=false;
- public static int con;
- public static void drawframe(int width,int height)
- {
- frame=new JFrame("The game");
- frame.setLayout(new BorderLayout());
- frame.setSize(width,height);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.setResizable(false);
- }
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- drawenemies(g,5);
- setradius(g);
- setplayer(g);
- if(entered()&&isin!=true)
- {
- isin=true;
- move=new Thread(new Drawing());
- move.start();
- }
- if(entered()==false&&isin==true)
- isin=false;
- for(int i=1;i<=a;i++)
- {
- Line2D left = new Line2D.Double(x[i], y[i]+5, x[i]-150, y[i]+5);
- Line2D right = new Line2D.Double(x[i], y[i]+5, x[i]+150, y[i]+5);
- g.setColor(Color.BLUE);
- if(p_y==y[i])
- {
- if(p_x>x[i])
- g.drawLine((int)right.getX1(),(int)right.getY1(),(int)right.getX2(),(int)right.getY2());
- else
- g.drawLine((int)left.getX1(),(int)left.getY1(),(int)left.getX2(),(int)left.getY2());
- }
- }
- }
- public void drawenemies(Graphics g,int amount)
- {
- random=new Random();
- a=amount;
- enemy_stop=enemy_stop+1;
- if(enemy_stop<=1)
- {
- for(int i=1;i<=amount;i++)
- {
- x[i]=random.nextInt(frame.getWidth());
- y[i]=random.nextInt(frame.getHeight());
- }
- }
- for(int i=1;i<=amount;i++)
- {
- g.fillRect(x[i], y[i], 20, 20);
- Rectangle store=new Rectangle(x[i], y[i], 20, 20);
- generated[i]=store;
- }
- }
- public void setradius(Graphics g)
- {
- g.setColor(Color.RED);
- for(int i=1;i<=a;i++)
- {
- g.drawRect(x[i]-40, y[i]-40, 100, 100);
- }
- }
- public void setplayer(Graphics g)
- {
- g.setColor(Color.GREEN);
- g.fillRect(p_x, p_y, 20, 20);
- g.setColor(Color.BLACK);
- g.drawRect(p_x, p_y, 20, 20);
- }
- public static void main(String args[])
- {
- drawframe(500,500);
- Drawing instance=new Drawing();
- frame.add(instance);
- frame.addKeyListener(instance);
- }
- public boolean entered()
- {
- boolean entered=false;
- for(int i=1;i<=a;i++)
- {
- Rectangle q=new Rectangle(x[i]-40, y[i]-40, 100, 100);
- Rectangle s=new Rectangle(p_x, p_y, 20, 20);
- if(q.contains(s))
- {
- entered=true;
- active=new Rectangle(x[i], y[i], 20, 20);
- }
- }
- return entered;
- }
- @Override
- public void keyPressed(KeyEvent e)
- {
- switch(e.getKeyCode())
- {
- case 39: p_x=p_x+10;repaint();break;
- case 37: p_x=p_x-10;repaint();break;
- case 38: p_y=p_y-10;repaint();break;
- case 40: p_y=p_y+10;repaint();break;
- }
- }
- public void keyReleased(KeyEvent arg0){}
- public void keyTyped(KeyEvent arg0){}
- @Override
- public void run()
- {
- try
- {
- for(int i=1;i<=a;i++)
- {
- if(active.getX()==generated[i].getX())
- {
- con=i;
- while(x[i]!=p_x||y[i]!=p_y)
- {
- if(x[i]>p_x)
- x[i]=x[i]-1;
- else
- x[i]=x[i]+1;
- if(y[i]>p_y)
- y[i]=y[i]-1;
- else
- y[i]=y[i]+1;
- Thread.sleep(100);
- frame.repaint();
- }
- }
- }
- }
- catch(InterruptedException e){}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment