I investigated a bit about the problem and found something about how swing manages threads but got me more confused i founded a couple of vids about java game developing but didn't help much, well i appreciate any help you can give me and thank you for reading.
package Game;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class plomero {
int x,y,movx;
Image marioderecha;
public plomero(){
ImageIcon i = new ImageIcon("C:/bla/bla/bla.gif");
marioderecha= i.getImage();
x=10;
y=250;
}
public void move(){
x += movx;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Image getImage(){
return marioderecha;
}
public void KeyPressed(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT){
movx = -1;
}
if (key == KeyEvent.VK_RIGHT){
movx = 1;
}
}
public void KeyReleased(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT){
movx = 0;
}
if (key == KeyEvent.VK_RIGHT){
movx = 0;
}
}
package Game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class panelG extends JPanel implements ActionListener{
plomero p;
Image img;
Timer time;
public panelG(){
p= new plomero();
addKeyListener(new ActionEscuchador());
setFocusable(true);
ImageIcon i = new ImageIcon("C:/Users/Benji/Desktop/java/win/bin/Juego/es.jpg");
img= i.getImage();
time = new Timer(5, this);
time.start();
}
public void actionPerformed(ActionEvent e) {
p.move();
repaint();
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(img, 0, 0, this);
g.drawImage(p.getImage(), p.getX(), p.getY(), this);
repaint();
}
private class ActionEscuchador extends KeyAdapter{
public void KeyReleased(KeyEvent e){
p.KeyReleased(e);
}
public void KeyPressed(KeyEvent e){
p.KeyPressed(e);
}
}
}
package Game;
import javax.swing.*;
public class windowG extends JFrame implements Runnable{
public void run(){
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new panelG());
setSize(800, 500);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new ventanaG());
}
}

New Topic/Question
Reply



MultiQuote




|