Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class prog {
- public static void main(String[] args) {
- myFrame okno = new myFrame();
- }
- }
- class myFrame extends JFrame
- {
- public myFrame()
- {
- setBounds(0,0,500,500);
- setVisible(true);
- Container cont = getContentPane();
- myPanel pan = new myPanel();
- cont.add(pan);
- }
- }
- class myPanel extends JPanel
- {
- private int x = 0,y = 0, napr=0, u = x + 25, i = y+25, j = 0;
- private class myKey implements KeyListener
- {
- public void keyPressed(KeyEvent e)
- {
- int key_ = e.getKeyCode();
- if ((key_ == 39)&&(x != 450))
- {
- x=x+5;
- napr = 0;
- }
- if ((key_ == 37)&&(x != 0))
- {
- x=x-5;
- napr = 1;
- }
- if ((key_ == 38)&&(y != 0))
- {
- y=y-5;
- napr = 2;
- }
- if ((key_ == 40)&&(y != 450))
- {
- y=y+5;
- napr = 3;
- }
- if (key_ == 10)
- {
- j = 1;
- }
- }
- public void keyReleased(KeyEvent e){}
- public void keyTyped(KeyEvent e){}
- }
- public myPanel()
- {
- addKeyListener(new myKey());
- setFocusable(true);
- Timer time = new Timer(1, new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if (j == 1)
- {
- if (napr == 0)
- {
- u = u +10;
- }
- }
- else
- {
- u = x+25;
- i = y+25;
- }
- if (u >= 500)j = 0;
- repaint();
- }
- });
- time.start();
- }
- public void paintComponent(Graphics gr)
- {
- super.paintComponent(gr);
- gr.setColor(Color.black);
- gr.fillRect(x, y, 50, 50);
- gr.fillRect(u, i, 1, 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement