Hallo!
Ich versuche grade eine Kopie eines JLabels zu erzeugen. Die Kopie ist im Prinzip eine neue Instanz einer meiner JLabelKlasse die exakt die gleichen Parameter wie das zu kopierende JLabel hat. Mein Problem dabei ist, dass ich die Kopie auf meinem Mainframe nicht gezeichnet wird. Ich vermute das ich es am falschen Zeitpunkt adde nur weiss ich nicht wie ich es besser lösen könnte... Ich hoffe ihr versteht mich und könnt mir weiterhelfen!
Hier die entsprechende Klasse mit MouseEvents:
Ich versuche grade eine Kopie eines JLabels zu erzeugen. Die Kopie ist im Prinzip eine neue Instanz einer meiner JLabelKlasse die exakt die gleichen Parameter wie das zu kopierende JLabel hat. Mein Problem dabei ist, dass ich die Kopie auf meinem Mainframe nicht gezeichnet wird. Ich vermute das ich es am falschen Zeitpunkt adde nur weiss ich nicht wie ich es besser lösen könnte... Ich hoffe ihr versteht mich und könnt mir weiterhelfen!
Hier die entsprechende Klasse mit MouseEvents:
Code:
public class GUI extends JFrame implements Runnable{
private static final long serialVersionUID = 6411499808530678723L;
...
private static Ship s1copy;
private static Ship s2copy;
private static Ship s3copy;
private static Ship s4copy;
private static Ship s5copy;
...
public GUI (int pWidth, int pHeight){
...
contentPanel.setLayout(null);
contentPanel.add (rivalPanel);
contentPanel.add (selfPanel);
contentPanel.add (additionPanel);
contentPanel.add (status);
contentPanel.add (finishRound);
contentPanel.add (capitulateGame);
contentPanel.add (createGame);
contentPanel.add (cancelGame);
contentPanel.add (joinGame);
contentPanel.add (exit);
this.setVisible (true);
doInit();
}
...
public void paintComponents (Graphics g){
super.paintComponents(g);
selfPanel.refresh();
additionPanel.refresh();
}
public void run(){
while (runGame){
computeDelta();
if (System.nanoTime() >= (refreshfps + 500000000)){
status.setText(" FPS: " + fps + "\n IP: none\n Ping: none");
refreshfps = System.nanoTime();
}
this.repaint();
try{
Thread.sleep (10);
}catch (InterruptedException e){}
}
}
....
private void guiPanelMouseDragged (MouseEvent evt){
if (hitLabel != null){
Point p = evt.getPoint();
p.x = evt.getX();
p.y = evt.getY();
hitLabel.setLocation (p);
this.setComponentZOrder (hitLabel, 0);
hitLabel.repaint();
}
}
private void guiMousePressed (MouseEvent evt){
Component c = contentPanel.getComponentAt (evt.getPoint());
if (c instanceof AdditionPanel){
Point p = evt.getPoint();
System.out.println(p);
p.x = evt.getX() - c.getX();
p.y = evt.getY() - c.getY() - 30;
System.out.println ("Pressed: " + p);
c = additionPanel.getComponentAt (p);
try{
hitLabel = (JLabel) c;
String name = c.getName();
if (name.equals("s1")){
if (s1copy == null){
s1copy = new Ship (2, "s1copy");
this.add (s1copy);
hitLabel = s1copy;
}else{
hitLabel = null;
}
}else if (name.equals("s2")){
if (s2copy == null){
s2copy = new Ship (3, "s2copy");
this.add (s2copy);
hitLabel = s2copy;
}else{
hitLabel = null;
}
}else if (name.equals("s3")){
if (s3copy == null){
s3copy = new Ship (3, "s3copy");
this.add(s3copy);
hitLabel = s3copy;
}else{
hitLabel = null;
}
}else if (name.equals("s4")){
if (s4copy == null){
s4copy = new Ship (4, "s4copy");
this.add (s4copy);
hitLabel = s4copy;
}else{
hitLabel = null;
}
}else if (name.equals("s5")){
if (s5copy == null){
s5copy = new Ship (5, "s5copy");
this.add (s5copy);
hitLabel = s5copy;
}else{
hitLabel = null;
}
}
if (hitLabel != null){
System.out.println (hitLabel);
deltaX = evt.getX();
deltaY = evt.getY();
System.out.println ("Delta: " + evt.getPoint());
backupX = this.getX();
backupY = this.getY();
}
}catch (ClassCastException e){
System.out.println ("Keine Label an der entsprechenden Position");
}
}
}
private void guiMouseReleased (MouseEvent evt){
if( hitLabel != null ){
if (this.getComponentAt (evt.getPoint()) instanceof GamePanel){
System.out.println ("On GamePanel");
hitLabel.setLocation(backupX, backupY);
hitLabel.repaint();
}else{
backupX = hitLabel.getX();
backupY = hitLabel.getY();
}
this.repaint();
hitLabel = null;
}
}
...
public static void main (String[] args){
new GUI (650, 660);
}
}