我正在完成我在OOP Java中的作业。分配是在JFrame上加载图像,能够移动它们(顶层应该优先,当前不是),然后单击它们“翻转它们”(基本上改变图像的来源)。我目前无法找到解决方案,如何正确“分层”屏幕上可见的图像,并优先在顶部优先处理图像(目前最重要的是图像的优先级)。
我也有找到改变图像来源的好方法的问题,因为我们的老师禁止使用Swing扩展Picture类。
我第一次尝试解决这个问题是在ArrayList中保存每个“Picture”对象的信息。这可以保存图像的位置,但不能解决我的分层问题。我也想使用JLayeredPane,但正如我发现的那样,它比我想象的更难,因为我还没有找到一种可行的解决方案(我可能会遗漏一些关于它如何工作的明显事实)。
我想可能需要发生的是我将每个图像的“位置”保存在某种类型的数组中,然后使用此数组通过paintComponent @ ImagePanel打印出图像。这是我正在做的事情,但它没有按照我的意愿行事。我认为我在“Picture”类中加载图像的方式可能与它有关。我没有很多Java经验,所以这对我来说都是新的。
我不想打印出我的所有代码,因为我有4个类,所以我打算打印出我觉得每个类中必不可少的方法。如果你们需要的东西是为了引导我朝着正确的方向发展,那么我也会提供。
画@图片
public void draw(Graphics g, int i) {
try {
BufferedImage img = ImageIO.read(new File("images/icon_"+ i +".gif"));
g.drawImage(img, x, y, null);
} catch(IOException ie) {
System.out.println("Could not find images");
}
}
mousePressed&mouseDragged @ MouseHandler
public void mousePressed (MouseEvent e) {
Point point = e.getPoint();
chosen = imagepanel.locateImage(point);
}
public void mouseDragged (MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (chosen != null) {
imagepanel.moveImage(chosen, x, y);
}
}
loadImages和paintComponent @ ImagePanel
private final static int IMAGES = 7;
private ArrayList <Picture> imageCollection = new ArrayList<>();
private Picture im;
Random rand = new Random();
public void loadImages() {
for(int i=0; i<IMAGES; i++) {
int x = rand.nextInt(400) + 40;
int y = rand.nextInt(400) + 60;
im = new Picture(x,y);
imageCollection.add(im);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int i = 0;
for (Picture im : imageCollection) {
i++;
im.draw(g, i);
}
}
我希望无论何时“翻转”(点击)或移动(拖动),图像都会堆叠在彼此之上。他们目前不这样做,因为他们只是保持他们的“深度”位置。我已经尝试实现Image []但没有成功。
我也有一个翻转方法,我尝试使用setIcon(我之前使用的是ImageIcon而不是Image),但由于某些原因,这并没有真正起作用。
到目前为止,我也非常希望能够获得有关代码的任何反馈,以及我总是希望改进的任何改进。
编辑:我设法解决我的问题,但我确信有更好的方法来做到这一点。
public void placeFirst(Picture im) {
int pos = imageCollection.indexOf(im);
imageCollection.remove(pos);
imageCollection.add(0, im);
}
public void flipImage(Picture im) {
im.flip();
placeFirst(im);
repaint();
}
public void moveImage(Picture im, Point point) {
im.move(point.x-(im.getWidth(im)/2), point.y-(im.getHeight(im)/2));
placeFirst(im);
repaint();
}
public Picture locateImage(Point point) {
for (int i=0; i<imageCollection.size(); i++) {
Picture im = imageCollection.get(i);
if (im.fits(point)) {
return im;
}
}
return null;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// There probably exists a better and visually nicer way of doing this
for (int i=imageCollection.size()-1; i>=0; i--) {
Picture im = imageCollection.get(i);
im.draw(g);
}
}
chosen = imagepanel.locateImage(point);
好吧,我们不知道locateImage(...)方法是如何工作的,但我猜你只是遍历数组直到找到匹配。
所以你总能找到相同的比赛。
因此,如果您希望图像堆叠在顶部,则有两个问题: