Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package turnbasedgameimproved;
  7.  
  8. import java.awt.Dimension;
  9. import java.awt.Graphics;
  10. import javax.swing.*;
  11. import java.awt.Color;
  12. import java.awt.image.BufferedImage;
  13. import javax.imageio.ImageIO;
  14. import java.io.IOException;
  15. import java.io.File;
  16. import java.awt.Dimension;
  17. import java.awt.EventQueue;
  18. import java.awt.Graphics;
  19. import java.awt.Graphics2D;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.KeyEvent;
  22. import javax.swing.AbstractAction;
  23. import javax.swing.Action;
  24. import javax.swing.ActionMap;
  25. import javax.swing.InputMap;
  26. import javax.swing.JFrame;
  27. import javax.swing.JPanel;
  28. import javax.swing.KeyStroke;
  29. import javax.swing.UIManager;
  30. import javax.swing.UnsupportedLookAndFeelException;
  31. /**
  32. *
  33. * @author jason
  34. */
  35. public class testingPanel extends JPanel{
  36. private int squareX = 60;
  37. private int squareY = 60;
  38. private int squareW = 30;
  39. private int squareH = 30;
  40. private BufferedImage image;
  41. private int x = 100;
  42. private int y = 100;
  43. // private boolean focus = true;
  44.  
  45. public testingPanel(){
  46. //creates an action
  47. Action leftAction = new AbstractAction() {
  48. @Override
  49. public void actionPerformed(ActionEvent e) {
  50. // if (focus){
  51. // x-=50;
  52. // }
  53. x-=50;
  54. repaint();
  55. }
  56. };
  57. //creates an action
  58. Action rightAction = new AbstractAction() {
  59. @Override
  60. public void actionPerformed(ActionEvent e) {
  61. /* so, i can use this panel as the game panel
  62. , have the image be the static
  63. for healthbars, i can figure that out from google
  64. for decision making i can make it pass the units down into this panel to play on?
  65. use a bunch of booleans to decide what kind of decision making is going to happen
  66. such as
  67. press z to confirm (change the booleans, set the cursor somewhere, do stuff
  68. but the problem is how to choose target? (since target can be ally or enemy) (want to avoid the massive block like before.)
  69. solutions?
  70. add a panel to this panel, that takes controll of key listener to help make decisions?
  71. make all the action buttons using the key listener in the main function
  72. that way it has all the information, its always being read anyways, and can control whats going on
  73. use the panel just for animation
  74. if i want to make a unit attack lets say
  75. have a function i can call after a sequence of presses from the main function. the main function will have ALOT of booleans to ensure the right things are displaying
  76. this panel will also have booleans to coresspond with the main. while they both may be following the same instructions, they are in fact. but one is to controll what the game
  77. is doing, while the other is controlling what the game displays
  78. the function will animate/put the displace for the game
  79.  
  80. use gamePanel to display how to select unit types
  81. and switch over to this panel once the selection is done
  82. in the units pages, maybe add an image to them so its easier to pass along?
  83.  
  84.  
  85.  
  86.  
  87.  
  88. */
  89. // if (focus){
  90. // x+=50;
  91. // }
  92. x+=50;
  93. //redraws
  94. repaint();
  95. }
  96. };
  97. //tells what keystroke does what
  98. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), leftAction);
  99. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0), leftAction);
  100. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_4, 0), leftAction);
  101. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), leftAction);
  102. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), rightAction);
  103. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0), rightAction);
  104. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_6, 0), rightAction);
  105. bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), rightAction);
  106.  
  107. try {
  108. image = ImageIO.read(new File("C:\\Users\\jason\\Documents\\NetBeansProjects\\TurnBasedGameImproved\\temporary.png")); //this is how to add images
  109. } catch (IOException ex) {
  110. System.out.println ("hi"); // handle exception...
  111. }
  112. }
  113. private void bindKeyStroke(int condition, String name, KeyStroke keyStroke, Action action) {
  114. //something to check if in focused window?
  115. InputMap im = getInputMap(condition);
  116. //something to do with the library?
  117. ActionMap am = getActionMap();
  118.  
  119. im.put(keyStroke, name);
  120. //takes the action map(the panel class?), and calls the action?
  121. am.put(name, action);
  122. }
  123. @Override
  124. protected void paintComponent(Graphics g) {
  125. super.paintComponent(g); // do your superclass's painting routine first, and then paint on top of it.
  126. // g.setColor(Color.BLUE);
  127. // g.fillRect(squareX,squareY,squareW,squareH);
  128.  
  129. int width = getWidth() - 20;
  130. int height = getHeight() - 20;
  131. g.drawArc(x, y, width, height, 0, 360);
  132. g.fillRect(squareX,squareY,squareW,squareH);
  133. g.drawImage(image, 0, 0, this);
  134. g.fillRect(x, y, 50, 50);
  135. }
  136. public void run() {
  137. while(true){
  138.  
  139. //x += 1;
  140. repaint();
  141. try {
  142. Thread.sleep(17);
  143. } catch (InterruptedException e) {
  144. System.out.println("Thread generates an error.");
  145. }
  146. }
  147.  
  148. }
  149. // public void focus(){
  150. // focus = true;
  151. // }
  152. // public void unfocus(){
  153. // focus = false;
  154. // }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement