• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i use my ImageArray extend JPanel class into another class extend JApplet

 
manuella djoufack
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My programmer was to make a program that translate each word you enter into another languages and shows the image of each word enter. The translator of English words into french Words by using Jfield and StringArray works. But i got a big problem for setting each images. I don't know how to call them in JApplet Class.

The goal is to use ImageArray class for setup each image corresponding to each world enter in Jfield.

THis is my ImageArray class:


public class ImageArray extends JPanel implements ActionListener {


Image[] pics;

String[] names;

int NUM_PICS;

JPanel controls, imagePanel;
JButton randomize;

Image currentImage;

ImageIcon icon;
JLabel imageLabel;



public ImageArray() {



setImages();
setLayout(new BorderLayout());

loadImages();

currentImage=pics[0];

setUpControls();
setUpImagePanel();




}





public void setUpControls(){



controls=new JPanel(new FlowLayout());
randomize=new JButton("randomize");
randomize.addActionListener(this);
controls.add(randomize);
add(controls, BorderLayout.SOUTH);


}



public void setUpImagePanel(){



imagePanel=new JPanel(new FlowLayout());
imageLabel=new JLabel();
imageLabel.setHorizontalAlignment(JLabel.CENTER);

icon=new ImageIcon();
icon.setImage(currentImage);
imageLabel.setIcon(icon);

imagePanel.add(imageLabel);

add(imagePanel, BorderLayout.CENTER);



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


}



public void setImages(){

pics = new Image[NUM_PICS];
NUM_PICS=names.length;


names[0]= "apple.jpg";
names[1]= "bags.jpg";
names[2] = "bathroom.jpg";
names[3] = "battled.jpg";
names[4]= "car.png";
names[5]= "chairs.jpg";
names[6] = "Computer.jpg";
names[7] = "family.jpg";
names[8] = "flowers.jpg";
names[9] = "god.jpg";
names[10] = "house.png";
names[11] = "map.jpg";
names[12] = "men.jpg";
names[13] = "pencil.jpg";
names[14] = "sisters.jpg";
names[15] = "tomato.jpg";
names[16] = "watch.jpg";
names[17] = "women.jpg";
names[18] ="book.gif";

}



public void loadImages(){



for(int i=0; i<names.length; i++){

currentImage=pics[NUM_PICS];
icon.setImage(currentImage);
repaint();
}




}




public int indexOfImages(ImageIcon userInput)

{



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

}
return -1;
}




@Override
public void actionPerformed(ActionEvent e) [color=violet]{
[/color]
Object button=e.getSource();

if(button==randomize){
int random=(int)(Math.random()*pics.length);
currentImage=pics[random];
icon.setImage(currentImage);
repaint();
}
}


}
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have this line in your paintComponent override:

That changes the background of the JPanel, or what ever component you are using, so all you have to do is:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!