2016-04-14 43 views
1

我在Swing中遇到了另一個問題。我寫了一個打印出可移動圖像的程序,當你點擊一個圖像時,它將被繪製在其他圖像的前面。 雖然我有一個問題,當我想要移動圖像時,例如,如果我點擊並拖動角上的圖像,圖像會跳轉,以便它在鼠標下居中。讓圖片在點擊時不居中

import javax.swing.*; 
 
import java.awt.event.MouseEvent; 
 
import java.nio.file.Paths; 
 
import java.util.ArrayList; 
 
import java.awt.Graphics; 
 
import java.awt.event.MouseAdapter; 
 

 
public class Bildvisare extends JFrame{ 
 
    public Bildvisare(){ 
 
     JPanel panel = new BildPanel();  
 
     add(panel); 
 
     setSize(640,460); 
 
     setVisible(true); 
 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
 
    } 
 
    public static void main(String[] arg){ 
 
     new Bildvisare(); 
 
    } 
 
} 
 
//Ritar ut bilderna i en JPanel o stoppar in dem i en ArrayLista 
 
class BildPanel extends JPanel{ 
 
\t private ArrayList<Image> Bilder = new ArrayList<Image>(); 
 
    Image place = null; 
 
    private int xKoord = 300; private int yKoord = 200; 
 
    
 

 
//Ritar upp alla bilder i ramen 
 
    @Override 
 
    public void paintComponent(Graphics g){ 
 
     super.paintComponent(g); 
 
     for (Image p : Bilder){   \t 
 
      p.draw(g);    
 
     }   
 
    }   
 
//Lägger till "muslyssnare" 
 
    public BildPanel(){  
 
     MouseHandler mh = new MouseHandler(this); 
 
     addMouseListener(mh); 
 
     addMouseMotionListener(mh);   
 
     add(); 
 
    } 
 
    
 
//Lägger till bildens fram och baksida i ArrayListan samt deras koorinater 
 
    public void print(String fram, String bak){ 
 
     Bilder.add(new Image(fram,bak,xKoord,yKoord)); 
 
    } 
 
    
 
//Lägger in bilderna i ArrayListan. 
 
    public void add(){ 
 
    \t print("0.gif","backside.gif"); print("1.gif","backside.gif"); 
 
    \t print("2.gif","backside.gif"); print("3.gif","backside.gif"); 
 
    print("4.gif","backside.gif"); print("5.gif","backside.gif"); 
 
    print("6.gif","backside.gif");print("7.gif","backside.gif");  
 
    } 
 
    
 
//Finner vilken bild som klickas på 
 
    public Image find(int a, int b){   
 
     for (Image p : Bilder){ 
 
      if (p.imageCheck(a, b)){ 
 
       place = p; 
 
       } 
 
     \t } 
 
//  System.out.println(place); 
 
     return place; 
 
    } 
 
    
 
//Flippar bilden 
 
    public void flip(Image img){ 
 
     toFront(img); 
 
     img.flipHelp(); 
 
     repaint(); 
 
    } 
 
//Gör att den bild man klickat på hamnar "längst fram". 
 
    public void toFront(Image img){ 
 
     Bilder.remove(img); 
 
     Bilder.add(img); 
 
    } 
 
    
 
//Flyttar bilden som klickats på och ritar om ramen med alla bilder. 
 
    public void move(Image img, int x0, int y0){ 
 
     toFront(img); 
 
     img.moveHelp(x0,y0); 
 
     repaint(); 
 
    } 
 
} 
 

 
class Image{ 
 
private ImageIcon front,backside,inView; 
 
private int x0,y0,bredd,höjd; 
 

 

 
public Image(String img_front, String img_back,int x1, int y1){ 
 
\t backside = new ImageIcon(img_back); 
 
    front = new ImageIcon(img_front); 
 
    inView=front; 
 
    x0=x1; 
 
    y0=y1; 
 
    getSize();  
 
} 
 

 
//Draw the image 
 
public void draw(Graphics g){ 
 
    g.drawImage(inView.getImage(), x0-bredd/2, y0-höjd/2, null); 
 
} 
 

 
//Flip the image/icon 
 
public void flipHelp(){ 
 
    if (inView==front){ 
 
     inView=backside; 
 
    } 
 
    else{ 
 
     inView=front; 
 
    } 
 
    getSize(); 
 
} 
 

 
//Get the size of each image/icon 
 
public void getSize(){ 
 
    bredd = inView.getIconWidth(); 
 
    höjd = inView.getIconHeight(); 
 
} 
 

 
//Move the picture 
 
public void moveHelp(int x1, int y1){ 
 
    x0=x1; 
 
    y0=y1; 
 

 
} 
 

 
//Check if point is in a certain image 
 
public boolean imageCheck(int x1,int y1){ 
 
    return (Math.abs(x1-x0)<(1+bredd)/2&&Math.abs(y1-y0)<(1+höjd)/2); 
 
    \t } 
 
} 
 

 
class MouseHandler extends MouseAdapter{ 
 
    BildPanel BildPanel; 
 
    Image place = null; 
 
    private int xKoord; private int yKoord; 
 
    
 
    //Konstruktor för att kunna vända på bilden i mouseClicked() 
 
    public MouseHandler(BildPanel p){ 
 
     BildPanel = p; 
 
    } 
 
    
 
    //Det som händer då en bild trycks på. 
 
    @Override 
 
    public void mousePressed(MouseEvent e){ 
 
     int xKoord = e.getX(); int yKoord = e.getY();  
 
     place = BildPanel.find(xKoord, yKoord);  
 
    } 
 
    
 
//Det som händer då en bild klickas på. 
 
    @Override 
 
    public void mouseClicked(MouseEvent e){ 
 
     int xKoord = e.getX(); int yKoord = e.getY();   
 
     Image p = BildPanel.find(xKoord, yKoord); 
 
     if (p!=null){ 
 
      BildPanel.flip(p); 
 
     } 
 
    } 
 
     
 
//Det som händer då man "drar" en bild 
 
    @Override 
 
    public void mouseDragged(MouseEvent e){ 
 
     int xKoord = e.getX(); 
 
     int yKoord = e.getY();   
 
     if (place!=null){ 
 
      BildPanel.move(place, xKoord, yKoord); 
 
     } 
 
    } 
 
}

+0

發佈代碼時不要使用「代碼段」標籤。這僅適用於JavaScript。改爲使用「{}」按鈕。 – camickr

+0

對於[示例](http://stackoverflow.com/questions/33163298/dragging-image-using-mousedrag-method/33163542#33163542)和[示例](http://stackoverflow.com/questions/27915214/how - 可以-I-拖動圖像與 - 對 - 小鼠光標功能於Java的GUI/27915358#27915358) – MadProgrammer

回答

1

用於圍繞面板拖動部件的基本邏輯如下:

public class DragListener extends MouseInputAdapter 
{ 
    Point location; 
    MouseEvent pressed; 

    public void mousePressed(MouseEvent me) 
    { 
     pressed = me; 
    } 

    public void mouseDragged(MouseEvent me) 
    { 
     Component component = me.getComponent(); 
     location = component.getLocation(location); 
     int x = location.x - pressed.getX() + me.getX(); 
     int y = location.y - pressed.getY() + me.getY(); 
     component.setLocation(x, y); 
    } 
} 

我將承擔邏輯是用於拖動的圖像是相同的。我會讓你比較這個工作代碼和你當前的代碼,看看有什麼不同。