Question
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Shape; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import javax.swing.JComponent; import javafx.scene.layout.Pane; import javafx.scene.shape.Circle; public
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Shape; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList;
import javax.swing.JComponent;
import javafx.scene.layout.Pane; import javafx.scene.shape.Circle;
public class MovePlayer extends JComponent implements KeyListener { private Rectangle rect; private Graphics g; public MovePlayer(){ addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void paintComponent(Graphics g){ this.g= g; Graphics2D g2 = (Graphics2D)g; keysDown = new ArrayList<>(); rect = new Rectangle(32,32,25,25); g2.draw(rect); g2.setColor(Color.black); g2.fill(rect); } public void dropBomb(Graphics g){ g.drawOval((int)rect.getCenterX(), (int)rect.getCenterY(), 10, 10); g.setColor(Color.RED); }
@Override public void keyPressed(KeyEvent e) { System.out.println("I am in"); if(!keysDown.contains(e.getKeyCode())){ keysDown.add(new Integer(e.getKeyCode())); } moveRect(); repaint(); } @Override public void keyReleased(KeyEvent e) { keysDown.remove(new Integer(e.getKeyCode())); } public void moveRect(){ int x = rect.x; int y = rect.y; if(keysDown.contains(KeyEvent.VK_UP)){ y -=2; } if(keysDown.contains(KeyEvent.VK_DOWN)){ y +=2; }if(keysDown.contains(KeyEvent.VK_LEFT)){ x -=2; }if(keysDown.contains(KeyEvent.VK_RIGHT)){ x +=2; }if(keysDown.contains(KeyEvent.VK_D)){ System.out.println("D button was pressed"); }if(keysDown.contains(KeyEvent.VK_S)){ System.out.println("S button was pressed"); } rect.setLocation(x, y); repaint(); } @Override public void keyTyped(KeyEvent e) { } }
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Window");
frame.setSize(new Dimension(800, 800));
frame.setLocationRelativeTo(null);
SwingUtilities.updateComponentTreeUI(frame);
frame.invalidate();
frame.validate();
frame.repaint();
MovePlayer mp = new MovePlayer();
frame.add(mp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
drawPanel class
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
private TileLayer layer;
private ArrayList
public DrawPanel(){
level = new ArrayList<>();
for(int i = 0;i<2;i++){
level.add("level"+(i+1)+".txt");
}
layer = TileLayer.FromFile(level.get(0));
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
layer.drawLayer(g);
}
}
THE QUESTION IS, WHEN THE FRAME GUI COMES OUT FIRST, IT PRINTS THE RECTANGLE BUT IT DOESN'T MOVE IT AS WE PLAY AROUND WITH THE ARROW KEYS. HOWEVER, IT PRINTS SOMETHING NEW AS WE PRESS AN ARROW KEY.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started