使用Java中的Graphics绘制多个图像

时间:2014-08-25 18:57:05

标签: java swing graphics

我想创建一个测试项目来改进我的Java图形。我的目标是做一个简单的Tic Tac Toe游戏,玩家可以使用Numpad输入他们想要下一步的动作。

我现在正在做的是我正在绘制Tic Tac Toe的框架/布局背景。 当我尝试在背景/布局上绘制X或O时,它不会显示出来。我还尝试在背景旁边画出X和O,但它只是不起作用。

我应该更新它吗?如果是,我该如何更新它或者是不可能的。我无法弄清楚。

我有4个单独的课程。

第一堂课是Frame。 第二背景。 第三个InputHandler。 第四轮。

以下是代码:

package Game;

import java.awt.GridLayout;
import javax.swing.JFrame;

public class Frame extends JFrame {

    private Background bg;
    private Turn trn;
    private InputHandler in;

    public Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setResizable(false);
        setTitle("Hi");
        init();
    }

    public void init() {
        setLocationRelativeTo(null);
        setLayout(new GridLayout(1, 1, 0, 0));
        bg = new Background();
        add(bg);
        trn = new Turn();
        add(trn);
        setVisible(true);

        in = new InputHandler();
        addKeyListener(in);
    }

    public static void main(String[] args) {
        new Frame();
    }
}

背景

package Game;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Background extends JPanel {

    private BufferedImage image;

    public Background() {
        try {
            image = ImageIO.read(getClass().getResourceAsStream("/tiles/tile_TicTacToeGrid.png"));
        } catch (Exception e) {
            System.out.println("File not found.\n\n" + e);
        }
        repaint();
    }

    public void paint(Graphics g) {
        // Drawing to screen
        g.drawImage(image, 0, 0, null);
    }
}

InputHandler

package Game;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class InputHandler implements KeyListener {

    private Turn turn;


    public InputHandler() {
        turn = new Turn();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        //
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int k = e.getKeyCode();

        if (k == KeyEvent.VK_NUMPAD1) {
            turn.play();            
        }
        if (k == KeyEvent.VK_NUMPAD2) {
            turn.play();
        }
        if (k == KeyEvent.VK_NUMPAD3) {
            turn.play();
        }
        if (k == KeyEvent.VK_NUMPAD4) {
            turn.play();
        }
        if (k == KeyEvent.VK_NUMPAD5) {
            turn.play();
        }
        if (k == KeyEvent.VK_NUMPAD6) {
            turn.play();
        }
        if (k == KeyEvent.VK_NUMPAD7) {
            turn.play();
        }
        if (k == KeyEvent.VK_NUMPAD8) {
            turn.play();
        }
        if (k == KeyEvent.VK_NUMPAD9) {
            turn.play();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //
    }
}

打开

package Game;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import tiles.Draw;

public class Turn extends JPanel{

    public char nextTurn = 'O';
    public BufferedImage image;
    private Draw draw;

    public Turn() {
    }

    public void pTurn() {
        if (nextTurn == 'O') {
            nextTurn = 'X';
        } else if (nextTurn == 'X') {
            nextTurn = 'O';
         }
    }

    public void play() {
        try {
            image = ImageIO.read(getClass().getResourceAsStream("/tiles/tile_X.png"));
            System.out.println("X Found");
            pTurn();
            repaint();

        } catch (Exception e) {
            System.out.println("File not found\n" + e);
        }
    }

    public void paint(Graphics2D g) {
        g.drawImage(image, 200, 200, null);
    }
}

0 个答案:

没有答案