Advertisement
pilgon-sesh60

graphics

Nov 13th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5.  
  6. public class Gui extends JFrame implements KeyListener {
  7.     public Gui() {
  8.         setTitle("My title");
  9.         setSize(300, 300);
  10.         setLocationRelativeTo(null);
  11.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  12.        
  13.         Board board = new Board();
  14.         add(board);
  15.          
  16.         addKeyListener(this);
  17.     }
  18.    
  19.     private String american2dvorak(String american) {
  20.         Map<String, String> keymap = new HashMap<String, String>();
  21.         keymap.put("r", "p");
  22.         keymap.put("t", "y");
  23.         keymap.put("y", "f");
  24.         keymap.put("u", "g");
  25.         keymap.put("i", "c");
  26.         keymap.put("o", "r");
  27.         keymap.put("p", "l");
  28.         keymap.put("s", "o");
  29.         keymap.put("d", "e");
  30.         keymap.put("f", "u");
  31.         if(keymap.get(american) == null) {
  32.             return american;
  33.         }
  34.         else {
  35.           return keymap.get(american);
  36.         }
  37.     }
  38.    
  39.  
  40.     public void keyPressed(KeyEvent e) {
  41.     }
  42.    
  43.     public void keyReleased(KeyEvent e) {
  44.     }
  45.    
  46.     public void keyTyped(KeyEvent e) {
  47.         System.out.println(american2dvorak(Character.toString(e.getKeyChar())));
  48.     }
  49.    
  50.     public static void main(String[] args) {
  51.         Gui mygui = new Gui();
  52.         mygui.setVisible(true);
  53.     }
  54.    
  55.     private class Board extends JPanel implements ActionListener {
  56.         public int y;
  57.        
  58.         public Board() {
  59.             y = 150;
  60.             javax.swing.Timer t = new javax.swing.Timer(100, this);
  61.             t.start();
  62.         }
  63.        
  64.         @Override
  65.         public void paintComponent(Graphics g) {
  66.           super.paintComponent(g);
  67.           String msg = "Game Over";
  68.           Font small = new Font("Helvetica", Font.BOLD, 14);
  69.           FontMetrics metr = getFontMetrics(small);
  70.  
  71.           if(y > 200) {
  72.               g.setColor(Color.blue);
  73.           }
  74.           else {
  75.               g.setColor(Color.red);
  76.           }
  77.           g.setFont(small);
  78.           g.drawString(msg, (300 - metr.stringWidth(msg)) / 2, y);
  79.         }
  80.        
  81.         public void actionPerformed(ActionEvent e) {
  82.             y++;
  83.             repaint();
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement