gpt4 book ai didi

java - 单击时图像不居中

转载 作者:行者123 更新时间:2023-11-30 07:27:27 26 4
gpt4 key购买 nike

我在 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{
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){
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(){
print("0.gif","backside.gif"); print("1.gif","backside.gif");
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;
}
}
// 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){
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);
}
}

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

最佳答案

在面板周围拖动组件的基本逻辑如下:

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

我假设拖动图像的逻辑是相同的。我会让您将此工作代码与当前代码进行比较,看看有什么不同。

关于java - 单击时图像不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36632600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com