ayuda con repaint
Publicado por andres (1 intervención) el 03/03/2013 17:28:35
bueno lo k pasa es k no se como mover una pelota, soy nuevo en esto, kiero hacerla en un jpanel y pasarla a un jframe, y la trato de mover con un hilo, pero nose por k no m funciona el repaint o no se k es exactamente, asi lo tengo.
import javax.swing.JFrame;
public class Area extends JFrame {
public Area(){
super();
Pelota p = new Pelota();
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(p);
setVisible(true);
}
public static void main(String[] args) {
Pelota p = new Pelota();
Area a = new Area();
p.run();
}
}
--------------
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Pelota extends JPanel implements Runnable{
private int x;
private int y;
private int paso=1;
private int radio=10;
Thread pel;
public Pelota(){
super();
setSize(30,30);
setVisible(true);
this.x=10;
this.y=10;
}
public void mover(){
x=x+paso;
y=y-paso;
}
public void run(){
for (int i = 0; i < 5; i++) {
mover();
repaint();
System.out.println(x+"---"+y);
try {
pel.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
g.fillOval(x, y, 30, 30);
}
}
import javax.swing.JFrame;
public class Area extends JFrame {
public Area(){
super();
Pelota p = new Pelota();
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(p);
setVisible(true);
}
public static void main(String[] args) {
Pelota p = new Pelota();
Area a = new Area();
p.run();
}
}
--------------
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Pelota extends JPanel implements Runnable{
private int x;
private int y;
private int paso=1;
private int radio=10;
Thread pel;
public Pelota(){
super();
setSize(30,30);
setVisible(true);
this.x=10;
this.y=10;
}
public void mover(){
x=x+paso;
y=y-paso;
}
public void run(){
for (int i = 0; i < 5; i++) {
mover();
repaint();
System.out.println(x+"---"+y);
try {
pel.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
g.fillOval(x, y, 30, 30);
}
}
Valora esta pregunta


0