Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package turnbasedgameimproved;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import javax.swing.*;
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import javax.imageio.ImageIO;
- import java.io.IOException;
- import java.io.File;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.ActionEvent;
- import java.awt.event.KeyEvent;
- import javax.swing.AbstractAction;
- import javax.swing.Action;
- import javax.swing.ActionMap;
- import javax.swing.InputMap;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.KeyStroke;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- /**
- *
- * @author jason
- */
- public class testingPanel extends JPanel{
- private int squareX = 60;
- private int squareY = 60;
- private int squareW = 30;
- private int squareH = 30;
- private BufferedImage image;
- private int x = 100;
- private int y = 100;
- // private boolean focus = true;
- public testingPanel(){
- //creates an action
- Action leftAction = new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // if (focus){
- // x-=50;
- // }
- x-=50;
- repaint();
- }
- };
- //creates an action
- Action rightAction = new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- /* so, i can use this panel as the game panel
- , have the image be the static
- for healthbars, i can figure that out from google
- for decision making i can make it pass the units down into this panel to play on?
- use a bunch of booleans to decide what kind of decision making is going to happen
- such as
- press z to confirm (change the booleans, set the cursor somewhere, do stuff
- but the problem is how to choose target? (since target can be ally or enemy) (want to avoid the massive block like before.)
- solutions?
- add a panel to this panel, that takes controll of key listener to help make decisions?
- make all the action buttons using the key listener in the main function
- that way it has all the information, its always being read anyways, and can control whats going on
- use the panel just for animation
- if i want to make a unit attack lets say
- 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
- 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
- is doing, while the other is controlling what the game displays
- the function will animate/put the displace for the game
- use gamePanel to display how to select unit types
- and switch over to this panel once the selection is done
- in the units pages, maybe add an image to them so its easier to pass along?
- */
- // if (focus){
- // x+=50;
- // }
- x+=50;
- //redraws
- repaint();
- }
- };
- //tells what keystroke does what
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), leftAction);
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0), leftAction);
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_4, 0), leftAction);
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), leftAction);
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), rightAction);
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0), rightAction);
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_6, 0), rightAction);
- bindKeyStroke(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), rightAction);
- try {
- image = ImageIO.read(new File("C:\\Users\\jason\\Documents\\NetBeansProjects\\TurnBasedGameImproved\\temporary.png")); //this is how to add images
- } catch (IOException ex) {
- System.out.println ("hi"); // handle exception...
- }
- }
- private void bindKeyStroke(int condition, String name, KeyStroke keyStroke, Action action) {
- //something to check if in focused window?
- InputMap im = getInputMap(condition);
- //something to do with the library?
- ActionMap am = getActionMap();
- im.put(keyStroke, name);
- //takes the action map(the panel class?), and calls the action?
- am.put(name, action);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g); // do your superclass's painting routine first, and then paint on top of it.
- // g.setColor(Color.BLUE);
- // g.fillRect(squareX,squareY,squareW,squareH);
- int width = getWidth() - 20;
- int height = getHeight() - 20;
- g.drawArc(x, y, width, height, 0, 360);
- g.fillRect(squareX,squareY,squareW,squareH);
- g.drawImage(image, 0, 0, this);
- g.fillRect(x, y, 50, 50);
- }
- public void run() {
- while(true){
- //x += 1;
- repaint();
- try {
- Thread.sleep(17);
- } catch (InterruptedException e) {
- System.out.println("Thread generates an error.");
- }
- }
- }
- // public void focus(){
- // focus = true;
- // }
- // public void unfocus(){
- // focus = false;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement