Einen String bewegen wie?

java007

Bekanntes Mitglied
Hallo Leute,

ich habe es geschaft einen String mit
drawString(); auf dem Bildschirm auszugeben nun würde mich interessieren wie ich eine bewegung hinzufügen könnte, sodass der String z.B. auf der Zeile bleibt und nach rechts bewegt wird mit anschließendem start von links wieder.

VGrüße :)
 

Niki

Top Contributor
Java:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class StringBewegen extends JFrame {

	private StringBewegenPanel panel = null;

	public StringBewegen() {
		super("String bewegen");
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());

		panel = new StringBewegenPanel();
		panel.setPreferredSize(new Dimension(300, 100));
		final JButton button = new JButton("Start");

		add(button, BorderLayout.NORTH);
		add(panel, BorderLayout.CENTER);

		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				new Thread(new Runnable(){
					public void run() {
						button.setEnabled(false);
						int w = panel.getWidth(); 
						for(int i = panel._x; i < w; i++){
							panel._x = i;
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
								
							}
							SwingUtilities.invokeLater(new Runnable(){
								public void run() {
									panel.validate();
									panel.repaint();
								};								
							});								
						}
						panel._x = 20;
						panel.repaint();
						button.setEnabled(true);						
					}
				}).start();
				

			}
		});

		setResizable(false);
		pack();
		setVisible(true);
	}

	private class StringBewegenPanel extends JPanel {

		private int _x = 20;
		private int _y = 20;

		@Override
		protected void paintComponent(Graphics arg0) {
			super.paintComponent(arg0);
			arg0.drawString("Ich bewege mich", _x, _y);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new StringBewegen().setVisible(true);
	}

}
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
G String an einen php Script senden Android & Cross-Platform Mobile Apps 8
T Android Studio: Einen Button in einer For Schleife verwenden Android & Cross-Platform Mobile Apps 2
F Mit Canvas zeichen und durch einen Timer aktualiesieren Android & Cross-Platform Mobile Apps 1
T einen kreis zeichnen.. Android & Cross-Platform Mobile Apps 1
M Inhalt eines Eingabefeldes an einen Server senden? Android & Cross-Platform Mobile Apps 9
N Textview macht immer nach einem Beistrich einen Abstand Android & Cross-Platform Mobile Apps 6
N Anfängerfrage - mit Java einen Anruf initiieren ? Android & Cross-Platform Mobile Apps 3
W UTF-8 String Android & Cross-Platform Mobile Apps 66
W Base64 konvertierter URI String Android & Cross-Platform Mobile Apps 32
W String Array Pfad in Int setzen Android & Cross-Platform Mobile Apps 54
W Volley String Response gibt falchen if aus Android & Cross-Platform Mobile Apps 35
H Anfänger String types not allowed (at 'textColor' with value 'black' Android & Cross-Platform Mobile Apps 13
W Firestore String in Apps Laden Android & Cross-Platform Mobile Apps 10
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
A Mit Java neues item in ein string-array einer Strings.xml schreiben Android & Cross-Platform Mobile Apps 4
C Zugriff auf die Position eines String- bzw Spinner-Arrays Android & Cross-Platform Mobile Apps 1
J Android String in andere Java-Dateien überführen Android & Cross-Platform Mobile Apps 1
J R.string.(variable) geht das Android & Cross-Platform Mobile Apps 3
R Android incomingNumber bein Eingehenden Anruf immer leerer String Android & Cross-Platform Mobile Apps 4
S SPLIT funktion bei STRING funktioniert nicht! Android & Cross-Platform Mobile Apps 4
J Plötzlich "java.lang.String cannot be converted to JSONObject" Android & Cross-Platform Mobile Apps 9
T int to string ... Android & Cross-Platform Mobile Apps 8
A String[] für Lisadapter Android & Cross-Platform Mobile Apps 4
M jsonobject cannot be cast to java.lang.string Android & Cross-Platform Mobile Apps 4
N Android Hilfe string to float geht nicht... Android & Cross-Platform Mobile Apps 4
R String wie WAV Datei nutzen Android & Cross-Platform Mobile Apps 4
C 2 kleine Probleme (Datei lesen, String durchsuchen) Android & Cross-Platform Mobile Apps 16
L String dem Display anpassen Android & Cross-Platform Mobile Apps 12
G Text parsen String to Double Android & Cross-Platform Mobile Apps 2
S ein String nach vorgegebenen Zeichen teilen Android & Cross-Platform Mobile Apps 3
N Zeichen im String löschen? Android & Cross-Platform Mobile Apps 18
M MIDlet + Datum in String Android & Cross-Platform Mobile Apps 5
J Canvas mit dem Accelorometersensor bewegen Android & Cross-Platform Mobile Apps 0

Ähnliche Java Themen

Neue Themen


Oben