Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Non vedo bene la BufferedImage

    Ciao a tutti,
    ho un problema con una BufferedImage. Praticamente non riesco a visualizzarla con la stessa qualità dell'effettiva immagine. Come mai?
    Ecco il codice:

    // Load an image public BufferedImage loadImage(String path) throws IOException{
    JFrame frame = new JFrame("Image Loaded");
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new TextFilter());
    fileChooser.addChoosableFileFilter(new JavaCodeFilter());
    int risposta = fileChooser.showOpenDialog(null);
    if (risposta==fileChooser.APPROVE_OPTION) {
    try { img=ImageIO.read(fileChooser.getSelectedFile());
    Graphics imgg= img.createGraphics();
    ImagePanel imgp= new ImagePanel(img);
    imgp.paintComponent(imgg);
    frame.add(imgp);
    frame.setSize(W,H);
    imgp.repaint();
    frame.setVisible(true);
    }
    catch(IOException e)
    { } }
    return img; }

    Vi ringrazio.

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Non vedo bene la BufferedImage

    Originariamente inviato da Alimentos
    Praticamente non riesco a visualizzarla con la stessa qualità dell'effettiva immagine. Come mai?
    Innanzitutto il tuo codice, così come l'hai postato, è praticamente illeggibile.

    Comunque l'approccio è sbagliato .... non sei tu che devi invocare paintComponent.
    Dal codice si deduce che hai fatto una classe ImagePanel, bene: passi la immagine al costruttore (che la mette in una variabile di istanza) e nella classe bisogna fare l'override di paintComponent(), al cui interno disegni la immagine sul Graphics.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Sinceramente ho capito solo in parte cosa vuoi dire!:S

    Comunque il codice di ImagePanel è questo:

    import java.awt.image.BufferedImage;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;

    import javax.swing.JPanel;
    import javax.swing.ImageIcon;

    public class ImagePanel extends JPanel {
    Image img;

    public ImagePanel(BufferedImage img) {
    super();
    this.img = img;
    }

    public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
    }
    }

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da Alimentos
    public class ImagePanel extends JPanel {
    Image img;

    public ImagePanel(BufferedImage img) {
    super();
    this.img = img;
    }

    public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
    }
    }
    La classe è minimale ma tecnicamente è ok. Ripeto che non sei tu che devi invocare paintComponent().
    Devi solo istanziare ImagePanel passando la tua immagine al costruttore e poi, nota bene aggiungere questo tuo pannello dentro un "contenitore", nel tuo caso potresti metterlo nel content pane del JFrame.

    frame.getContentPane().add(tuoImagePanel);

    e lo mette nella parte CENTER del BorderLayout (layout manager di default di un JFrame).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Ho provato a modificare così:

    import java.awt.image.BufferedImage;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JPanel;


    public class ImagePanel extends JPanel {
    BufferedImage img;
    Graphics2D imgg;
    public ImagePanel(BufferedImage img) {
    super();
    this.img = img;
    imgg= this.img.createGraphics();
    paintComponent(imgg);
    }

    public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
    }
    }



    // Load an image
    public BufferedImage loadImage(String path) throws IOException{
    JFrame frame = new JFrame("Image Loaded");
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new TextFilter());
    fileChooser.addChoosableFileFilter(new JavaCodeFilter());
    int risposta = fileChooser.showOpenDialog(null);

    if (risposta==fileChooser.APPROVE_OPTION)
    {
    try
    {
    img=ImageIO.read(fileChooser.getSelectedFile());
    //Graphics imgg= img.createGraphics();
    ImagePanel imgp= new ImagePanel(img);
    //imgp.paintComponent(imgg);

    frame.add(imgp);
    frame.setSize(W,H);
    imgp.repaint();
    frame.setVisible(true);
    }
    catch(IOException e)
    {

    }
    }

    return img;
    }


    Però anche in questo caso mi fa la stessa cosa.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.