Nova的科學反主流學院 

反主流的精神在於不屈於大環境, 本站旨在提供輕鬆自學各種科學。

JAVA 2D繪圖範例架構+滑鼠鍵盤事件

 

採用Components而不用JPanel或Canvas 是因為內建有雙緩衝,更新畫面不會閃

 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JFrame;


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

    public TestPaint() {

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        
        GamePanel gp = new GamePanel(500,500,10);
        
        frame.getContentPane().add(gp);
        frame.pack();
        frame.setSize(500, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class GamePanel extends JComponent implements MouseListener, KeyListener
{
    private static final long serialVersionUID = 1L;
    
    public GamePanel() { }
    
    int ballX = 0;
    int ballY = 0;
    
    public GamePanel(int x, int y, int diameter)
    {
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
        this.addKeyListener(this);
        this.addMouseListener(this);
        this.setFocusable(true);
        this.requestFocusInWindow();
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.clearRect(0, 0, 100, 100);
        g.setColor(Color.red);
        g.fillOval(ballX, ballY, 100, 100);
        repaint();
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        int keyCode = arg0.getKeyCode();
        switch( keyCode ) { 
            case KeyEvent.VK_UP:
                // handle up 
                break;
            case KeyEvent.VK_DOWN:
                // handle down 
                break;
            case KeyEvent.VK_LEFT:
                ballX = ballX - 5;
                System.out.println("Left");
                break;
            case KeyEvent.VK_RIGHT :
                ballX = ballX + 5;
                System.out.println("Right");
                break;
         }
        
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
}