0
I started programming a little game where an object moves across the screen via the arrow keys on the keyboard. The difficulty I found is the fact that the object surpasses the JFrame
, that is, there is nothing to block the passage at the border of JFrame
.
I would like to know how I can solve this problem, or at least an indication of a path I should take to achieve success in this goal.
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Jogo1 extends JFrame{ //Herda para usar JFrame
String n = "0";
JLabel labelFlappy = new JLabel(n);
int posPrincX = 300;
int posPrincY = 300;
public Jogo1(){
editarJanela();
editarComponentes();
addMovimento();
}
public void addMovimento(){
addKeyListener(new KeyListener(){ //Teclado
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode()); //Exibe código da tecla pressionada
if(e.getKeyCode() == 38){
posPrincY -= 20;
}
if(e.getKeyCode() == 40){
posPrincY += 20;
}
if(e.getKeyCode() == 37){
posPrincX -= 20;
}
if(e.getKeyCode() == 39){
posPrincX += 20;
}
labelFlappy.setBounds(posPrincX, posPrincY, 180, 90); //Atualiza posição
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) { //Ao clicar no objeto em questão
posPrincX = (int)(Math.random() * 650);
posPrincY = (int)(Math.random() * 700);
labelFlappy.setBounds(posPrincX, posPrincY, 180, 90);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
}
public void editarComponentes(){
labelFlappy.setBounds(posPrincX, posPrincY, 180, 90);
}
public void editarJanela(){
setDefaultCloseOperation(EXIT_ON_CLOSE); //Encerra ao fechar
setSize(750, 700); //Tamanho
setLocationRelativeTo(null); //Centraliza
setVisible(true); //Torna visível
setLayout(null); //Permite redimensionamento de cada componente
setResizable(false); //Impossibilita o redimensionamento pelo usuário
setTitle("Magisterix"); //Título
add(labelFlappy);
}
public static void main(String[] args){
new Jogo1();
}}
Provides a [mcve] so that it is possible to run your code and detect the problem, without it it is difficult to help
– user28595
Clarify your specific issue or add other details to highlight exactly what you need. The way it’s written here, it’s hard to know exactly what you’re asking. See the [Ask] page for help clarifying this question.
– Diego Rafael Souza
These icons don’t let the code run, which can disrupt the problem. Try to edit the form code that is possible to execute without the icons.
– user28595
@Guilhermecostamilam undoes its edition because it removed important excerpts from the code, and this is not recommended to be done in the editions.
– user28595
Import is important? I never put the Imports in my questions, sometimes they are several and several lines and I consider irrelevant in the question, at least in that
– Costamilam
@Guilhermecostamilam yes, it is important, if added, should not be removed.
– user28595
how can I edit to not need images?
– eddunic
@Eddunic ai vc needs to test its own code without the images in the Abels and see if you can reproduce the problem in the same way.
– user28595
I just edited
– eddunic
I executed and did not identify the problem mentioned in the question, if possible, explain how Voce did so that the object leaves the area of the screen.
– user28595
I believe the problem is in the addMovimento method. My goal is to make the object can go to the edge of the Jframe and do not pass from there. When I compile and guide the object, it crosses the edge of the Jframe. I want to impose a limit on the edge of that screen for the movement of the object.
– eddunic
@Eddunic your code does not reproduce very well and has other problems, but see the answer below where I suggest a solution, even without having managed to understand the purpose of this code.
– user28595
@Guilhermecostamilam I particularly think imports help. I’ve seen some issues where the error was in the
import
because there are classes with equal names in different packages. In addition, to be a 100% neat MVCE you have to have the Imports, even to save the respondent from having to fix the given code to reproduce the problem.– Victor Stafusa
I found the solution, what should be done was simple. Just create pause and return conditions according to the coordinates of the object on the screen. Thanks for all your help!
– eddunic