2016-10-18 1 views
0

私はずっとjavaでプログラミングしていないので、とても混乱しています。私はコードがかなり整理されていて、それほどオブジェクト指向ではないことは知っていますが、なぜ私の "Laser Dot"イメージが "Map Tracker"イメージだけで動くのか不思議です。私はそれを私がやろうとしているGPSトラッカープロジェクトのテストとして使っています。私はちょうど赤い点画像が地図画像に沿って動くことができるようにしたい。setLocation()関数は別のJLabel上のJLabelの位置を変更することを許可していません

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"); 

} 

} 

引き出し線は無視することができます。私の問題には関係しません。

+0

try/catchブロックを使用して例外を見つける方法はありますか?いいえ、ありがとうございます。 – Omer

答えて

0

あなたのレーザードットイメージは移動していないため、移動していません。レーザードットを描いた後、もう一度触れないでください。移動するには、位置が変更された後に再描画するための更新ループが必要です。より多くの情報を得るには、いくつかのゲームのチュートリアルを見てください。 exampleの場合

0

dotラベルdoiesにはコンテンツがありません。あなたのredDotイメージをアイコンとして入れてみてください。

さらに、setLoaction()setBounds()に電話する必要はありません。あなたのドットラベルのx軸とyの位置は、すでにsetBounds()に含まれている:

申し訳
... 
    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 

、あなたはどのような方法でそれを更新せずに一度だけ、あなたのビューを作成することを言及するのを忘れてしまいました。それは動きがないことを説明します。

ここでは、動きやアニメーションを実現する方法の例を示します。

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 
関連する問題