public class PanelImage extends JPanel { private static final long serialVersionUID = -6350324456764768311L; private Image image; public PanelImage(Image image) { this.image = image; } @Override public void paintComponent(Graphics g){ if(image!=null){ Graphics2D g2d = (Graphics2D)g; g2d.drawImage(image, 0, 0, getWidth(), getHeight(), null); } } } public class GroupeImages extends JPanel { public GroupeImages(){ setLayout(null); } public void ajouterPanelImage(PanelImage image, int x, int y, int w, int h){ image.setBounds(x, y, w, h); add(image); } }
public class PanelImage extends JPanel { private static final long serialVersionUID = -6350324456764768311L; private Image image; public PanelImage(Image image) { this.image = image; addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e){ // Ecris ton code ici } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} }); } @Override public void paintComponent(Graphics g){ if(image!=null){ Graphics2D g2d = (Graphics2D)g; g2d.drawImage(image, 0, 0, getWidth(), getHeight(), null); } } }
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import javax.swing.JPanel; public class PanelImage extends JPanel { private static final long serialVersionUID = -6350324456764768311L; private Image image; public void setImage(Image image) { this.image = image; } public Image getImage() { return image; } @Override public void paintComponent(Graphics g){ if(image!=null){ Graphics2D g2d = (Graphics2D)g; //x, y, w, h représentent la position et la taille de l'image à dessiner g2d.drawImage(image, x, y, w, h, null); } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questiontry{Image image = ImageIO.read(getClass().getResource("image.jpg"));} catch{IOException e) {}
private PanelImage panel; panel.setImage(image);
try{Image image = ImageIO.read(getClass().getResource("image.jpg"));} catch{IOException e) {}
try{Image image = ImageIO.read(getClass().getResource("image.jpg"));} catch{IOException e) { e.printStackTrace(); }
public class Test { // instance variables - replace the example below with your own private PanelImage panel; private Image image; private Image image1; private JFrame myFrame; /** * Constructor for objects of class Test */ public void Test(Image image, Image image1,PanelImage panel) { this.panel=panel; this.image=image; this.image1=image1; myFrame = new JFrame(); } public Image getImage() { try{Image image = ImageIO.read(getClass().getResource("image.jpg"));} catch (IOException e) {} return image; } public void exemple() { panel.setImage(getImage(),50,50); panel.repaint(); myFrame.getContentPane().add(panel); } }
public static void main(String[] args){ JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // image est à définir f.add(new PanelImage(image)); f.setSize(500, 500); f.setLocationRelativeTo(null); f.setVisible(true); }
public class Test { public Image image; public Image getImage(){ try{image = ImageIO.read(getClass().getResource("image.jpg"));} catch (IOException e) {} return image; } public void main(String[] args){ JFrame f = new JFrame(); PanelImage p = new PanelImage(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.setImage(getImage(),50,50); p.repaint(); f.add(p); f.setSize(500, 500); f.setLocationRelativeTo(null); f.setVisible(true); } }