1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import javax.swing.* ;
import java.awt.* ;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
class MaFenetre extends JFrame implements ActionListener,Runnable
{MenuBar menubar;
Menu menu1;
MenuItem menuitem0,menuitem1;
Thread thread;
MaFenetre ()
{menubar= new MenuBar();
menu1= new Menu("File");
menuitem0=new MenuItem("gray");
menu1.add(menuitem0);
menuitem0.addActionListener(this);
menuitem1=new MenuItem("End");
menu1.add(menuitem1);
menuitem1.addActionListener(this);
setTitle ("IMAGES") ;
setSize (800, 500) ;
pan = new Panneau() ;
getContentPane().add(pan) ;
menubar.add(menu1);
setMenuBar(menubar);
thread= new Thread(this);
}
private JPanel pan ;
java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
public void actionPerformed(ActionEvent event) {
}
public void run() {
}
java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
}
class Panneau extends JPanel
{MediaTracker tracker;
public Panneau()
{ tracker=new MediaTracker(this);
photo = getToolkit().getImage("image.jpg") ;
try{
tracker.waitForID(0);
if (tracker.isErrorAny()){
throw new IllegalArgumentException ("Images non chargees");
}
}catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public int height() { return photo.getHeight(null); }
public int width() { return photo.getWidth(null); }
// return Color of pixel (i, j)
public Color getColor(int i, int j) {
return new Color(((BufferedImage) photo).getRGB(i, j));
}
// return grayscale equivalent of pixel (i, j)
public int getGray(int i, int j) {
Color color = getColor(i, j);
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
int luminance = (int) (0.299*r + 0.587*g + 0.114*b); // NTSC formula
return luminance;
}
// set pixel (i, j) to given grayscale value
public void setGray(int i, int j, int c) {
Color color = new Color(c, c, c);
setColor(i, j, color);
}
private void setColor(int i, int j, Color c) {
((BufferedImage) photo).setRGB(i, j, c.getRGB());
}
public void paintComponent(Graphics g)
{ super.paintComponent(g) ;
int x=10, y=10 ;
if(photo != null)
{g.drawImage (photo, x, y, this);System.out.println("photo");}
}
private Image photo = null;
}
public class TestMenu
{ public static void main (String args[])
{ MaFenetre fen = new MaFenetre() ;
fen.setVisible(true) ;
}
} |
Partager