使用java将图像加载到数组中
我有一个我正在开发的纸牌游戏,需要将纸牌图像加载到屏幕上。我正在使用ImageIcon和PaintIcon打印图像。我希望图像是可点击的,但我不知道如何做到这一点 我之所以使用PaintIcon,是因为我希望图像只需点击一个按钮即可移动。(堆叠卡片以节省空间,展开以查看所有卡片) 我不知道该搜索什么,也不知道该开始什么 如果有人能告诉我样本代码或正确的方向,这将有所帮助 这是我使用的代码:使用java将图像加载到数组中,java,Java,我有一个我正在开发的纸牌游戏,需要将纸牌图像加载到屏幕上。我正在使用ImageIcon和PaintIcon打印图像。我希望图像是可点击的,但我不知道如何做到这一点 我之所以使用PaintIcon,是因为我希望图像只需点击一个按钮即可移动。(堆叠卡片以节省空间,展开以查看所有卡片) 我不知道该搜索什么,也不知道该开始什么 如果有人能告诉我样本代码或正确的方向,这将有所帮助 这是我使用的代码: public class CustomGraphicsDemo2 extends JFrame {
public class CustomGraphicsDemo2 extends JFrame {
// Define constances
private static final int CANVAS_WIDTH = 640;
private static final int CANVAS_HEIGHT = 480;
//Array of image cards
//Handle for the custom drawing panel
private DrawCanvas canvas;
private ImageIcon card1, card2, card3,card4;
//Attributes of Drawing object
private int x = 100; // x and y position
private int y = 100;
private int size = 50;
private int xSpeed = 3; // moving speed in x and y directions
private int ySpeed = 5;
//Constructor to create the UI components
public CustomGraphicsDemo2() {
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
this.setContentPane(canvas);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setTitle("Custom Graphics Demo");
this.setVisible(true);
// Refresh the display at regular interval.
// Run the display refresh code in its own thread.
Thread updateThread = new Thread() {
public void run() {
while (true) {
update(); // update the (x, y) position
repaint(); // Refresh the JFrame, callback paintComponent()
try {
// Delay and give other thread a chance to run
Thread.sleep(50); // milliseconds
} catch (InterruptedException ex) {}
}
}
};
updateThread.start(); // callback run()
}
// Update the (x, y) position of the graphical object
public void update() {
x += xSpeed;
y += ySpeed;
if (x > CANVAS_WIDTH - size || x < 0) {
xSpeed = -xSpeed;
}
if (y > CANVAS_HEIGHT - size || y < 0) {
ySpeed = -ySpeed;
}
}
//Custom drawing canvas (designed as inner class).
class DrawCanvas extends JPanel {
// Custom drawing codes
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.BLACK);
g.setColor(Color.BLUE);
g.fillOval(x, y, size, size); // draw a circle
//cards being drawn
card1 = new ImageIcon("Uno Cards/Blue/ EIGHT.png");
card1.paintIcon(this, g, 50, 100);
card2 = new ImageIcon("Uno Cards/Blue/FIVE.png");
card2.paintIcon(this, g, 100, 100);
card3 = new ImageIcon("Uno Cards/Blue/NINE.png");
card3.paintIcon(this, g, 150, 100);
card4 = new ImageIcon("Uno Cards/Blue/EIGHT.png");
card4.paintIcon(this, g, x, y);
//Graphics2D g2 = (Graphics2D) g; //we use this for drawing later.
//g2.fill(new drawImage());
//g2.fill(new Rectangle2D.Double(10,y,size,size));
}
}
// main program
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CustomGraphicsDemo2();
}
});
}
}
公共类CustomGraphicsDemo2扩展了JFrame{
//定义恒量
专用静态最终整型画布宽度=640;
专用静态最终整型画布高度=480;
//图像卡阵列
//自定义图形面板的控制柄
私人帆布;
私人图像图标卡1、卡2、卡3、卡4;
//图形对象的属性
私有int x=100;//x和y位置
私人int y=100;
私有整数大小=50;
private int xSpeed=3;//在x和y方向上的移动速度
私人互联网速度=5;
//构造函数来创建UI组件
公共自定义图形DEMO2(){
画布=新的DrawCanvas();
setPreferredSize(新维度(画布宽度、画布高度));
这个.setContentPane(画布);
此.setDefaultCloseOperation(关闭时退出);
这个包();
此.setTitle(“自定义图形演示”);
此.setVisible(true);
//定期刷新显示器。
//在自己的线程中运行显示刷新代码。
Thread updateThread=新线程(){
公开募捐{
while(true){
update();//更新(x,y)位置
repaint();//刷新JFrame,回调paintComponent()
试一试{
//延迟并给其他线程一个运行的机会
Thread.sleep(50);//毫秒
}catch(InterruptedException ex){}
}
}
};
updateThread.start();//回调运行()
}
//更新图形对象的(x,y)位置
公共无效更新(){
x+=x速度;
y+=y速度;
如果(x>CANVAS|u WIDTH-size | x<0){
xSpeed=-xSpeed;
}
如果(y>CANVAS|U高度-大小| y<0){
ySpeed=-ySpeed;
}
}
//自定义绘图画布(设计为内部类)。
类DrawCanvas扩展了JPanel{
//自定义图形代码
@凌驾
公共组件(图形g){
超级组件(g);
挫折背景(颜色:黑色);
g、 setColor(Color.BLUE);
g、 圆角椭圆(x,y,大小,大小);//画一个圆
//抽牌
card1=新的图像图标(“Uno Cards/Blue/EIGHT.png”);
卡片1.油漆图标(此,g,50,100);
card2=新的图像图标(“Uno Cards/Blue/FIVE.png”);
卡片2.油漆图标(此,g,100100);
card3=新的图像图标(“Uno Cards/Blue/NINE.png”);
卡片3.油漆图标(此,g,150,100);
card4=新图像图标(“Uno Cards/Blue/EIGHT.png”);
卡片4.油漆图标(此,g,x,y);
//Graphics2D g2=(Graphics2D)g;//我们在以后的绘图中使用它。
//g2.填充(新的drawImage());
//g2.填充(新矩形2D.双(10,y,大小,大小));
}
}
//主程序
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
新CustomGraphicsDemo2();
}
});
}
}
您可以将图像图标添加到JLabel中,然后将该标签添加到面板中
JLabel label = new JLabel(new ImageIcon(path));
panel.add(label);
更好的方法是为每个卡创建单独的组件。让CardUI扩展JComponent
class CardUI extends JComponent {
//... class members ...
public CardUI(BufferedImage cardPhoto){
this.cardPhoto= cardPhoto;
}
void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(cardPhoto, x, y, this);
}
}
编辑:
忘了提一下,现在您有了一个CardUI类,您可以向其中添加侦听器,以查看鼠标是否在此CardUI上。您可以将图像图标添加到JLabel,然后将该标签添加到面板
JLabel label = new JLabel(new ImageIcon(path));
panel.add(label);
更好的方法是为每个卡创建单独的组件。让CardUI扩展JComponent
class CardUI extends JComponent {
//... class members ...
public CardUI(BufferedImage cardPhoto){
this.cardPhoto= cardPhoto;
}
void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(cardPhoto, x, y, this);
}
}
编辑:
忘了提一下,现在您有了一个CardUI类,您可以向它添加侦听器,以查看鼠标是否在这个CardUI上。您可能应该使用一个而不是一个数组。您可能应该使用一个而不是一个数组。好的,那么您希望看到用户何时单击一张卡并执行一些操作。您需要向CardUI类添加鼠标侦听器 这是我制作的示例代码。两个班。一个是主面板:panel,另一个是CardUI,它是卡片的UI。后者显示一个图像。前者有一个空的布局管理器,否则卡片将不会显示在您希望它们显示的位置,而是布局管理器决定显示的位置 我使用的照片是今天的谷歌图片:我把它保存在C:\im.png上
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Panel extends JPanel {
private CardUI cui1, cui2;
public Panel(BufferedImage cardPhoto){
cui1= new CardUI(cardPhoto,10,10);
cui2= new CardUI(cardPhoto,10,60);
setLayout(null);//make sure you dont have a layout manager
this.add(cui1);
this.add(cui2);
}
public static void main(String[] args) {
BufferedImage cardPhoto;
Panel panel;
try {
cardPhoto= ImageIO.read(new File("c:/im.png"));
panel = new Panel(cardPhoto);
JFrame f = new JFrame("Card UI Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250,250);
f.getContentPane().add(panel);
f.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.border.LineBorder;
/**
* @author eamocanu
* This class is not thread safe
*/
public class CardUI extends JComponent implements MouseListener {
private BufferedImage cardPhoto;
private static int id=0;
private int myId; //so to name components for the purpose of this sample
public CardUI(BufferedImage cardPhoto, int x, int y) {
this.cardPhoto= cardPhoto;
myId= ++id;
setBorder(LineBorder.createGrayLineBorder());
setMaximumSize(new Dimension(cardPhoto.getWidth(),cardPhoto.getHeight()));
setMinimumSize(new Dimension(cardPhoto.getWidth(),cardPhoto.getHeight()));
//FYI: setting preferred size has different effects on various systems
setPreferredSize(new Dimension(cardPhoto.getWidth(),cardPhoto.getHeight()));
//move this component to the right location on the screen
//Note: this works if there is no layout manager to its parent container
setBounds(x, y, cardPhoto.getWidth(), cardPhoto.getHeight());
this.addMouseListener(this);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(cardPhoto, 0, 0, this);
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getX() >= 0 && e.getX()<cardPhoto.getWidth()
&& e.getY() >= 0 && e.getY()<cardPhoto.getHeight()){
System.out.println("Clicked Card: "+myId);
}
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("mouse entered "+myId);
}
@Override
public void mouseExited(MouseEvent arg0) {
System.out.println("mouse exited"+myId);
}
@Override
public void mousePressed(MouseEvent arg0) {}
@Override
public void mouseReleased(MouseEvent arg0) {}
}
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类面板扩展了JPanel{
私有卡用户界面cui1、cui2;
公共面板(BuffereImage cardPhoto){
cui1=新的CardUI(cardPhoto,10,10);
cui2=新的CardUI(cardPhoto,10,60);
setLayout(null);//确保没有布局管理器
添加(cui1);
添加(cui2);
}
公共静态void main(字符串[]args){
缓冲图像卡片照片;
专家组;
试一试{
cardPhoto=ImageIO.read(新文件(“c:/im.png”);
面板=新面板(卡片照片);
JFrame f=新JFrame(“卡UI演示”);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 设置大小(250250);
f、 getContentPane().add(面板);
f、 setVisible(真);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.awt.image.buffereImage;
导入javax.swing.JComponent;
导入javax.swing.border.LineBorder;
/**
*@eamocanu作者
*这个类不是线程安全的
*/
公共类CardUI扩展JC