gpt4 book ai didi

java - 如何使用 imageArray 将每个单词的图像转换为另一个单词

转载 作者:行者123 更新时间:2023-12-02 03:43:58 26 4
gpt4 key购买 nike

我制作了 5 个 Java 类,它们的目标是将英语翻译成法语,并设置当您单击 J 按钮时在 Jfield1 中写入的每个单词的图像。我的小程序可以将 JField1 中的每个英文单词翻译成 JField2 中的法语单词。我添加了两个类(ImageImplement 和 ImageInFrame)来设置图像但不幸的是没有任何作用。而且我真的不知道如何使用imageArray。请帮助我!!!

抱歉我的英语不太好。

这是我的小程序

我的第一个类是 StringArrayEnglishWord

公共(public)类 StringArrayEnglishWords 扩展 JPanel {

String[] names;
Graphics2D g2d;

public StringArrayEnglishWords(){
EnglishWords();
// drawNames();
}

public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}

public void EnglishWords(){

names=new String[16];
names[0]="Tomatoes";
names[1]="Chairs";
names[2]="Car";
names[3]="Computer";
names[4]="Flowers";
names[5]="Family";
names[6]="Sister";
names[7]="Husband";
names[8]="God";
names[9]="Book";
names[10]="Watch";
names[11]="Pencil";
names[12]="Bottle";
names[13]="Map";
names[14]="Wife";
names[15]="Bag";


}

public void drawNames(){
for (int i=0; i<names.length; i++){
g2d.drawString(names[i], 50, (1+i)*16);
}
}

public int indexOfWord(String userInput)
{
for(int i=0;i<names.length;i++)
{
if(userInput.equals(names[i])){return i;}

}
return -1;
}

}

我的第二个类(class)是 StringArrayFrenchWords:

公共(public)类 StringArrayFrenchWords 扩展 JPanel {

String[] names2;
Graphics2D g2d;

public StringArrayFrenchWords(){
FrenchWorld();
// drawNames();

}

public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}

public void FrenchWorld(){

names2=new String[16];
names2[0]="Tomate";
names2[1]="Chaise";
names2[2]="Voiture";
names2[3]="Ordinateur";
names2[4]="Fleurs";
names2[5]="Familles";
names2[6]="Soeur";
names2[7]="Epoux, Mari";
names2[8]="Dieu";
names2[9]="Livre";
names2[10]="Montre, Regarder";
names2[11]="Crayon";
names2[12]="Bouteille";
names2[13]="Carte,plan";
names2[14]="Femmme";
names2[15]="Sac";


}
public void drawNames(){
for (int i=0; i<names2.length; i++){
g2d.drawString(names2[i], 50, (1+i)*16);
}
}

}

我的第三个类(class)是扩展 Japplet(翻译者):

公共(public)类 Translator 扩展 JApplet 实现 ActionListener {

String StringField1, StringImage;
StringArrayEnglishWords Englishw;
StringArrayFrenchWords Frenchw;

ImageInJframe img;
ImageImplement Images;
Image Image;

JButton[] buttons;
JComboBox[] comboBoxes;
JButton TranslateButton;


JPanel JPanel0,JPanel1;
JPanel buttonPanel;
JPanel comboBoxesPanel;
JTextField field1,field2;
ImageIcon Imageicon;
JLabel ImageLabel;


public void init(){

setUpFields();

Englishw=new StringArrayEnglishWords();
Frenchw=new StringArrayFrenchWords();
img = new ImageInJframe(); // this was for set up the image
setUpimages();// for setup image too
}


public void setUpFields() {
TranslateButton = new JButton("TranslateButton");
field1 = new JTextField(10);

field2 = new JTextField(10);
field2.setEditable(false);


JPanel0 = new JPanel(new FlowLayout());

JPanel0.add(field1);

TranslateButton.addActionListener(this);

field1.addActionListener(this);

field2.addActionListener(this);

JPanel0.add(TranslateButton);

JPanel0.add(field2);


add(JPanel0, BorderLayout.SOUTH);

}
public void paint(Graphics g) {
super.paint(g);

Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);


}


public void setUpimages() {//setUp images too

ImageLabel.setIcon((Icon) img);

JPanel1 = new JPanel(new FlowLayout());


JPanel1.add(ImageLabel);

add(JPanel1, BorderLayout.CENTER);


}


@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == TranslateButton) {

System.out.println(field1);
StringField1 = field1.getText();

int index = Englishw.indexOfWord(StringField1);//this will give you the index of the word
if (index == -1) {
field2.setText("Word not found.");
System.out.println(field2);

} else {
field2.setText(Frenchw.names2[index]);
System.out.println("English Words=" + field1.getText());
System.out.println("French Words =" + field2.getText());

//setup Image
Image = img.getIconImage();
int index2 = img.indexOfImage(StringImage);
JPanel1.add(img);


}
}
}

}

我的两个类用于使用 JFame 和 imageArray 设置图像:ImageInJframe 和 ImageImplement

公共(public)类 ImageInJframe 扩展 JFrame {

JFrame Frame;


Image[] pics;
String[] names = {"apple.jpg", "bags.jpg", "bathroom.jpg", "battled.jpg", "car.png",
"chairs.jpg", "Computer.jpg", "family.jpg", "flowers.jpg", "god.jpg", "house.png", "map.jpg"
, "men.jpg", "pencil.jpg", "sisters.jpg", "tomato.jpg", "watch.jpg", "women.jpg", "book.gif"};
final int NUM_PICS=names.length;
Graphics g;


public static void main(String args[])
{
new ImageInJframe().start();
}


public void start()
{
ImageImplement panel = new ImageImplement(new ImageIcon(String.valueOf(pics)).getImage());
add(panel);
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setImage();
}

public void setImage() {


pics = new Image[NUM_PICS];

}


public int indexOfImage(String userInput)
{
for(int i=0;i<pics.length;i++)
{
if(userInput.equals(pics[i])){return i;}

}
return -1;
}

}

ImageImplement 扩展 JPanel:

公共(public)类 ImageImplement 扩展 JPanel {

Image img;


public ImageImplement(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}

public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}

最佳答案

当您的 paintComponent 时,绘画会按需发生。被调用时,您需要重新绘制 UI 的当前状态。也许您应该首先看一下 Painting in AWT and SwingPerforming Custom Painting有关绘画工作原理的更多详细信息

基本上,来自您的paintComponent ,您需要调用draw ,但您应该将 Graphics 传递给它传递给 paintComponent 的上下文方法。维持对 Graphics 的引用是危险且不明智的。您没有创建的上下文

Text

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new StringArrayEnglishWords());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class StringArrayEnglishWords extends JPanel {

String[] names;

public StringArrayEnglishWords() {

EnglishWords();

}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawNames(g2d);
g2d.dispose();
}

public void EnglishWords() {

names = new String[16];
names[0] = "Tomatoes";
names[1] = "Chairs";
names[2] = "Car";
names[3] = "Computer";
names[4] = "Flowers";
names[5] = "Family";
names[6] = "Sister";
names[7] = "Husband";
names[8] = "God";
names[9] = "Book";
names[10] = "Watch";
names[11] = "Pencil";
names[12] = "Bottle";
names[13] = "Map";
names[14] = "Wife";
names[15] = "Bag";

}

public void drawNames(Graphics2D g2d) {
for (int i = 0; i < names.length; i++) {
g2d.drawString(names[i], 50, (1 + i) * 16);
}
}

public int indexOfWord(String userInput) {
for (int i = 0; i < names.length; i++) {
if (userInput.equals(names[i])) {
return i;
}

}
return -1;
}

}
}

修改Graphics时也应该小心。上下文中,它由同一窗口内的所有组件共享,在大多数情况下,我发现拍摄 Graphics 的快照很有用。 context ( Graphics#create ) 并使用它,那么我不需要记住在完成后重置它。

小程序也是一种死技术,该插件已被弃用,不再受支持,并且在大多数情况下,被大多数浏览器主动阻止。请参阅Java Plugin support deprecatedMoving to a Plugin-Free Web了解更多详情

已更新

改变...

ImageImplement panel = new ImageImplement(new ImageIcon(String.valueOf(pics)).getImage());
add(panel);

到...

JLabel panel = new JLabel(new ImageIcon(names[0]).getImage());
add(panel);

假设图像文件位于执行程序的当前目录中,那么它应该可以工作。如果它们包含在带有类文件的包中,那么您需要使用 Class#getResource获取对图像的引用并传递结果 URLImageIcon

您的ImageImplement面板没有执行任何操作,因此即使它有正确的图像可以显示,它也不会显示任何内容。

String.valueOf(pics)完全没有意义

您发布了一大堆看似不相关的代码,很难知道您指的是什么问题

这个...

ImageLabel.setIcon((Icon) img);

毫无意义,imgImageInJFrame 的一个实例,这与 Icon 有什么关系呢? ?

我完全不知道你想做什么

关于java - 如何使用 imageArray 将每个单词的图像转换为另一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514359/

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