|
| 1 | +import java.awt.Color; |
| 2 | +import java.awt.Dimension; |
| 3 | +import java.awt.Graphics; |
| 4 | +import java.awt.event.ActionEvent; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | +import java.awt.event.KeyEvent; |
| 7 | +import java.awt.event.KeyListener; |
| 8 | +import java.awt.event.MouseEvent; |
| 9 | +import java.awt.event.MouseListener; |
| 10 | +import java.awt.event.MouseMotionListener; |
| 11 | +import java.awt.event.MouseWheelEvent; |
| 12 | +import java.awt.event.MouseWheelListener; |
| 13 | + |
| 14 | +import javax.swing.JFrame; |
| 15 | +import javax.swing.JPanel; |
| 16 | + |
| 17 | +public class Frame extends JPanel implements ActionListener, MouseListener, MouseMotionListener, MouseWheelListener, KeyListener { |
| 18 | + |
| 19 | + JFrame window; |
| 20 | + int size; |
| 21 | + double a1, a2; |
| 22 | + Node cursor = null, goal = null; |
| 23 | + String typeAttempt = ""; |
| 24 | + String mode = ""; |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + new Frame(); |
| 28 | + } |
| 29 | + |
| 30 | + public Frame() { |
| 31 | + size = 25; |
| 32 | + setLayout(null); |
| 33 | + addMouseListener(this); |
| 34 | + addMouseMotionListener(this); |
| 35 | + addMouseWheelListener(this); |
| 36 | + addKeyListener(this); |
| 37 | + setFocusable(true); |
| 38 | + setFocusTraversalKeysEnabled(false); |
| 39 | + a1 = (5000.0000 / (Math.pow(25.0000/5000, 1/49.0))); |
| 40 | + a2 = 625.0000; |
| 41 | + mode = "Normal"; |
| 42 | + window = new JFrame(); |
| 43 | + window.setContentPane(this); |
| 44 | + window.setTitle("Neovim Key Practice"); |
| 45 | + window.getContentPane().setPreferredSize(new Dimension(800, 700)); |
| 46 | + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 47 | + window.pack(); |
| 48 | + window.setLocationRelativeTo(null); |
| 49 | + window.setVisible(true); |
| 50 | + window.setResizable(false); |
| 51 | + cursor = new Node(0, 0, ""); |
| 52 | + goal = new Node((int)(Math.random()*((this.getWidth()/size)-2)+1), (int)(Math.random()*((this.getHeight()/size)-2)+1), getRandomText()); |
| 53 | + this.revalidate(); |
| 54 | + this.repaint(); |
| 55 | + } |
| 56 | + |
| 57 | + public void paintComponent(Graphics g) { |
| 58 | + super.paintComponent(g); |
| 59 | + int height = getHeight(); |
| 60 | + int width = getWidth(); |
| 61 | + g.setColor(Color.lightGray); |
| 62 | + for(int j = 0; j < this.getHeight(); j += size) { |
| 63 | + for(int i = 0; i < this.getWidth(); i += size) { |
| 64 | + g.drawRect(i, j, size, size); |
| 65 | + if(cursor.x == i/size && cursor.y == j/size) { |
| 66 | + if(mode.equalsIgnoreCase("Normal")) g.setColor(Color.black); |
| 67 | + else g.setColor(Color.green); |
| 68 | + g.fillRect(i+1, j+1, size-1, size-1); |
| 69 | + } |
| 70 | + if(goal != null && goal.x == i/size && goal.y == j/size) { |
| 71 | + g.setColor(Color.blue); |
| 72 | + g.fillRect(i+1, j+1, size-1, size-1); |
| 73 | + g.setColor(Color.white); |
| 74 | + g.drawString(goal.text, i+7, j+16); |
| 75 | + } |
| 76 | + g.setColor(Color.lightGray); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void mouseClicked(MouseEvent e) {} |
| 83 | + |
| 84 | + @Override |
| 85 | + public void mousePressed(MouseEvent e) {} |
| 86 | + |
| 87 | + @Override |
| 88 | + public void mouseReleased(MouseEvent e) {} |
| 89 | + |
| 90 | + @Override |
| 91 | + public void mouseEntered(MouseEvent e) {} |
| 92 | + |
| 93 | + @Override |
| 94 | + public void mouseExited(MouseEvent e) {} |
| 95 | + |
| 96 | + @Override |
| 97 | + public void mouseDragged(MouseEvent e) {} |
| 98 | + |
| 99 | + @Override |
| 100 | + public void mouseMoved(MouseEvent e) {} |
| 101 | + |
| 102 | + @Override |
| 103 | + public void keyTyped(KeyEvent e) {} |
| 104 | + |
| 105 | + @Override |
| 106 | + public void keyPressed(KeyEvent e) { |
| 107 | + int currentKey = e.getKeyCode(); |
| 108 | + |
| 109 | + if(mode.equalsIgnoreCase("Normal")) { |
| 110 | + if(currentKey == KeyEvent.VK_I) mode = "Insert"; |
| 111 | + else { |
| 112 | + if(currentKey == KeyEvent.VK_H) cursor.x--; |
| 113 | + else if(currentKey == KeyEvent.VK_J) cursor.y++; |
| 114 | + else if(currentKey == KeyEvent.VK_K) cursor.y--; |
| 115 | + else if(currentKey == KeyEvent.VK_L) cursor.x++; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if(mode.equalsIgnoreCase("Insert")) { |
| 120 | + if(currentKey == KeyEvent.VK_ESCAPE) mode = "Normal"; |
| 121 | + else { |
| 122 | + if(currentKey == KeyEvent.VK_LEFT) cursor.x--; |
| 123 | + if(currentKey == KeyEvent.VK_DOWN) cursor.y++; |
| 124 | + if(currentKey == KeyEvent.VK_UP) cursor.y--; |
| 125 | + if(currentKey == KeyEvent.VK_RIGHT) cursor.x++; |
| 126 | + else { |
| 127 | + char currentChar = e.getKeyChar(); |
| 128 | + if(typeAttempt.length() == 1 && currentChar == goal.text.charAt(1)) typeAttempt += currentChar; |
| 129 | + else typeAttempt = ""; |
| 130 | + if(typeAttempt.length() == 0 && currentChar == goal.text.charAt(0)) typeAttempt += currentChar; |
| 131 | + } |
| 132 | + |
| 133 | + if(typeAttempt.length() == 2) { |
| 134 | + goal = new Node((int)(Math.random()*((this.getWidth()/size))), (int)(Math.random()*((this.getHeight()/size))), getRandomText()); |
| 135 | + typeAttempt = ""; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if(cursor.x < 0) cursor.x = 0; |
| 141 | + if(cursor.x > this.getWidth()/size) cursor.x = this.getWidth()/size; |
| 142 | + if(cursor.y < 0) cursor.y = 0; |
| 143 | + if(cursor.y > this.getWidth()/size) cursor.y = this.getHeight()/size; |
| 144 | + |
| 145 | + repaint(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void keyReleased(KeyEvent e) {} |
| 150 | + |
| 151 | + // Disabled this feature fornow |
| 152 | + @Override |
| 153 | + public void mouseWheelMoved(MouseWheelEvent m) {} |
| 154 | + |
| 155 | + @Override |
| 156 | + public void actionPerformed(ActionEvent e) {} |
| 157 | + |
| 158 | + int randomWithRange(int min, int max) { |
| 159 | + int range = (max - min) + 1; |
| 160 | + return (int)(Math.random() * range) + min; |
| 161 | + } |
| 162 | + |
| 163 | + String getRandomText() { |
| 164 | + String alphabet = "abcdefghijklmnopqrstuvwxyz"; |
| 165 | + char f = alphabet.charAt((int)(Math.random()*alphabet.length())), s = alphabet.charAt((int)(Math.random()*alphabet.length())); |
| 166 | + return f + "" + s; |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments