gpt4 book ai didi

java - 使用 jframe 的多个 jComponent 动画

转载 作者:行者123 更新时间:2023-12-01 23:28:37 26 4
gpt4 key购买 nike

我无法在花园网格面板顶部看到动画。无论我最后添加到显示的内容 Pane 中。无论如何可以同时显示多个组件。我尝试切换类动画和花园网格来扩展 jpanel 但没有任何效果。动画是一系列图像,显示用鼠标拖动角色时移动的角色。我需要该角色在花园网格面板的顶部移动。有什么帮助吗?

  package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.Panel;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;


public class Driver {
public static void main(String[] args) {

JFrame frame = new JFrame();
JLayeredPane pane = new JLayeredPane();
frame.add(pane);

Animation animation = new Animation();
GardenPanel garden = new GardenPanel(6,4,600,800);


pane.add(animation,new Integer(1));

pane.add(garden, new Integer(0));
frame.setSize(800, 600);


System.out.println(pane.highestLayer());// just to check I dont have hidden layers







frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

while(true){
frame.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

}

--------动画类------------

你应该看到一个正在移动的兽人。您可以使用鼠标拖动兽人。如果你按 * 鼠标不动时,威力会增加。 * */

package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Animation extends JComponent implements MouseMotionListener, MouseListener{
final int frameCount = 10;
int picNum = 0;
BufferedImage[][] pics;
int xloc = 0;
int yloc = 0;
final int xIncr = 8;
final int yIncr = 2;
final static int frameWidth = 900;
final static int frameHeight = 600;
final static int imgWidth = 165;
final static int imgHeight = 165;
//basic info about the orc

BufferedImage seedImage;
int mouseX;
int mouseY;
int seedX = xloc;
int seedY = yloc;
boolean mouseholding;
boolean moving = false;
int power;
public enum stage {
MOVE, POWER
}
stage s;

//Override this JPanel's paint method to cycle through picture array and draw images
public void paint(Graphics g) {
picNum = (picNum + 1) % frameCount;
if(mouseholding && (s == s.POWER)){
System.out.println(power++);
}


g.drawImage(pics[0][picNum], xloc, yloc, Color.gray, this);
g.drawImage(seedImage, seedX, seedY, imgWidth/8, imgHeight/8, this);
}


//Constructor: get image, segment and store in array
public Animation(){
seedImage = createImage();
BufferedImage[] img = createAnimation();
pics = new BufferedImage[img.length][10];
for(int j = 0; j < img.length; j++){
for(int i = 0; i < frameCount; i++)
pics[j][i] = img[j].getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
}
addMouseMotionListener(this);
addMouseListener(this);
}

//Read image from file and return
private BufferedImage[] createAnimation(){
BufferedImage[] bufferedImage = new BufferedImage[4];
try {
bufferedImage[0] = ImageIO.read(new File("images/orc_forward_southeast.png"));
bufferedImage[1] = ImageIO.read(new File("images/orc_forward_southwest.png"));
bufferedImage[2] = ImageIO.read(new File("images/orc_forward_northeast.png"));
bufferedImage[3] = ImageIO.read(new File("images/orc_forward_northwest.png"));
return bufferedImage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private BufferedImage createImage(){
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(new File("images/seed.png"));
return bufferedImage;
} catch (IOException e) {
e.printStackTrace();
}
return null;

// TODO: Change this method so you can load other orc animation bitmaps
}

// implement the method in mouseListener and mouseMotionListener interface
@Override
public void mouseDragged(MouseEvent event) {
mouseX = event.getX();
mouseY = event.getY();
if( ( xloc<mouseX ) && (xloc+imgWidth >mouseX) && (yloc<mouseY) && (yloc+imgHeight>mouseY) && (s==stage.MOVE) ){

//System.out.println("Imagecoor:("+xloc+", "+yloc+")"+" mousecoor:("+mouseX+","+mouseY+")");
// you can print the coordinate if you want
xloc = mouseX-imgWidth/2;
yloc = mouseY-imgHeight/2;
seedX = mouseX;
seedY = mouseY;
}
} // draging the image
@Override
public void mouseMoved(MouseEvent arg0) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}

// for increasing the power.
@Override
public void mousePressed(MouseEvent event) {
mouseholding = true;
mouseX = event.getX();
mouseY = event.getY();
if( (xloc == mouseX-imgWidth/2) && (yloc == mouseY-imgHeight/2) ){
s = stage.POWER;
}
}
@Override
public void mouseReleased(MouseEvent event) {
mouseholding = false;
s = stage.MOVE;
power = 0;
}
}

--------GardenPanel类------------应显示 2 条黑线

package view;

import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class GardenPanel extends JComponent {
private int numOfRows;
private int numOfColumns;
private int frameWidth;
private int frameHeight;

public GardenPanel(int numOfRows, int numOfColumns, int frameHeight, int frameWidth){
this.numOfRows = numOfRows;
this.numOfColumns = numOfColumns;
this.frameHeight = frameHeight;
this.frameWidth = frameWidth;


}
int secondX = ((frameWidth -((frameWidth/3) *2)) / 4) * 3;

@Override
public void paint(Graphics g){
g.drawLine((frameWidth/3) * 2, (frameHeight/4) * 3, (frameWidth/3 * 2) - 30, frameHeight/4);

g.drawLine(((frameWidth/3) * 2) + 200, (frameHeight/4) * 3, ((frameWidth -((frameWidth/3) *2)/4) * 3) - 30, frameHeight/4);



}

}

最佳答案

您声明:

I am having trouble getting the animation visible on top of my garden grid panel. Whichever I add last to the contentpane that one shows.

JFrame 的 contentPane 使用 BorderLayout,这就是 BorderLayout 的工作原理。如果您默认添加一个组件(没有 int 第二个参数),则默认情况下它将放置在 BorderLayout.CENTER 位置并覆盖之前添加的所有内容。您需要阅读 Swing 布局管理器教程以获取更多详细信息。

is there anyway to show multiple components at the same time.

是的,使用不同的布局管理器和组件。如果您希望组件彼此重叠,请考虑使用 JLayeredPane。

I've tried switching the classes animation and garden grid to extend jpanel but nothing has worked. Animation is a series of images that shows a character moving when you are dragging it with a mouse. I need that character to move on top of the garden grid panel. any help?

不确定您的具体问题是什么。

关于java - 使用 jframe 的多个 jComponent 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648299/

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