I wrote a falling blocks game as a Java Applet many years ago. Java Applets don’t work any more. I found this article here https://examples.javacodegeeks.com/desktop-java/swing/java-swing-application-example/ and with a few simple tweaks I can now run it as a Swing application. Here In take you through the steps in which I converted the Java Applet to a Java Swing Application.
Add the Swing libraries to the import. From:
import java.applet.Applet;
To:
import javax.swing.*;
Extend a JPanel not an Applet. From:
public class KevTris extends Applet implements KeyListener {
To:
public class KevTris extends JPanel implements KeyListener {
You’ll need a main method and a createAndShowGUI method. Hooking into your existing code as below. Add:
private static void createAndShowGUI() {
//Make sure we have nice window decorations.JFrame.setDefaultLookAndFeelDecorated(true);//Create and set up the window.JFrame frame = new JFrame("HelloWorldSwing");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Add Kevtris classJPanel panel = new KevTris();//Run init() to do something((KevTris)panel).init();frame.getContentPane().add(panel);//Display the window.frame.pack();frame.setVisible(true);}
public static void main(String[] argv) {
//Schedule a job for the event-dispatching thread://creating and showing this application's GUI.javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run() {createAndShowGUI();}});}
Comment out contents of keyPressed:
public void keyPressed(KeyEvent e) {
// BlockAbstract tempBlock = (BlockAbstract) blockStack.peek();//// if(e.getKeyCode() == KeyEvent.VK_LEFT) {// tempBlock.moveLeft(1);// debugMessages = "went left";// repaint((long) 0);// } else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {// tempBlock.moveRight(1);// debugMessages = "went right";// repaint((long) 0);// } else if(e.getKeyCode() == KeyEvent.VK_DOWN) {// tempBlock.moveDown(1);// debugMessages = "went down";// repaint((long) 0);// } else if(e.getKeyCode() == KeyEvent.VK_UP) {// tempBlock.rotateRight();// debugMessages = "rotated right";// repaint((long) 0);// } else {// debugMessages = "didn't recognise: " + e.getKeyCode();// repaint((long) 0);// }}
For detecting keys pressed; add your above code to init() method as following:
InputMap im = getInputMap(WHEN_FOCUSED);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "onUp2");
am.put("onUp2", new AbstractAction() {
@Overridepublic void actionPerformed(ActionEvent e) {BlockAbstract tempBlock = (BlockAbstract) blockStack.peek();tempBlock.rotateRight();debugMessages = "rotated right";repaint((long) 0);}});
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "onLeft");
am.put("onLeft", new AbstractAction() {
@Overridepublic void actionPerformed(ActionEvent e) {BlockAbstract tempBlock = (BlockAbstract) blockStack.peek();tempBlock.moveLeft(1);debugMessages = "went left";repaint((long) 0);}});
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "onRight");
am.put("onRight", new AbstractAction() {
@Overridepublic void actionPerformed(ActionEvent e) {BlockAbstract tempBlock = (BlockAbstract) blockStack.peek();tempBlock.moveRight(1);debugMessages = "went right";repaint((long) 0);}});
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "onDown");
am.put("onDown", new AbstractAction() {
@Overridepublic void actionPerformed(ActionEvent e) {BlockAbstract tempBlock = (BlockAbstract) blockStack.peek();tempBlock.moveDown(1);debugMessages = "went down";repaint((long) 0);}});
See a short video here: https://twitter.com/digitaltechlabs/status/1545564299321565185?s=20&t=ARb997sRap4KYK3OAJQWtQ
Recent Comments