简体   繁体   中英

How to make characters appear on canvas

Copy and modify your canvas to implement the KeyListener interface, add itself as its own KeyListener , and call setFocusable(true) in order to receive keyboard focus. When a key is typed, your program should draw the corresponding character (see KeyEvent.getKeyChar() ) on the canvas at the location of the last mouse event. If another key is typed without the mouse being clicked, then draw the next character to the right of the previous one as if you were typing in a text field. Again, think about what state you need to maintain to do this. It doesn't have to be perfect (you can hard-code the width of the characters if you like).

  import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JComponent;
    import javax.swing.JFrame;


    public class Canvas05 extends JComponent implements KeyListener {


          public Canvas05() {

              addKeyListener(this);
              setFocusable(true);

            }

        @Override
        public void keyTyped(KeyEvent e) {

            System.out.print(e.getKeyChar());
        }

        @Override
        public void keyPressed(KeyEvent e) {


        }

        @Override
        public void keyReleased(KeyEvent e) {


        }

        public static void main (String[] args){

            Canvas05 c = new Canvas05();

            JFrame frame = new JFrame("Q05");
            frame.add(c);

            frame.setSize(400, 400);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setVisible(true);
        }

    }

Here you go. The location from mouseEvent is very easily acessable from the MouseListener event where you can declare both an x and ay variable from

MouseListener.getX() and MouseListener.getY();

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel{

public static int WIDTH = 500,HEIGHT = 500;
public static char myChar = ' ';
public static Game game = new Game();
public static JFrame frame = new JFrame();

public Game() {
    this.addKeyListener(new KeyListener(){

        public void keyPressed(KeyEvent e) {
            myChar = (char)e.getKeyCode();
            repaint();

        }

        @Override
        public void keyReleased(KeyEvent arg0) {

        }

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

        }

    });

    setFocusable(true);
}

public void paint(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    g.clearRect(0, 0, WIDTH, HEIGHT);

    g.setColor(Color.red);
    g.setFont(new Font("Cambria",Font.BOLD,30));
    g.drawString(String.valueOf(myChar),100, 100);

}

public static void main(String [] args){

    frame.add(game);
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM