Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. package blog.gui;
  2.  
  3. import java.awt.Cursor;
  4. import java.awt.EventQueue;
  5. import java.awt.Image;
  6. import java.awt.Toolkit;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9.  
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15.  
  16. public class Keyboard1 extends JFrame{
  17.  
  18. static final long serialVersionUID = 4982848;
  19.  
  20. private ImageIcon human = new ImageIcon("human.jpg");
  21.  
  22. private final int STEP_NUM = 20;
  23.  
  24. private JLabel background = new JLabel(new ImageIcon("background.jpg"));
  25. private JButton buttonHuman = new JButton();
  26.  
  27.  
  28. public Keyboard1() {
  29.  
  30. super("키보드 이동");
  31.  
  32. this.setSize(1280, 720);
  33. this.setLocationRelativeTo(null);
  34. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  35.  
  36. this.setLayout(null);
  37. this.setResizable(false);
  38.  
  39. background.setSize(1280, 720);
  40.  
  41. Toolkit tk = Toolkit.getDefaultToolkit();
  42. super.setIconImage(tk.getImage("human.jpg"));
  43.  
  44. this.start();
  45.  
  46. this.setVisible(true);
  47.  
  48. }
  49.  
  50. public void start() {
  51.  
  52. this.buttonHuman.setBounds(520, 360, 100, 100);
  53.  
  54. // 이미지 추출 후 사이즈 변환
  55. Image img = human.getImage()
  56. .getScaledInstance(100, 100, Image.SCALE_SMOOTH);
  57. // 변환된 이미지를 다시 아이콘으로 치환
  58. human = new ImageIcon(img);
  59.  
  60. buttonHuman.setIcon(human);
  61. buttonHuman.setContentAreaFilled(false); // 버튼 색상 제거
  62. buttonHuman.setBorderPainted(false); // 경계선 제거
  63. buttonHuman.setCursor(new Cursor(Cursor.HAND_CURSOR));
  64.  
  65. this.add(buttonHuman);
  66. this.add(background);
  67.  
  68. this.buttonHuman.addKeyListener(new KeyListener() {
  69.  
  70. @Override // 키보드 방향키 이벤트 부여
  71. public void keyPressed(KeyEvent e) {
  72. int key = e.getKeyCode();
  73.  
  74. if (key == KeyEvent.VK_LEFT) {
  75. move(buttonHuman, -STEP_NUM, 0);
  76. }
  77.  
  78. if (key == KeyEvent.VK_RIGHT) {
  79. move(buttonHuman, STEP_NUM, 0);
  80. }
  81.  
  82. if (key == KeyEvent.VK_UP) {
  83. move(buttonHuman, 0, -STEP_NUM);
  84. }
  85.  
  86. if (key == KeyEvent.VK_DOWN) {
  87. move(buttonHuman, 0, STEP_NUM);
  88. }
  89.  
  90. }
  91.  
  92. @Override
  93. public void keyReleased(KeyEvent e) {
  94.  
  95. }
  96.  
  97. @Override
  98. public void keyTyped(KeyEvent e) {
  99.  
  100. }
  101. });
  102.  
  103.  
  104. }
  105.  
  106. private void move(JButton buttonHuman, int x, int y) {
  107. int currentX = buttonHuman.getX();
  108. int currentY = buttonHuman.getY();
  109. JPanel parentPanel = null;
  110.  
  111. if(buttonHuman.getParent() instanceof JPanel) {
  112. parentPanel = (JPanel) buttonHuman.getParent();
  113. }
  114.  
  115. // 부모 패널의 가로세로, 버튼의 가로세로 길이
  116. int parentWidth = parentPanel.getWidth();
  117. int parentHeight = parentPanel.getHeight();
  118. int buttonWidth = buttonHuman.getWidth();
  119. int buttonHeight = buttonHuman.getHeight();
  120.  
  121. System.out.println(parentWidth + ", " + parentHeight
  122. + " : " + currentX + ", " + currentY
  123. + " : " + (currentX + buttonWidth) + ", " + (currentY + buttonHeight));
  124.  
  125. // 현재 X의 좌표가 마이너스인데 x 입력값도 마이너스이거나,
  126. // 현재 (X 좌표 + 버튼 가로 길이)의 값이 부모 패널의 길이보다 크면 더 이상 움직이지 않는다.
  127. if(currentX <= 0 + -x / 4 && x < 0 || currentY <= 0 + -y / 4 && y < 0
  128. || (currentX + buttonWidth + x / 4) >= parentWidth && x > 0
  129. || (currentY + buttonHeight + y / 4) >= parentHeight && y > 0) {
  130. return;
  131.  
  132. } else {
  133. // Jdk 8 이상
  134. new Thread(() -> {
  135.  
  136. int ix = x > 0 ? 1 : -1;
  137. int iy = y > 0 ? 1 : -1;
  138. while(true) {
  139.  
  140. int xChunk = 0; // 움직일 값 (1 또는 -1)
  141. int yChunk = 0;
  142. boolean condition = false;
  143.  
  144. if(x != 0 && y == 0) {
  145. xChunk = (ix > 0 ? ix++ : ix--);
  146. condition = (ix == x);
  147. } else if(x == 0 && y != 0) {
  148. yChunk = (iy > 0 ? iy++ : iy--);
  149. condition = (iy == y);
  150. }
  151.  
  152. buttonHuman.setLocation(currentX + xChunk, currentY + yChunk);
  153.  
  154. // 컨디션을 만족하면 종료한다.
  155. if(condition) {
  156. break;
  157. }
  158.  
  159. // 최소 1ms 이상을 슬립해 이동 모션을 부드럽게 한다.
  160. try {
  161. Thread.sleep(2, 500);
  162. } catch (InterruptedException e) {
  163. e.printStackTrace();
  164. }
  165. }
  166.  
  167.  
  168. } ).start();
  169.  
  170. }
  171. }
  172.  
  173. public static void main(String[] args) {
  174.  
  175. EventQueue.invokeLater(new Runnable () {
  176. public void run()
  177. {
  178. new Keyboard1();
  179. }
  180. });
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement