Advertisement
Guest User

Untitled

a guest
May 29th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. package projekt2;
  2.  
  3. import java.awt.*;
  4.  
  5. import javax.swing.JPanel;
  6.  
  7. import java.awt.event.*;
  8.  
  9. import javax.swing.*;
  10.  
  11. import java.awt.Graphics;
  12.  
  13.  
  14. public class MovingBar extends JFrame implements KeyListener{
  15.  
  16. private int recx;
  17. private int recy;
  18. private int recw;
  19. private int rech;
  20. private int frameWidth = 400;
  21. private int frameHeight = 300;
  22.  
  23. private boolean movingL;
  24. private boolean movingR;
  25.  
  26. JPanel mp = new JPanel();
  27.  
  28. public MovingBar()
  29. {
  30. this.setTitle("SpaceSpace");
  31. this.setSize(frameWidth,frameHeight);
  32. this.recw = 100;
  33. this.rech = 10;
  34. this.recx = getWidth() / 2 - this.rech / 2;
  35. this.recy = getHeight() - this.rech - 10;
  36. this.getContentPane().setLayout(new BorderLayout());
  37. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.  
  39. mp.addKeyListener(this);
  40. this.add(mp);
  41. mp.setFocusable(true);
  42. mp.setBackground(Color.BLACK);
  43. this.movingL = false;
  44. this.movingR = false;
  45. }
  46.  
  47. @Override
  48. public void paint(Graphics g)
  49. {
  50.  
  51. super.paint(g);
  52. g.setColor(Color.WHITE);
  53. g.fillRect(this.recx, this.recy, this.recw, this.rech);
  54. repaint();
  55. }
  56.  
  57. public void moveLeft()
  58. {
  59. if (this.recx >= 0)
  60. this.recx--;
  61. }
  62.  
  63. public void moveRight()
  64. {
  65. if (this.recx < this.frameWidth - this.recw)
  66. this.recx++;
  67. }
  68.  
  69. @Override
  70. public void keyPressed (KeyEvent e)
  71. {
  72. if (e.getKeyCode() == KeyEvent.VK_LEFT)
  73. {
  74. this.movingL = true;
  75. this.movingR = false;
  76. }
  77.  
  78. if (e.getKeyCode() == KeyEvent.VK_RIGHT)
  79. {
  80. this.movingR = true;
  81. this.movingL = false;
  82. }
  83. }
  84.  
  85. @Override
  86. public void keyReleased (KeyEvent e)
  87. {
  88. if (e.getKeyCode() == KeyEvent.VK_LEFT)
  89.  
  90. this.movingL = false;
  91.  
  92. if (e.getKeyCode() == KeyEvent.VK_RIGHT)
  93.  
  94. this.movingR = false;
  95. }
  96.  
  97. @Override
  98. public void keyTyped(KeyEvent e)
  99. {
  100.  
  101. }
  102.  
  103. public void actionHandler()
  104. {
  105. if (this.movingL == true )
  106. moveLeft();
  107.  
  108. if (this.movingR == true)
  109. moveRight();
  110. }
  111.  
  112. }
  113.  
  114. //Main
  115.  
  116. package projekt2;
  117.  
  118. import java.awt.Color;
  119.  
  120. public class Main {
  121.  
  122. public static void main(String[] args) {
  123. // TODO Auto-generated method stub
  124. MovingBar mb = new MovingBar();
  125. mb.setResizable(false);
  126. mb.setVisible(true);
  127. mb.actionHandler();
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement