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
- {
- private int z = 0;
- String z1 = Integer.toString(z);
- public myFrame()
- {
- setBounds(0,0,500,500);
- setVisible(true);
- Container cont = getContentPane();
- myPanel pan = new myPanel();
- cont.add(pan);
- JLabel lab = new JLabel(z1);
- lab.setSize(10, 10);
- lab.setLocation(0,0);
- cont.add(lab);
- }
- }
- class myPanel extends JPanel
- {
- private int x = 0,y = 0, napr=0;
- private class myKey implements KeyListener
- {
- public void keyPressed(KeyEvent e)
- {
- int key_ = e.getKeyCode();
- if ((key_ == 39)&&(x != 450))x=x+5;
- if ((key_ == 37)&&(x != 0))x=x-5;
- if ((key_ == 38)&&(y != 0))y=y-5;
- if ((key_ == 40)&&(y != 450))y=y+5;
- }
- 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) {
- repaint();
- }
- });
- time.start();
- }
- public void paintComponent(Graphics gr)
- {
- super.paintComponent(gr);
- gr.setColor(Color.black);
- gr.fillRect(x, y, 50, 50);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement