1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
Public class Player{
// engine flag
boolean run;
// swing components
JViewport view;
JTextArea txtarea;
JSlider speed;
JSlider progress;
// listeners
KeyAdapter key;
MouseAdapter mouse;
public Player(String text) {
// engine setup
run = false;
// listeners setup
key = new KeyAdapter() {
public void keyReleased(KeyEvent e) {
// press ESC to quit the program
if (e.getKeyCode() == 27) System.exit(0);
if (e.getKeyChar() == 'r') {
progress.setValue(0);
view.setViewPosition(new Point(0, 0));
}
if (e.getKeyChar() == 'p') {
if (run) run = false;
else {
run = true;
timerFactory().start();
}
}
}
};
mouse = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
switch (e.getButton()) {
case MouseEvent.BUTTON1:
if (run) run = false;
else {
run = true;
timerFactory().start();
}
break;
case MouseEvent.BUTTON3:
progress.setValue(0);
view.setViewPosition(new Point(0, 0));
break;
}
}
};
JFrame window = new JFrame("Player");
Container tank = window.getContentPane();
tank.setLayout(new BorderLayout());
txtarea = new JTextArea(text);
txtarea.addKeyListener(key);
txtarea.addMouseListener(mouse);
txtarea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18)); // Font
txtarea.setForeground(Color.GREEN); // Font color
txtarea.setBackground(Color.BLACK); // Background color
txtarea.setEditable(false);
view = new JViewport();
view.setView(txtarea);
tank.add(view, BorderLayout.CENTER);
speed = new JSlider(SwingConstants.VERTICAL, 0, 90, 50);
speed.addKeyListener(key);
speed.setBackground(Color.BLACK);
tank.add(speed, BorderLayout.WEST);
progress = new JSlider(SwingConstants.HORIZONTAL, 0, 1000, 0);
progress.addKeyListener(key);
progress.setBackground(Color.BLACK);
tank.add(progress, BorderLayout.SOUTH);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);
// window.pack();
window.setVisible(true);
}
private Thread timerFactory() {
return new Thread() {
public void run() {
System.out.println("run");
while (progress.getValue() < 1000 && run) {
progress.setValue(progress.getValue() + 1);
view.setViewPosition(new Point(0,
(int) ((float) progress.getValue() / 1000f * (txtarea.getHeight() - view.getHeight()))));
try {
Thread.sleep(100 - speed.getValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
run = false;
}
};
}
} |
Partager