2016-10-18 8 views
0

Ich habe nicht in Java für eine lange Zeit programmiert, so dass ich sehr verwirrt bin. Ich weiß, dass der Code ziemlich unorganisiert ist und nicht sehr OO, aber ich frage mich, warum mein "Laser Dot" Bild nicht allein das "Map Tracker" Bild bewegt. Ich benutze es als Test für ein GPS-Tracker-Projekt, das ich machen werde. Ich möchte nur, dass das rote Punktbild sich entlang des Kartenbildes bewegen kann.setLocation() Funktion erlaubt mir nicht, den Standort eines JLabel auf einem anderen JLabel zu ändern

import java.awt.*; 
import java.awt.geom.Line2D; 
import java.awt.image.*; 
import java.io.*; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

public class Main extends JFrame{ 

public static void main(String[] args) { 


    BufferedImage img = null; 
    BufferedImage redDot = null; 

    try { 
     System.out.println(new File("C:/Users/User/Downloads/MapTracker.jpg").exists()); 
     img = ImageIO.read(new File("C:/Users/User/Downloads/MapTracker.jpg")); 
     redDot = ImageIO.read(new File("C:/Users/User/Downloads/LaserDot.png")); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    JFrame frame = new JFrame(); 
    frame.setTitle("Window"); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(1366,768); 
    int w = img.getWidth(null); 
    int h = img.getHeight(null); 

    ImageIcon icon = new ImageIcon(img); 
    JLabel label = new JLabel(icon); 
    label.setLayout(null); 
    JLabel dot = new JLabel(); 

    frame.add(label); 

    Graphics2D g = (Graphics2D) img.getGraphics(); 
    label.add(dot); 
    dot.setLocation(1000,300); 
    dot.setBounds(100,100,500,500); 


    g.drawImage(redDot, 0, 0, null); 
    g.setStroke(new BasicStroke(2)); 
    g.setColor(Color.BLACK); 
    g.draw(new Line2D.Double(0.0,0.0,500.0,500.0)); 
    System.out.println("Image Drawn"); 

} 

} 

Die Zeichnungslinie kann ignoriert werden. Es ist nicht relevant für mein Problem.

+0

Kannst du try/catch-Block setzen, um die Ausnahme durch eine der Methoden zu finden? – Omer

Antwort

0

Nun, Ihr Laserpunktbild bewegt sich nicht, weil Sie es nicht bewegen. Nachdem Sie einen Laserpunkt gezeichnet haben, berühren Sie ihn nicht mehr. Um es zu bewegen, benötigen Sie eine Update-Schleife, um es neu zu zeichnen, nachdem sich seine Position geändert hat. Um mehr Informationen zu erhalten, werfen Sie einen Blick auf eine Spielanleitung. Für example.

0

Ihr dot Etikett hat keinen Inhalt. Setzen Sie Ihr redDot Bild als Symbol darauf und Sie werden etwas sehen.

Außerdem müssen Sie nicht setLoaction()undsetBounds() anrufen. Die x- und y-Position Ihres dot-Label ist bereits im setBounds() enthalten:

... 
    ImageIcon icon = new ImageIcon(img); // your map background as icon 
    JLabel label = new JLabel(icon); // your map background label 
    label.setLayout(null); 
    JLabel dot = new JLabel(new ImageIcon(redDot)); // you need to put some content in your dot label - like 

    frame.add(label); 

    Graphics2D g = (Graphics2D) img.getGraphics(); 
    label.add(dot); 
    // dot.setLocation(1000, 300);  // when you use setBounds, you don't need location 
    dot.setBounds(50, 50, 60, 60); // x-location, y-location, with, height 
    dot.revalidate(); // just to make sure that your dot actually takes the new bounds and paints it 

Leider vergaß zu erwähnen, dass Sie nur einmal Ihre Ansicht erstellen, ohne sie in irgendeiner Weise zu aktualisieren. Was erklärt, dass es keine Bewegung gibt.

Hier ist ein Beispiel, wie Sie eine Bewegung/Animation erreichen können. Aber es gibt so viele verschiedene Möglichkeiten und es kommt darauf an - es ist nur ein Beispiel:

int w = img.getWidth(); 
    int h = img.getHeight(); 
    frame.setSize(w, h);  // I just did that to make the example easier to understand 

    JLabel label = new JLabel(new ImageIcon(img)); // so your "background" label with the map image 
    // we need this dot label somewhere else and it must be final 
    final JLabel dot = new JLabel(new ImageIcon(redDot)); // and your dot label with the dot image as icon 

    label.add(dot); 
    frame.add(label); 
    label.setBounds(0, 0, w, h); // just for the example, the map-label is set to fill the frame 
    dot.setBounds(50, 50, 60, 60); // setBounds will handle both: location and size 
    frame.pack(); // pack will do all the stuff to make sure everything is painted and has the correct size when the window becomes visible 

    // and here comes the animation: 
    // my solution is to use a swing timer. Swing timers execute in a background thread and are perfectly suited to "update" any swing component without blocking the User Input thread. 
    // this timer will call the actionPerformed() of the ActionListener every 500 ms 
    Timer timer = new Timer(500, new ActionListener(){ 

     @Override 
     public void actionPerformed(ActionEvent e){ 
      Point location = dot.getLocation(); // get current location of the dot 
      dot.setLocation(location.x + 10, location.y + 10); // just set a new location 10 px further right and further down 
      dot.revalidate(); // revalidate, so the updated position will be taken into account when the dot label will get painted again 
     } 
    }); 
    timer.start(); // start the timer and it will immediately start moving your dot on the img