No java avançado num responderam…
Já li e reli, mas creio q nao entendo como funciona o componente…
preciso q no código abaixo, ele abra AMPLIADA a primeira imagem da lista. Que a primeira imagem seja aberta no centro da tela… pq da forma como estão, o usuário precisa clicar no icone na base da tela:
public class Visualizador extends JFrame {
private JLabel photographLabel = new JLabel();
private JToolBar buttonBar = new JToolBar();
String imagedir = "c:\\sp\\imagens\\";
private MissingIcon placeholderIcon = new MissingIcon();
/**
* List of all the descriptions of the image files. These correspond one to
* one with the image file names
*/
static String[] imageCaptions = null;
/**
* List of all the image files to load.
*/
static String[] imageFileNames = null;
/**
* Main entry point to the demo. Loads the Swing elements on the "Event
* Dispatch Thread".
*
* @param args
*/
public static void main(String args[]) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
//IconDemoApp app = new IconDemoApp();
//app.setVisible(true);
}
});
}
public boolean setFileNames(String[] names){
//String[] h = new String[2];
//h[0] = "Montanhas azuis.jpg";
//h[1] = "Ninféias.jpg";
imageFileNames = names;
for(int i=0;i<names.length; i++){
}
return true;
//imageFileNames = names;
}
public boolean setDescription(String[] des){
//String[] h = new String[2];
//h[0] = "Montanhas azuis.jpg";
//h[1] = "Ninféias.jpg";
imageCaptions = des;
return true;
//imageFileNames = names;
}
/**
* Default constructor for the demo.
*/
public void VisualizadorEXE() {
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("Visualizador de imagens");
// A label for displaying the pictures
photographLabel.setVerticalTextPosition(JLabel.BOTTOM);
photographLabel.setHorizontalTextPosition(JLabel.CENTER);
photographLabel.setHorizontalAlignment(JLabel.CENTER);
photographLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// We add two glue components. Later in process() we will add thumbnail buttons
// to the toolbar inbetween thease glue compoents. This will center the
// buttons in the toolbar.
buttonBar.add(Box.createGlue());
buttonBar.add(Box.createGlue());
add(buttonBar, BorderLayout.SOUTH);
add(photographLabel, BorderLayout.CENTER);
setSize(900, 750);
//setExtendedState(JFrame.MAXIMIZED_BOTH);
// this centers the frame on the screen
setLocationRelativeTo(null);
// start the image loading SwingWorker in a background thread
loadimages.execute();
this.repaint();
this.setVisible(true);
//this.pack();
}
/**
* SwingWorker class that loads the images a background thread and calls publish
* when a new one is ready to be displayed.
*
* We use Void as the first SwingWroker param as we do not need to return
* anything from doInBackground().
*/
private SwingWorker<Void, ThumbnailAction> loadimages =
new SwingWorker<Void, ThumbnailAction>() {
//boolean b = setFileNames();
/**
* Creates full size and thumbnail versions of the target image files.
*/
@Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < imageCaptions.length; i++) {
ImageIcon icon;
icon = createImageIcon(imagedir + imageFileNames[i], imageCaptions[i]);
ThumbnailAction thumbAction;
if(icon != null){
ImageIcon thumbnailIcon = new ImageIcon(getScaledImage(icon.getImage(), 64, 64));
thumbAction = new ThumbnailAction(icon, thumbnailIcon, imageCaptions[i]);
}
else{
// the image failed to load for some reason
// so load a placeholder instead
thumbAction = new ThumbnailAction(placeholderIcon, placeholderIcon, imageCaptions[i]);
}
publish(thumbAction);
}
// unfortunately we must return something, and only null is valid to
// return when the return type is void.
return null;
}
/**
* Process all loaded images.
*/
@Override
protected void process(List<ThumbnailAction> chunks) {
for (ThumbnailAction thumbAction : chunks) {
JButton thumbButton = new JButton(thumbAction);
// add the new button BEFORE the last glue
// this centers the buttons in the toolbar
buttonBar.add(thumbButton, buttonBar.getComponentCount() - 1);
}
}
};
/**
* Creates an ImageIcon if the path is valid.
* @param String - resource path
* @param String - description of the file
*/
protected ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = null;
//getClass().getClassLoader().getResource(path);
if (imgURL != null) {
// System.out.println(imgURL + ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; eis a url");
// return new ImageIcon(imgURL, description);
return null;
}
else {
System.out.println(path + "é o path");
ImageIcon iAntes = new ImageIcon(path, description);
ImageIcon i = new ImageIcon(getScaledImage(iAntes.getImage(), 600, 500));
return i;
}
}
/**
* Resizes an image using a Graphics2D object backed by a BufferedImage.
* @param srcImg - source image to scale
* @param w - desired width
* @param h - desired height
* @return - the new resized image
*/
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
/**
* Action class that shows the image specified in it's constructor.
*/
private class ThumbnailAction extends AbstractAction{
/**
*The icon if the full image we want to display.
*/
private Icon displayPhoto;
/**
* @param Icon - The full size photo to show in the button.
* @param Icon - The thumbnail to show in the button.
* @param String - The descriptioon of the icon.
*/
public ThumbnailAction(Icon photo, Icon thumb, String desc){
displayPhoto = photo;
// The short description becomes the tooltip of a button.
putValue(SHORT_DESCRIPTION, desc);
// The LARGE_ICON_KEY is the key for setting the
// icon when an Action is applied to a button.
putValue(LARGE_ICON_KEY, thumb);
}
/**
* Shows the full image in the main area and sets the application title.
*/
public void actionPerformed(ActionEvent e) {
photographLabel.setIcon(displayPhoto);
setTitle("Imagem: " + getValue(SHORT_DESCRIPTION).toString());
}
}
}
valeu ae!