0 Members and 3 Guests are viewing this topic.
import javax.swing.*;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.KeyListener;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;public class Squared { JFrame mainFrame = new JFrame("Squared"); public int x = 5; public int y = 5; public boolean canRunConstructor = true; public static void main (String[] args) { Squared mySquared = new Squared(); mySquared.Draw(); } public Squared() { // TODO Constructors } public void Draw() { canRunConstructor = false; mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setSize(480,320); mainFrame.getContentPane().setLayout(null); KeyEvents keyEvents = new KeyEvents(); mainFrame.addKeyListener(keyEvents); BufferedImage img = null; try { img = ImageIO.read(new File("bin/block.png")); } catch (IOException e) { e.printStackTrace(); } JLabel picLabel = new JLabel(new ImageIcon( img )); picLabel.setBounds(x,y,40,40); mainFrame.add(picLabel); mainFrame.setVisible(true); }}
import java.awt.event.KeyEvent;import java.awt.event.KeyListener;public class KeyEvents implements KeyListener { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 39) //Right key { Squared mySquared = new Squared(); mySquared.x = mySquared.x + 5; mySquared.mainFrame.repaint(); } System.out.println(e.getKeyCode()); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub }}
you'll want to have a class which extends JPanel, implements KeyListener, and overrides the method public void paintComponent(Graphics g)
public class MyClass extends JPanel implements KeyListener { @Override public void paintComponent(Graphics g){ //your code here }}
import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;public class Squared { public static void main(String[] args) { Squared mySquared = new Squared(); mySquared.createAndShowGUI(); } public void createAndShowGUI() { JFrame f = new JFrame("Squared"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(480,320); f.setResizable(false); f.add(new GamePanel()); KeyEvents myKeyEvents = new KeyEvents(); f.addKeyListener(myKeyEvents); f.setVisible(true); }}class GamePanel extends JPanel { int x = 5; int y = 5; public GamePanel() { } public Dimension getPreferredSize() { return new Dimension(250,200); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.black); g.fillRect(x,y,40,40); } }
import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JPanel;import java.awt.Graphics;public class KeyEvents extends JPanel implements KeyListener { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 39) //Right key { GamePanel myGamePanel = new GamePanel(); myGamePanel.x += 5; //So now how to update graphics? } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub }}
import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JPanel;import java.awt.Graphics;public class KeyEvents extends JPanel implements KeyListener { GamePanel myGamePanel = new GamePanel(); @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 39) //Right key { myGamePanel.x += 5; } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub }}
frame.setDefaultCloseOperation(-3);