Afficher une image en java
Résolu/Fermé
Nicolasbc
Messages postés
92
Date d'inscription
mardi 14 février 2012
Statut
Membre
Dernière intervention
15 juin 2014
-
21 nov. 2012 à 22:58
Nicolasbc Messages postés 92 Date d'inscription mardi 14 février 2012 Statut Membre Dernière intervention 15 juin 2014 - 22 nov. 2012 à 03:29
Nicolasbc Messages postés 92 Date d'inscription mardi 14 février 2012 Statut Membre Dernière intervention 15 juin 2014 - 22 nov. 2012 à 03:29
A voir également:
- Afficher une image en java
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel football - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Image iso - Guide
- Java décompiler - Télécharger - Langages
2 réponses
KX
Messages postés
16755
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
12 février 2025
3 020
21 nov. 2012 à 23:43
21 nov. 2012 à 23:43
"avec des erreurs affichées dans la console"
Quelles erreurs ? Moi je n'en ai pas...
"le programme ne dépasse pas l'affichage de l'applet"
Tu ne fais rien d'autre ton code que l'affichage, que veux tu qu'il fasse de plus ?
Le serialVersionUID c'est un numéro de version pour la sérialisation, a priori tu ne t'en sers pas, tu peux donner une valeur bidon (1 par exemple)
Quelles erreurs ? Moi je n'en ai pas...
"le programme ne dépasse pas l'affichage de l'applet"
Tu ne fais rien d'autre ton code que l'affichage, que veux tu qu'il fasse de plus ?
Le serialVersionUID c'est un numéro de version pour la sérialisation, a priori tu ne t'en sers pas, tu peux donner une valeur bidon (1 par exemple)
import java.applet.Applet; import java.awt.Graphics; import javax.swing.ImageIcon; public class Exercice5 extends Applet { private static final long serialVersionUID = 1; private ImageIcon mon_image; public void init() { mon_image = new ImageIcon ("C:/..."); resize(mon_image.getIconWidth(),mon_image.getIconHeight()); } public void paint(Graphics g) { g.drawImage(mon_image.getImage(),0,0,null); } }
Nicolasbc
Messages postés
92
Date d'inscription
mardi 14 février 2012
Statut
Membre
Dernière intervention
15 juin 2014
86
Modifié par Nicolasbc le 22/11/2012 à 00:31
Modifié par Nicolasbc le 22/11/2012 à 00:31
Merci pour ta réponse en fait mon programme n'est pas celui la, j'ai juste copié des parties de celui pour ajouter une image.
Mon programme est:
package isn.projet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Horloge extends JFrame implements Runnable, KeyListener {
class EF extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE);}
}
Thread h;
public void run()
{
while(true)
{
repaint();
try{h.sleep(100);}
catch(InterruptedException e){}
}
}
String val;
int xb=50;
int yb=50;
int xb1=4;
int yb1=5;
int p=0;
int c1;
int z=0;
int ch=0;
int x1=5;
int y1=4;
int d;
int secondes=0,heures=0,minutes=0;
int x=275, y=315, taille=50;
int touche;
int rayon=240;
int x2=x-104;
String strsecondes, strminutes, strheures;
public void paint(Graphics g){
if (z==0){
d=(int)( Math.random()*8+1);
if (1<=d && d<=3){x1=-x1;}
if (d==1 || d==7 || d==8){y1=-y1;}
z=1;
d=(int)( Math.random()*8+1);
if (1<=d && d<=3){xb1=-xb1;}
if (d==1 || d==7 || d==8){yb1=-yb1;}
z=1;
}
c1=c1+1;
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800,600);
if (c1==10)
{
c1=0;
secondes++;
}
if (secondes==60){ secondes=0 ; minutes=minutes+1;}
if (minutes==60) { minutes=0 ; heures= heures+1; }
if (heures==24) {heures=0;}
x=x+x1; x2=x2+x1;
y=y+y1;
if (x<=15 || x>=800-280){x1=-x1;}
if (y<=75 || y>=600-23){y1=-y1;}
if ((xb>=x-25 && xb<x+10 && yb>=y-5 && yb<=y+28) || (xb<=x+287 && xb>=x+250 && yb>=y-76 && yb<=y)){x1=-x1;ch=101;}
if ((yb>=y-100 && yb<=y+65 && xb>=x && xb<=x+280) || (yb<=y+5 && yb>=y && xb>=x && xb<=x+280 && yb1+yb!=0)){y1=-y1;ch=102;}
if (touche==37 && p!=0) {p=p-1;x2=x2-104;}
if (touche==39 && p!=4) {p=p+1;x2=x2+104;}
if (touche!=0) {
if (p==1){
if (touche==38 && heures!=24) {heures=heures+1; }
if (touche==40 && heures!=0) {heures=heures-1; }
}
if (p==2) {
if (touche==38 && minutes!=60) {minutes=minutes+1; }
if (touche==40 && minutes !=0) {minutes=minutes-1; }
}
if (p==3) {
if (touche==38 && secondes!=60) {secondes=secondes+1; }
if (touche==40 && secondes!=0) {secondes=secondes-1;}
}
}
if (p==4) {p=0;x2=x2-4*105;}
if (p!=0){
g.setFont(new Font("Comic sans ms", Font.PLAIN, 90));
g.setColor(Color.BLUE);
g.drawString ("_",x2+5,y);
}
touche=0;
xb=xb+xb1;
yb=yb+yb1;
if (xb<=20 || xb>=800-25) {xb1=-xb1;}
if (yb<=20 || yb>=600-25){yb1=-yb1;}
if ((xb>=x-25 && xb<x+10 && yb>=y-5 && yb<=y+28) || (xb<=x+287 && xb>=x+250 && yb>=y-76 && yb<=y && xb1+xb!=0 && ch!=101)){xb1=-xb1;ch=103;}
if ((yb>=y-100 && yb<=y+65 && xb>=x && xb<=x+280) || (yb<=y+5 && yb>=y && xb>=x && xb<=x+280 && ch!=102)){yb1=-yb1;ch=104;}
g.setColor(Color.YELLOW);
g.fillOval(xb,yb,20,20);
g.setColor(Color.BLACK);
g.setFont(new Font("Comic sans ms", Font.PLAIN, taille));
g.setColor(Color.white);
if (ch>=100){
ch=100;
ch=(int)( Math.random()*7+1);
}
switch(ch){
case 1: g.setColor(Color.WHITE);
break;
case 2: g.setColor(Color.BLUE);
break;
case 3: g.setColor(Color.YELLOW);
break;
case 4: g.setColor(Color.GREEN);
break;
case 5: g.setColor(Color.RED);
break;
case 6: g.setColor(Color.CYAN);
break;
case 7: g.setColor(Color.ORANGE);
break;
}
if (heures<10) strheures="0"+heures;
else strheures=""+heures;
if (minutes<10) strminutes="0"+minutes;
else strminutes=""+minutes;
if (secondes<10) strsecondes="0"+secondes;
else strsecondes=""+secondes;
g.drawString(strheures+" : "+strminutes+" : "+strsecondes,x,y);
}
public Horloge(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new EF());
setVisible(true);
setSize(800,600);
setBackground(new Color(0,0,0));
addKeyListener(this);
h=new Thread(this);
h.start();
}
public static void main(String [] args){
Horloge HORLOGE=new Horloge();
}
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
touche=e.getKeyCode();
System.out.println(e.getKeyChar()+" : "+e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
Mon programme est:
package isn.projet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Horloge extends JFrame implements Runnable, KeyListener {
class EF extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{JOptionPane.showMessageDialog(null, "", "", JOptionPane.INFORMATION_MESSAGE);}
}
Thread h;
public void run()
{
while(true)
{
repaint();
try{h.sleep(100);}
catch(InterruptedException e){}
}
}
String val;
int xb=50;
int yb=50;
int xb1=4;
int yb1=5;
int p=0;
int c1;
int z=0;
int ch=0;
int x1=5;
int y1=4;
int d;
int secondes=0,heures=0,minutes=0;
int x=275, y=315, taille=50;
int touche;
int rayon=240;
int x2=x-104;
String strsecondes, strminutes, strheures;
public void paint(Graphics g){
if (z==0){
d=(int)( Math.random()*8+1);
if (1<=d && d<=3){x1=-x1;}
if (d==1 || d==7 || d==8){y1=-y1;}
z=1;
d=(int)( Math.random()*8+1);
if (1<=d && d<=3){xb1=-xb1;}
if (d==1 || d==7 || d==8){yb1=-yb1;}
z=1;
}
c1=c1+1;
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800,600);
if (c1==10)
{
c1=0;
secondes++;
}
if (secondes==60){ secondes=0 ; minutes=minutes+1;}
if (minutes==60) { minutes=0 ; heures= heures+1; }
if (heures==24) {heures=0;}
x=x+x1; x2=x2+x1;
y=y+y1;
if (x<=15 || x>=800-280){x1=-x1;}
if (y<=75 || y>=600-23){y1=-y1;}
if ((xb>=x-25 && xb<x+10 && yb>=y-5 && yb<=y+28) || (xb<=x+287 && xb>=x+250 && yb>=y-76 && yb<=y)){x1=-x1;ch=101;}
if ((yb>=y-100 && yb<=y+65 && xb>=x && xb<=x+280) || (yb<=y+5 && yb>=y && xb>=x && xb<=x+280 && yb1+yb!=0)){y1=-y1;ch=102;}
if (touche==37 && p!=0) {p=p-1;x2=x2-104;}
if (touche==39 && p!=4) {p=p+1;x2=x2+104;}
if (touche!=0) {
if (p==1){
if (touche==38 && heures!=24) {heures=heures+1; }
if (touche==40 && heures!=0) {heures=heures-1; }
}
if (p==2) {
if (touche==38 && minutes!=60) {minutes=minutes+1; }
if (touche==40 && minutes !=0) {minutes=minutes-1; }
}
if (p==3) {
if (touche==38 && secondes!=60) {secondes=secondes+1; }
if (touche==40 && secondes!=0) {secondes=secondes-1;}
}
}
if (p==4) {p=0;x2=x2-4*105;}
if (p!=0){
g.setFont(new Font("Comic sans ms", Font.PLAIN, 90));
g.setColor(Color.BLUE);
g.drawString ("_",x2+5,y);
}
touche=0;
xb=xb+xb1;
yb=yb+yb1;
if (xb<=20 || xb>=800-25) {xb1=-xb1;}
if (yb<=20 || yb>=600-25){yb1=-yb1;}
if ((xb>=x-25 && xb<x+10 && yb>=y-5 && yb<=y+28) || (xb<=x+287 && xb>=x+250 && yb>=y-76 && yb<=y && xb1+xb!=0 && ch!=101)){xb1=-xb1;ch=103;}
if ((yb>=y-100 && yb<=y+65 && xb>=x && xb<=x+280) || (yb<=y+5 && yb>=y && xb>=x && xb<=x+280 && ch!=102)){yb1=-yb1;ch=104;}
g.setColor(Color.YELLOW);
g.fillOval(xb,yb,20,20);
g.setColor(Color.BLACK);
g.setFont(new Font("Comic sans ms", Font.PLAIN, taille));
g.setColor(Color.white);
if (ch>=100){
ch=100;
ch=(int)( Math.random()*7+1);
}
switch(ch){
case 1: g.setColor(Color.WHITE);
break;
case 2: g.setColor(Color.BLUE);
break;
case 3: g.setColor(Color.YELLOW);
break;
case 4: g.setColor(Color.GREEN);
break;
case 5: g.setColor(Color.RED);
break;
case 6: g.setColor(Color.CYAN);
break;
case 7: g.setColor(Color.ORANGE);
break;
}
if (heures<10) strheures="0"+heures;
else strheures=""+heures;
if (minutes<10) strminutes="0"+minutes;
else strminutes=""+minutes;
if (secondes<10) strsecondes="0"+secondes;
else strsecondes=""+secondes;
g.drawString(strheures+" : "+strminutes+" : "+strsecondes,x,y);
}
public Horloge(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new EF());
setVisible(true);
setSize(800,600);
setBackground(new Color(0,0,0));
addKeyListener(this);
h=new Thread(this);
h.start();
}
public static void main(String [] args){
Horloge HORLOGE=new Horloge();
}
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
touche=e.getKeyCode();
System.out.println(e.getKeyChar()+" : "+e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
Nicolasbc
Messages postés
92
Date d'inscription
mardi 14 février 2012
Statut
Membre
Dernière intervention
15 juin 2014
86
22 nov. 2012 à 00:32
22 nov. 2012 à 00:32
J'ai essayé d'ajouter l'image puis je l'ai suprimée car ça ne marchait pas
KX
Messages postés
16755
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
12 février 2025
3 020
22 nov. 2012 à 01:00
22 nov. 2012 à 01:00
Il y a beaucoup de choses à dire de ton code, déjà ce n'est pas une Applet comme tout à l'heure, mais une JFrame, ensuite je trouve qu'il y a beaucoup de calculs fait à chaque paint() et ça rame, je pense que la majorité de ces calculs devraient être fait dans ta boucle while lorsque tu sors de ton Thread.sleep, juste avant le repaint, ça devrait alléger les calculs.
Bref, ce n'est pas vraiment le problème, dans ton paint tu as ceci :
Or je pense que c'est là que tu as un problème, tu devrais enlever ces lignes pour mettre l'affichage de ton image à la place :
Et bien sûr ne pas oublier de charger l'image dans ton constructeur Horloge :
Bref, ce n'est pas vraiment le problème, dans ton paint tu as ceci :
g.setColor(Color.BLACK); g.fillRect(0, 0, 800,600);
Or je pense que c'est là que tu as un problème, tu devrais enlever ces lignes pour mettre l'affichage de ton image à la place :
g.drawImage(mon_image.getImage(),0,0, null);
Et bien sûr ne pas oublier de charger l'image dans ton constructeur Horloge :
mon_image = new ImageIcon ("C:/...");
Nicolasbc
Messages postés
92
Date d'inscription
mardi 14 février 2012
Statut
Membre
Dernière intervention
15 juin 2014
86
22 nov. 2012 à 01:04
22 nov. 2012 à 01:04
merci! Je vais tester ça!
.....
.....
Nicolasbc
Messages postés
92
Date d'inscription
mardi 14 février 2012
Statut
Membre
Dernière intervention
15 juin 2014
86
22 nov. 2012 à 01:07
22 nov. 2012 à 01:07
Qu'est-ce que le constructeur horloge?
KX
Messages postés
16755
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
12 février 2025
3 020
22 nov. 2012 à 01:10
22 nov. 2012 à 01:10
public Horloge() { mon_image = new ImageIcon ("C:/..."); // ... }