//SimpleImage3CutDown.java //jdk1.5.0.01 import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.*; import javax.swing.filechooser.*; class Simpleimage3CutDown extends JFrame //implements ActionListener { //image1 held in BufferedImage BufferedImage image1; //displayImage declared to hold BufferedImage BufferedImage displayImage; //Declare string variable named directory String directory; //Declare string variable named filename String filename; //Declare myExit as a JMenuItem JMenuItem myExit; //String title declared in Constructor public Simpleimage3CutDown (String title){ //accesses super class Jframe, passes title super (title); //default close operation setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //call method setMenu()to create the menu setMenu(); //draw an innerJPanel JPanel myDrawJPanel=new InnerJPanel(); //get the contentPane and name it cp Container cp=getContentPane(); //add the new InnerJPanel, with design parameters cp.add(myDrawJPanel,BorderLayout.CENTER); // a prewritten method to work with JPanel pack (); //setResizable (false);? //Visible setVisible (true); } //create menu private void setMenu() { JMenuBar mBar=new JMenuBar(); super.setJMenuBar(mBar); //Create menu header FILE JMenu fileMenu=new JMenu("File"); fileMenu.setMnemonic('F'); mBar.add(fileMenu); //Create Open under FILE JMenuItem loadItem= new JMenuItem("Open"); loadItem.setMnemonic('O'); fileMenu.add(loadItem); loadItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ displayImage=image1=myOpenx(); //to help with a histogram below new MyFloatingJFrame(image1); repaint(); return; } } ); //Create Revert under FILE JMenuItem revertItem= new JMenuItem("Revert"); revertItem.setMnemonic('R'); fileMenu.add(revertItem); revertItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("revert"); displayImage=image1; repaint(); //invalidate(); //invaldate component as we have it in a frame return; } } ); //End of File/Open/Revert //Create Filters Header menu JMenu filterMenu=new JMenu("XFilters"); filterMenu.setMnemonic('X'); mBar.add(filterMenu); //Menu-slowBlur JMenuItem blurItem= new JMenuItem("slowBlur"); loadItem.setMnemonic('B'); filterMenu.add(blurItem); blurItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("blur filter "); //display image displayImage=slowBlurBufImage(displayImage); //refresh the screen repaint(); //invalidate(); return; } } ); //Menu-FastBlur JMenuItem fastBlur= new JMenuItem("FastBlur"); loadItem.setMnemonic('A'); filterMenu.add(fastBlur); fastBlur.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("Fast Blur "); displayImage=fastBlurBufImage(displayImage); repaint(); return; } } ); //Menu-Remove Red JMenuItem remRed= new JMenuItem("Remove Red"); loadItem.setMnemonic('L'); filterMenu.add(remRed); remRed.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("red filter "); //displayImage=slowBlurBufImage(displayImage); displayImage=redBlurBufImage(displayImage); repaint(); //invalidate(); return; } } ); //Menu-Remove Green JMenuItem remGreen= new JMenuItem("Remove Green"); loadItem.setMnemonic('L'); filterMenu.add(remGreen); remGreen.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("green filter "); //displayImage=slowBlurBufImage(displayImage); displayImage=greenBlurBufImage(displayImage); repaint(); return; } } ); //Menu-Remove Blue JMenuItem remBlue= new JMenuItem("Remove Blue"); loadItem.setMnemonic('L'); filterMenu.add(remBlue); remBlue.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("blue filter "); displayImage=blueBlurBufImage(displayImage); repaint(); return; } } ); //Menu-Display Grey JMenuItem changeGrey= new JMenuItem("Display Grey"); loadItem.setMnemonic('E'); filterMenu.add(changeGrey); changeGrey.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("change grey "); displayImage=greyBlurBufImage(displayImage); repaint(); return; } } ); //Menu-Invert Image JMenuItem invertImage= new JMenuItem("Invert Image"); loadItem.setMnemonic('I'); filterMenu.add(invertImage); invertImage.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("Invert image "); displayImage=invertBlurBufImage(displayImage); repaint(); return; } } ); //Menu-Halve Red JMenuItem halveRed= new JMenuItem("Halve Red"); loadItem.setMnemonic('H'); filterMenu.add(halveRed); halveRed.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("halve red "); displayImage=halveRedBlurBufImage(displayImage); repaint(); return; } } ); } //end set menu //get preferred size //set the dimension to 300, 400, else get width and height and return those measurements public Dimension getPreferredSize (){ Dimension ps; if(displayImage==null){ ps = new Dimension (300,400); } else{ Image im=displayImage; Insets in = getInsets(); //insets(); ps = new Dimension (im.getWidth (this) + in.left + in.right, im.getHeight (this) + in.top + in.bottom); } return ps; } //deals with open //uses file chooser //provides file dialogue box //gets the file //puts image in im //returns im as mygetBufferedImage //or, returns null private BufferedImage myOpenx(){ JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getAbsolutePath()); Image im=new javax.swing.ImageIcon( chooser.getSelectedFile().getAbsolutePath()).getImage(); //this is a blocking function dangerous on the internet return mygetBufferedImage(im); } return null; } //buffered image im //bi = new Buffered Image //create graphics, draw and return bi private BufferedImage mygetBufferedImage(Image im){ BufferedImage bi = new BufferedImage (im.getWidth (this), im.getHeight (this), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bi.createGraphics (); g2.drawImage (im, 0, 0, this); return bi; } //START OF SLOWBLURBUFIMAGE private BufferedImage slowBlurBufImage(BufferedImage src) { if(src==null){return null;} //print start slowBlueBufIMage //Time now //get width //get height //put image into new BufferedImaged named dst System.out.println("start slowBlurBufImage"); long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); //initialise alph, rs, gs, bs, argb //intitalse r g b int alpha, rs, gs, bs, argb; int r,g,b; //row is height //col is width //loop through h1, then width from 1 to total width less 1 //loop through h2, then width from 1 to total width less 1 //continue until reached full height and full width less 1 for (int h = 1; h < height-1; h++){ for (int w = 1; w < width-1; w++){ //set the variables that hold the sum to 0 alpha=rs=gs=bs=0; //these two INNERMOST LOOPS run three times each for(int hh=-1; hh<=1; hh++) { for(int ww=-1; ww<=1; ww++) { //int getRGB(int x, int y); //why the two w + ww? and h+hh? why not ww and hh? argb=src.getRGB(w + ww, h +hh);//3.65 seconds //move alpha over 3 octets and mask alpha= (argb>>24) & 0xff; //move the red over 2 octets and mask r = (argb >> 16) & 0xff; //move the green over 1 octet and mask g = (argb >> 8) & 0xff; //move the blue over not at all, mask b = argb & 0xff; rs += r; gs += g; bs += b; //rs must = 0, plus the value of r //gs must = 0 plus the value of g //bs must = 0 plus the value of b }//END OF ww }//END OF hh //rs which should be the total is divided by 9 //as is gs and bs rs /= 9; gs /= 9; bs /= 9; //now we have newColor which is being put back //where is the mask? //why are we having int x int y //new colour is here with OR statement int newColor=alpha<<24 |rs<<16| gs<<8| bs; //void setRGB(int x, int y, int rgb) //dst is the image //set the rgb, width height and new colour dst.setRGB (w, h, newColor);//3.6sec }//END OF w }//END OF h //calculate the time it has taken long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished slowBlurBufImage"); return dst; //return dst }//END OF private BufferedImage SlowBlurBufImage //START OF BufferedImage fastBlurBufImage private BufferedImage fastBlurBufImage(BufferedImage src) { if(src==null){return null;} System.out.println("start fastBlurBufImage"); //set time long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back //get rgb int[] arrayIn=src.getRGB(0,0,width,height,null,0,width); //put new buffered image into dst BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); //pur the rgb into arrayOut int[] arrayOut=dst.getRGB(0,0,width,height,null,0,width); //initialise to hold totals int alpha, rs, gs, bs, argb; //initialise rgb int r,g,b; //row is height //col is width //start loop height for (int h = 1; h < height-1; h++){ //start loop width for (int w = 1; w < width-1; w++){ //set variables to hold totals = 0 alpha=rs=gs=bs=0; //inner loop for 3 x 3 area starting with height for(int hh=-1; hh<=1; hh++) { //inner, inner loop to hold the width to make the 3 x 3 area for(int ww=-1; ww<=1; ww++) { //read in each argb argb=arrayIn[(h*width)+(hh*width) +w + ww];//1.24seconds //bit shift the colour and mask alpha= (argb>>24) & 0xff; r = (argb >> 16) & 0xff; g = (argb >> 8) & 0xff; b = argb & 0xff; //total each r g b and place in rs, gs or bs rs += r; gs += g; bs += b; }//END OF ww }//END OF hh //perform the blur rs /= 9; gs /= 9; bs /= 9; //put the altered color back arrayOut[w+(h*width)]=alpha<<24|rs<<16|gs<<8|bs; }//END OF w }//END OF h //set the rgb dst.setRGB(0,0,width,height,arrayOut,0,width);//1.24seconds //time taken long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished fastBlurBufImage"); //return the image dst return dst; }//END OF fastBlurBufImage //START OF remove red private BufferedImage redBlurBufImage(BufferedImage src) { if(src==null){return null;} System.out.println("start redBlurBufImage"); long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back int[] arrayIn=src.getRGB(0,0,width,height,null,0,width); BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); //pur the rgb into arrayOut int[] arrayOut=dst.getRGB(0,0,width,height,null,0,width); //initialise to hold totals int alpha, rs, gs, bs, argb; //initialise rgb int r,g,b; //start loop height for (int h = 1; h < height-1; h++){ //start loop width for (int w = 1; w < width-1; w++){ //loop through and set variables to hold totals to = 0 alpha=rs=gs=bs=0; //inner loop for 3 x 3 area starting with height for(int hh=-1; hh<=1; hh++) { //inner, inner loop to hold the width to make the 3 x 3 area for(int ww=-1; ww<=1; ww++) { //read in each argb argb=arrayIn[(h*width)+(hh*width) +w + ww];//1.24seconds //bitshift the argb and mask alpha= (argb>>24)& 0xff; r = (argb >> 16) & 0xff; g = (argb >> 8) & 0xff; b = argb & 0xff; //add each r in the loop to rs, g to gs and b to bs rs += r; gs += g; bs += b; }//END OF ww }//END OF hh //perform the blur by dividing the 9 values by 9 to create the blur effect rs /= 9; gs /= 9; bs /= 9; //put the altered color back //rs bitshift is set to 0 to reflect no red in the image arrayOut[w+(h*width)]=alpha<<24|gs<<8|bs; }//END OF w }//END OF h //set the rgb dst.setRGB(0,0,width,height,arrayOut,0,width);//1.24seconds //time taken long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished redBlurBufImage"); //return the image dst return dst; }//END of remove red //START OF remove green private BufferedImage greenBlurBufImage(BufferedImage src) { if(src==null){return null;} System.out.println("start greenBlurBufImage"); long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back int[] arrayIn=src.getRGB(0,0,width,height,null,0,width); BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); int[] arrayOut=dst.getRGB(0,0,width,height,null,0,width); int alpha, rs, gs, bs, argb; int r,g,b; for (int h = 1; h < height-1; h++){ for (int w = 1; w < width-1; w++){ alpha=rs=gs=bs=0; for(int hh=-1; hh<=1; hh++) { for(int ww=-1; ww<=1; ww++) { argb=arrayIn[(h*width)+(hh*width) +w + ww];//1.24seconds alpha= (argb>>24) & 0xff; r = (argb >> 16) & 0xff; g = (argb >> 8) & 0xff; b = argb & 0xff; rs += r; gs += g; bs += b; }//END OF ww }//END OF hh rs /= 9; gs /= 9; bs /= 9; //put the altered color back (green removed) arrayOut[w+(h*width)]=alpha<<24|rs<<16|bs; }//END OF w }//END OF h dst.setRGB(0,0,width,height,arrayOut,0,width);//1.24seconds long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished greenBlurBufImage"); return dst; } //END OF remove green //START OF remove blue private BufferedImage blueBlurBufImage(BufferedImage src) { if(src==null){return null;} System.out.println("start blueBlurBufImage"); long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back int[] arrayIn=src.getRGB(0,0,width,height,null,0,width); BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); int[] arrayOut=dst.getRGB(0,0,width,height,null,0,width); int alpha, rs, gs, bs, argb; int r,g,b; for (int h = 1; h < height-1; h++){ for (int w = 1; w < width-1; w++){ alpha=rs=gs=bs=0; for(int hh=-1; hh<=1; hh++) { for(int ww=-1; ww<=1; ww++) { argb=arrayIn[(h*width)+(hh*width) +w + ww];//1.24seconds alpha= (argb>>24) & 0xff; r = (argb >> 16) & 0xff; g = (argb >> 8) & 0xff; b = argb & 0xff; rs += r; gs += g; bs += b; }//END OF ww }//END OF hh rs /= 9; gs /= 9; bs /= 9; //blue removed here arrayOut[w+(h*width)]=alpha<<24|rs<<16|gs<<8; }//END OF w }//END OF h dst.setRGB(0,0,width,height,arrayOut,0,width);//1.24seconds long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished remove blue BlueBlurBufImage"); return dst; }//END of remove blue //START of halve red private BufferedImage halveRedBlurBufImage(BufferedImage src) { if(src==null){return null;} System.out.println("start halveRedBlurBufImage"); long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back int[] arrayIn=src.getRGB(0,0,width,height,null,0,width); BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); int[] arrayOut=dst.getRGB(0,0,width,height,null,0,width); int alpha, rs, gs, bs, argb; int r,g,b; for (int h = 1; h < height-1; h++){ for (int w = 1; w < width-1; w++){ alpha=rs=gs=bs=0; for(int hh=-1; hh<=1; hh++) { for(int ww=-1; ww<=1; ww++) { argb=arrayIn[(h*width)+(hh*width) +w + ww];//1.24seconds alpha= (argb>>24) & 0xff; r = (argb >>16) & 0xff; g = (argb >> 8) & 0xff; b = argb & 0xff; //red halved here rs += r/2; gs += g; bs += b; }//END OF ww }//END OF hh rs /= 9; gs /= 9; bs /= 9; arrayOut[w+(h*width)]=alpha<<24|rs<<16|gs<<8|bs; }//END OF w }//END OF h dst.setRGB(0,0,width,height,arrayOut,0,width);//1.24seconds long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished halve red BlueBlurBufImage"); return dst; }//END of halve red //START OF grey private BufferedImage greyBlurBufImage(BufferedImage src) { if(src==null){return null;} System.out.println("start greyBlurBufImage"); long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back int[] arrayIn=src.getRGB(0,0,width,height,null,0,width); BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); int[] arrayOut=dst.getRGB(0,0,width,height,null,0,width); int alpha, rs, gs, bs, argb; int r,g,b; int rrs, ggs, bbs; for (int h = 1; h < height-1; h++){ for (int w = 1; w < width-1; w++){ alpha=rs=gs=bs=0; for(int hh=-1; hh<=1; hh++) { for(int ww=-1; ww<=1; ww++) { argb=arrayIn[(h*width)+(hh*width) +w + ww];//1.24seconds alpha= (argb>>24) & 0xff; r = (argb >> 16) & 0xff; g = (argb >> 8) & 0xff; b = argb & 0xff; rs += r; gs += g; bs += b; rs=(rs+gs+bs)/3; gs=(rs+gs+bs)/3; bs=(rs+gs+bs)/3; }//END OF ww }//END OF hh rs /= 9; gs /= 9; bs /= 9; arrayOut[w+(h*width)]=alpha<<24|rs<<16|gs<<8|bs; }//END OF w }//END OF h dst.setRGB(0,0,width,height,arrayOut,0,width);//1.24seconds long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished change to greyBlurBufImage"); return dst; }//END of gray private BufferedImage invertBlurBufImage(BufferedImage src) { if(src==null){return null;} System.out.println("start invertBlurBufImage"); long start_time=new java.util.Date().getTime(); int width = src.getWidth(null); int height = src.getHeight(null);//null call back int[] arrayIn=src.getRGB(0,0,width,height,null,0,width); BufferedImage dst = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB); int[] arrayOut=dst.getRGB(0,0,width,height,null,0,width); int alpha, rs, gs, bs, argb; int r,g,b; int rrs, ggs, bbs; for (int h = 1; h < height-1; h++){ for (int w = 1; w < width-1; w++){ alpha=rs=gs=bs=0; for(int hh=-1; hh<=-1; hh++) {//changed to -1 for(int ww=-1; ww<=-1; ww++) {//changed to -1 argb=arrayIn[(h*width)+(hh*width) +w + ww]; alpha= (argb>>24) & 0xff; r = (argb >> 16) & 0xff; g = (argb >> 8) & 0xff; b = argb & 0xff; rs += r; gs += g; bs += b; rs=255-rs; gs=255-gs; bs=255-bs; }//END OF ww }//END OF hh // rs /= 9; gs /= 9; bs /= 9; //blur effect taken out, and ww/hh loop above reduced to 1 x ww and 1 x hh arrayOut[w+(h*width)]=alpha<<24|rs<<16|gs<<8|bs; }//END OF w }//END OF h dst.setRGB(0,0,width,height,arrayOut,0,width);//1.24seconds long finish=(new java.util.Date()).getTime(); long milsec=finish-start_time; System.out.println(" "+finish+" - "+start_time +" dif"+milsec); System.out.println(+(milsec/1000)+"sec ."+(milsec%1000)); System.out.println(" finished invertBlurBufImage"); return dst; }//END of invert /* public void paintComponent(Graphics g){ //this gives the tool bar but no picture } */ private class InnerJPanel extends JPanel{ InnerJPanel(){ setBorder(BorderFactory.createTitledBorder( "myDrawJPanel " )); } public void paintComponent(Graphics g){ //this gives the tool bar but no picture super.paintComponent(g); //not in JFrame ok jPanel gives border g.drawString(" inner Jpanel just to show the alpha on the image to come",1,60); if(displayImage != null){ //g.drawImage (displayImage, 0, 0, this); //top=this.getBorder().getBorderInsets(this).top; int top=getInsets().top; int left=getInsets().left; int right=getInsets().right; int bottom=getInsets().bottom; g.drawImage (displayImage, left , top, this.getWidth()-right-left, this.getHeight()-bottom-top, this); } } } } /*class main{ public static void main (String [] args) { new Simpleimage3CutDown ("SimpleImage3CutDown java 1.5 swingstyle kghkh"); } } */ class MyFloatingJFrame extends JFrame//tried jInternalFrame { BufferedImage bi; MyFloatingJFrame(BufferedImage src){// super("Floating Frame"); bi=src; super.setSize(400,350); super.setLocation(50,90); //super.getContentPane().add( somepanel ); super.setVisible(true); super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } //public void paintComponent(Graphics g){ public void paint(Graphics g){ if(bi != null){ g.drawImage (bi, 0, 0, this); } } }