Java - mover letra en applet

 
Vista:
Imágen de perfil de Charly
Val: 178
Ha disminuido su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

mover letra en applet

Publicado por Charly (118 intervenciones) el 13/11/2017 17:22:44
Tengo que crear un applet que mueva una letra horizontalmente y que rebote en las paredes, y un botón para parar el hilo o reanudarlo.
He creado el applet y la letra en la posición que debe empezar, y el botón, pero no se cómo hacer lo del movimiento hacia derecha, o izquierda en su momento. Este es el código que he hecho:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ejercicio2_1 extends Applet implements Runnable,ActionListener{
	private Thread h;
	private boolean parar;
	private Font fuente;
	private Button b1;
	private String letra="C";
	public void start(){
	}
	public void init(){
		setBackground(Color.BLUE);
		add(b1=new Button("Iniciar hilo"));
		b1.addActionListener(this);
		fuente=new Font("Verdana",Font.BOLD,26);
	}
	public void run(){
		parar=false;
		Thread hiloActual=Thread.currentThread();
		while(h==hiloActual&&!parar){
			try{
				Thread.sleep(300);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			repaint();
		}
	}
	public void paint(Graphics g){
		g.clearRect(0,0,400,400);
		g.setFont(fuente);
		g.drawString(letra,1,100);
	}
	public void actionPerformed(ActionEvent e){
		b1.setLabel("Finalizar Hilo");
		if(e.getSource()==b1){
			if(h!=null&&h.isAlive()){
				b1.setLabel("Finalizar Hilo");
			}else{
				h=new Thread(this);
				h.start();
			}
		}else{
			parar=true;
			b1.setLabel("Reanudar Hilo");
		}
	}
	public void stop(){
		h=null;
	}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

mover letra en applet

Publicado por Red (1 intervención) el 18/11/2020 08:53:06
Yo he tenido un trabajo en el cual tube que hacer lo mismo , cogi tu codigo y lo solucione , muchas gracias , puede que no sea ya util pero esto es el codigo solucionado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class clase extends Applet implements Runnable,ActionListener{
 
 
    private Thread h;
    private boolean parar;
    private Font fuente;
    private Button b1;
    private String letra="C";
    private int pos_x=1;
    private int pos_y=100;
    private boolean volver=true;
    public void start(){
    }
    public void init(){
        setBackground(Color.YELLOW);
        add(b1=new Button("Iniciar hilo"));
        b1.addActionListener(this);
        fuente=new Font("Verdana",Font.BOLD,26);
    }
    public void run(){
        parar=false;
        Thread hiloActual=Thread.currentThread();
        while(h==hiloActual&&!parar){
            repaint();
            if(pos_x == getSize().width) {
                volver=true;
            }else if (volver && pos_x==1) {
                volver=false;
            }
            if (volver==false) {
                pos_x=pos_x+1;
            }else {
                pos_x=pos_x-1;
            }
            try{
                Thread.sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
 
        }
    }
    public void paint(Graphics g){
        g.clearRect(0,0,400,400);
        g.setFont(fuente);
        g.drawString(letra,pos_x,pos_y);
    }
    public void actionPerformed(ActionEvent e){
        b1.setLabel("Finalizar Hilo");
        if(e.getSource()==b1){
            if(h!=null&&h.isAlive()){
                b1.setLabel("Reanudar Hilo");
                h.stop();
            }
            else{
                b1.setLabel("Finalizar Hilo");
                h=new Thread(this);
                h.start();
            }
        }else{
            parar=true;
            b1.setLabel("Reanudar Hilo");
        }
    }
    public void stop(){
        h=null;
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar