java - 这应该在数组中获取图像并在将其打印到框架之前调整其大小
问题描述
鳕鱼首先进入数组并获取文件名。然后它将其转换为缓冲图像并更改图像的大小。然后它将图片数组发送到 main 方法,在该方法中将其转换为 JLabel,然后再将其添加到帧中。我知道我的编程转换技能有点缺乏,但如果你能解释为什么当我运行它时图像没有调整大小,那就太好了
这是主要课程
import java.awt.Color;
import java.awt.Image;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Frame extends JFrame
{
public Frame(JLabel welcomeImage)
{
setTitle("Tower Defence Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(750 , 500);
setLocationRelativeTo(null);
setVisible(true);
getContentPane().setBackground(Color.BLACK);
add(welcomeImage);
}
public static JLabel WelcomeImage(Image[] pics)
{
pics[0].getScaledInstance(750, 1000, Image.SCALE_DEFAULT);
ImageIcon WelcomeImage = new ImageIcon(pics[0]);
JLabel JWelcomeImage = new JLabel();
JWelcomeImage.setIcon(WelcomeImage);
return JWelcomeImage;
}
public static void main(String[] args) throws IOException
{
Image[] pics = Images.Image();
JLabel welcomeImage = WelcomeImage(pics);
JFrame frame = new Frame(welcomeImage);
}
}
这是保存所有图像的类
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Images
{
public static Image[] Image() throws IOException
{
//Getting all the Picture in a png format
String Picture[] = {"apple.png"};
//Getting the Picture as an image
Image[] Pics = new Image[Picture.length];
BufferedImage iPic;
BufferedImage oPic;
//Using a for loop to put the Picture in an array
for(int i = 0; i < Picture.length; i++)
{
iPic = ImageIO.read(new File(Picture[i]));
oPic = new BufferedImage(750 , 500 , iPic.getType());
Pics[i] = (Image) oPic;
}
return Pics;
}
}
解决方案
如果你能解释为什么当我运行它时图像没有调整大小,那就太好了
嗯,第一件事是在尝试缩放图像之前学习如何以实际大小显示图像。如果您不知道如何显示默认图像,则无法显示缩放图像。
您的代码的问题是您甚至无法显示实际图像,因此尝试显示缩放图像没有机会工作。
考虑到这一点,您有几个结构性问题:
- 不要称你为“框架”类。有一个具有该名称的 AWT 组件,它令人困惑。类名应该更具描述性。
- 不要扩展 JFrame。只应在向类添加功能时扩展类。向框架添加组件不是添加功能,
- 在使框架可见之前,需要将组件添加到框架中。
- 不要使用 setSize()。帧大小和图像大小怎么能一样?框架包含标题栏和边框,因此它必须更大。相反,您应该
pack()
在 setVisible(...) 语句之前使用。 - 变量名不应以大写字符开头。始终如一。
- 方法名称应该是描述性的。“图像”方法有什么作用。没有人可以通过查看方法名称来猜测。也许像“readImages”或“loadImages”之类的?
在图像类中:
iPic = ImageIO.read(new File(Picture[i]));
oPic = new BufferedImage(750 , 500 , iPic.getType());
Pics[i] = (Image) oPic;
“oPic”变量有什么意义。此方法的目的是读取图像,而不是缩放图像。“oPic”变量只包含一个空的 BufferedImage。
所以摆脱那个变量,只需将“iPic”图像添加到数组中。
在框架类中:
pics[0].getScaledInstance(750, 1000, Image.SCALE_DEFAULT);
ImageIcon WelcomeImage = new ImageIcon(pics[0]);
上面的代码什么都不做,因为它getScaledInstance(...)
返回了一个新的 Image 实例。因此,您需要先将其分配给变量,然后才能使用它:
// pics[0].getScaledInstance(750, 1000, Image.SCALE_DEFAULT);
// ImageIcon WelcomeImage = new ImageIcon(pics[0]);
Image scaled = pics[0].getScaledInstance(750, 1000, Image.SCALE_DEFAULT);
ImageIcon WelcomeImage = new ImageIcon(scaled);
我建议您通过阅读Swing 教程来学习 Swing 基础知识。每个部分都包含示例,并且代码结构会更好。这些示例将向您展示如何在不扩展 JFrame 的情况下创建类。
推荐阅读
- php - 如何通过 query() 获取 wordpress 帖子并以 json 格式输出?
- javascript - 如何使提及的成员可以成为默认作者(Discord.js)
- javascript - 如何使用 Bootstrap 使谷歌地图 iFrame 响应?
- javascript - 生成包含溢出内容的pdf
- reactjs - Next.js:在 getInitialProps() 中获取数据之前加载的页面
- android - admob 的测试广告给出错误代码 3
- node.js - bcrypt 的数据和盐参数
- c++ - 如何指定 begin() 和 end() 迭代器的范围以在向量向量中查找子向量的第一个值?
- kotlin - 与 kotlins 流异步发出流值
- .net - 为什么我的单元测试项目看不到另一个项目的单一方法?[.NET 框架]