我已经开始学习Java,而我要做的第一件事就是将所有AutoIt程序都转换为Java。
我要转换的第一个程序是我创建的身份验证程序(基本上是社交媒体网站的密码保护程序)。我决定要做的第一件事是重新创建GUI。我已经设法绘制一个JFrame
并更改背景颜色以匹配AutoIt gui的背景颜色。下一步将是添加横幅。我这样做很麻烦。我正在寻找一种将图像添加到框架并能够使用像素移动图像的功能。
示例:(请注意,这不是真正的函数。我知道。)
addImageToGUI("myImage.jpg", 45, 35, 250, 500);
这样,我可以简单地通过更改功能参数中的数字来浏览图像。
下面是我到目前为止的代码。
// Imports
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
// Class. public class <nameOfFile>
public class GAC extends JPanel {
// Main class.
public static void main(String[] args) {
drawGUI ();
}
// Method to create GUI
public static void drawGUI() {
// Create a new JFrame and name it 'f'.
JFrame f = new JFrame("Griffin Account Cracker - Java Edition");
// Set the size of the new GUI.
f.setSize(600, 785);
// I don't know what this does.
f.add(new GAC());
// Tell the GUI to exit whenever the 'x' button is pressed.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String path = "Images/logo.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
f.getContentPane().add(label);
// Make the GUI visible.
f.setVisible(true);
}
// Method to set GUI's background color.
@Override
public void paint(Graphics f) {
String guiBanner = "Images/logo.jpg";
Image guiBannerImg = ImageIO.read(new File(guiBanner));
f.drawImage(guiBannerImg, 25, 25, null);
f.setColor(Color.decode("#A0A0A4"));
f.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
另外,有人会告诉我我代码的以下部分做什么吗?我是Java的新手。
f.add(new GAC());
任何建议,不胜感激!
最佳答案
f.add(new GAC())将面板添加到框架。在这种情况下,这不是绝对必要的,但是您必须做一些调整才能将其删除(例如使类扩展为框架而不是面板)。我将把讨论放在一边。
最简单的方法是在绘制方法中绘制横幅。更好的方法可能是创建一个新的自定义类扩展面板,将该类添加到您的框架中,然后在该类的paint方法中添加这些更改。我把它留给您-无论哪种方式,代码都是相似的。要获取图像:
String myPath = "somepath.gif";
Image myImage = ImageIO.read(new File(myPath));
下一步是绘制该图像,这也发生在paint()方法中:
g.drawImage(myImage, xPixel, yPixel, null);
希望这可以帮助!
编辑:完整代码:
import java.awt.*;
import javax.swing.*;
import java.io.File;
import javax.imageio.ImageIO;
// Class. public class <nameOfFile>
public class GAC extends JPanel {
// Main class.
public static void main(String[] args) {
drawGUI();
}
// Method to create GUI
public static void drawGUI() {
// Create a new JFrame and name it 'f'.
JFrame f = new JFrame("Griffin Account Cracker - Java Edition");
// Set the size of the new GUI.
f.setPreferredSize(new Dimension(600, 785));
// add a panel to the frame - the background image will be drawn on the panel
GAC t = new GAC();
t.setVisible(true);
f.add(t);
// Tell the GUI to exit whenever the 'x' button is pressed.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make the GUI visible.
f.setVisible(true);
f.pack();
f.repaint();
}
// Method to set GUI's background color.
@Override
public void paintComponent(Graphics f) {
//good practice to call this
super.paintComponent(f);
//color the background
f.setColor(Color.decode("#A0A0A4"));
f.fillRect(0, 0, this.getWidth(), this.getHeight());
//we need this try block to handle file reading errors
try {
//get the image from a file and scale it to the size you want
String guiBanner = "Images/Logo.jpg";
Image guiBannerImg = ImageIO.read(new File(guiBanner)).getScaledInstance(480, 270, Image.SCALE_SMOOTH);
//draw it at the position you want
f.drawImage(guiBannerImg, 25, 25, null);
} catch (Exception e) {
}
}
}