2016-08-22 6 views
-4

イメージの座標を取得して、座標に対してJPanelの背景色を変更することができます。ここまで私がこれまで持っていたことがあります。コードでは、矢印を上下左右に移動できます。矢印が最初の象限であれば背景は赤で、矢印が第2象限に移動すると背景は緑に変わりますJPanel(Java)上のImageIconの現在の位置を取得したい

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class DirectionPanel extends JPanel 
{ 
    private final int X = 200; 
    private Point point = null; 

    private final int WIDTH = 400, HEIGHT = 200; 
    private final int JUMP = 10; // increment for image movement 

    private final int IMAGE_SIZE = 31; 

    private ImageIcon up, down, right, left, currentImage; 
    private int x, y; 

    //----------------------------------------------------------------- 
    // Constructor: Sets up this panel and loads the images. 
    //----------------------------------------------------------------- 
    public DirectionPanel() 
    { 
     addKeyListener (new DirectionListener()); 

     x = WIDTH/2; 
     y = HEIGHT/2; 

     up = new ImageIcon ("arrowUp.gif"); 
     down = new ImageIcon ("arrowDown.gif"); 
     left = new ImageIcon ("arrowLeft.gif"); 
     right = new ImageIcon ("arrowRight.gif"); 

     currentImage = right; 

     setBackground (Color.black); 
     setPreferredSize (new Dimension(WIDTH, HEIGHT)); 
     setFocusable(true); 
    } 

    //----------------------------------------------------------------- 
    // Draws the image in the current location. 
    //----------------------------------------------------------------- 
    public void paintComponent (Graphics page) 
    { 
     super.paintComponent (page); 
     currentImage.paintIcon (this, page, x, y); 
    } 

    //***************************************************************** 
    // Represents the listener for keyboard activity. 
    //***************************************************************** 
    private class DirectionListener implements KeyListener 
    { 
     //-------------------------------------------------------------- 
     // Responds to the user pressing arrow keys by adjusting the 
     // image and image location accordingly. 
     //-------------------------------------------------------------- 
     public void keyPressed (KeyEvent event) 
     { 
     switch (event.getKeyCode()) 
     { 
      case KeyEvent.VK_UP: 
       currentImage = up; 
       y -= JUMP; 
       break; 
      case KeyEvent.VK_DOWN: 
       currentImage = down; 
       y += JUMP; 
       break; 
      case KeyEvent.VK_LEFT: 
       currentImage = left; 
       x -= JUMP; 
       break; 
      case KeyEvent.VK_RIGHT: 
       currentImage = right; 
       x += JUMP; 
       break; 
     } 

     point = ??????? 

     if(point.x < 200 && point.y < 100) 
     { 
      setBackground(Color.red); 
     } 
     else if(point.x < 200 && point.y > 100) 
     { 
      setBackground(Color.green); 
     } 
     else if(point.y > 100 && point.x > 200) 
     { 
      setBackground(Color.cyan); 
     } 
     else if(point.y < 100 && point.x > 200) 
     { 
      setBackground(Color.yellow); 
     } 

     repaint(); 
     } 

     //-------------------------------------------------------------- 
     // Provide empty definitions for unused event methods. 
     //-------------------------------------------------------------- 
     public void keyTyped (KeyEvent event) {} 
     public void keyReleased (KeyEvent event) {} 
    } 
} 
+0

'point = new Point(x、y);' ?? –

+0

ImageIconsには場所のプロパティがありません – ControlAltDel

+0

ありがとうございます。それは –

答えて

3

「x」と「y」を使用して、これらの変数がJPanelでの画像の座標になるように画像を描画します。

あなたはにあなたのポイントを設定することができます:あなたは「X」と「Y」を更新したときに今、ポイントも更新され、背景があなたの仕様に応じて描画されます

point = new Point(x, y); 

私はそれが助けてくれることを願っています!

+0

ありがとうございます。コードは今動作します! –

+0

問題の解決に役立つ場合は、[受け入れてください](http://meta.stackexchange.com/a/5235/155831)してください。 –

関連する問題