Java Programs - Image Editor
Java Programs - Image Editor
Java programs
This site provides you the opportunity to learn Java programming language by programs.
Home Java Programs Random Posts Programming Tutorials Programming Exercises Programming Tips
► August (29)
► July (34)
▼ June (18)
Word to Text Converter
Text To Word Converter
Extract images from PDF
PDF To JPG Converter
JPG To PDF Converter
PDF To Text Converter
Text to PDF Converter
Phonebook
MediaTracker mt; JScrollPane (1) JSlider (1) JSpinner (1) JTable (1)
JTextArea (1) JTextField (1) JTextPane (1) JToolBar (1)
static Color c;
JTree (1) ListCellRenderer (1) ListIterator (1) Locale (1)
Color colorTextDraw;
MouseEvent (1) MouseListener (1)
Robot rb; MouseMotionListener (1) NumberFormat (1)
boolean dirHor; ObjectInputStream (1) ObjectOutputStream (1) SQLite
String imgFileName; (1) Serializable (1) SimpleDateFormat (1) StringBuilder
String fontName; (1) Toolkit (1) ZipFile (1) ZipOutputStream (1)
int fontSize; actionevent (1) add image to image (1) add text to
String textToDraw; image (1) adjustmentlistener (1) amortization (1)
public ImgArea(){ analyzer (1) animation (1) applet (1) array (1) array
manipulation (1) arrays (1) audio (1) automation (1)
background process (1) bar chart (1) borderlayout (1)
addMouseListener(new Mousexy()); //hanlding mouse event of Canvas class
brightness (1) bufferedimage (1) bufferedimage to byte
addKeyListener(new KList()); //handling key event of the Canvas array (1) bufferedimage to image (1) button (1)
try{ calculation (1) capture (1) capture screen (1) celsius
rb=new Robot(); //create Robot object (1) change event (1) charts in Java (1) checkbox (1)
}catch(AWTException e){} checkboxgroup (1) checker (1) choice (1) clock (1)
color (1) combine (1) compression (1) contact list (1)
ds=getToolkit().getScreenSize(); //get the screen size conversion (1) convert string to date (1) convert string
mX=(int)ds.getWidth()/2; //half of the screen width to number (1) copy files (1) count words frequency (1)
counter (1) create and extract jar file (1) create image
mY=(int)ds.getHeight()/2;//half of the screen height
(1) create pdf (1) currency conversion (1) currency
exchange (1) cursor position (1) database (1) date add
} (1) date diff (1) date time (1) display (1) document
event (1) documentation in Java (1) documentlistener
public void paint(Graphics g){ (1) download (1) drag and drop (1) drawing (1) e-mail
Graphics2D g2d=(Graphics2D)g; //create Graphics2D object (1) edit picture color (1) event (1) extract links from
if(imageLoaded){ html (1) extract zip (1) fahrenheit (1) file (1) file view (1)
files (1) final (1) flowlayout (1) focuseven (1)
//draw the update image focuslistener (1) font (1) format date (1) format number
(1) fraction (1) frame (1) ftp client (1) ftp server (1)
if(actionSlided || actionResized || actionTransparent || actionRotated || drawn ){
generics (1) gridlayout (1) guess (1) hello world (1)
x=mX-bimg.getWidth()/2;
host name (1) html to pdf converter (1) image editor (1)
y=mY-bimg.getHeight()/2; image to bufferedimage (1) image viewer (1)
g2d.translate(x,y); //move to coordinate (x,y) imagefilter (1) imageproducer (1) interface (1) item
g2d.drawImage(bimg,0,0,null); //draw the iamge event (1) jar pack (1) java code (1) java mail (1) java
programs (1) javadoc (1) jdom (1) jpeg (1) jpg to pdf
} converter (1) keyevent (1) label (1) launch e-mail client
(1) learn java by programs (1) lines (1) list (1) login
else{ //draw the original image form (1) math calculator (1) max (1) measurement (1)
merge (1) merge pdf (1) mouse and key controls (1)
x=mX-orBufferedImage.getWidth()/2;
move files (1) number (1) open and print file (1) open
y=mY-orBufferedImage.getHeight()/2;
browser (1) password check (1) password checker (1)
g2d.translate(x,y); //move to coordinate (x,y) password protection (1) pdf reader (1) pdf to jpg
g2d.drawImage(orBufferedImage,0,0,null); //draw image converter (1) pdf to text converter (1) performance and
} resources consumption assessment (1) phonebook (1)
} photo viewer (1) power (1) print (1) printer (1) programs
g2d.dispose(); //clean the Graphic2D object (1) quiz (1) random (1) recording (1) resize (1) robot (1)
rotate (1) runnable (1) sale calculator (1) save chart in
} pdf and png image files (1) screen (1) screen recording
(1) screen size (1) scrollbar (1) send e-mail (1)
slideshow (1) sound (1) sound player (1) source doe
class Mousexy extends MouseAdapter{
(1) splash screen (1) split pdf (1) test score (1) text
animation (1) text editor (1) text to pdf converter (1) text
public void mousePressed(MouseEvent e){ to word converter (1) textarea (1) textfield (1)
Color color=rb.getPixelColor(e.getX(),e.getY()); //get the color at the clicked point textlistener (1) thread (1) transparent (1) transparent
try{ window (1) upload file (1) video (1) video player (1)
setColor(color); //take the color at the clicked point for later use word to pdf converter (1) word to text converter (1) xml
if(actionDraw){ //add text to the update image processing (1) zip file (1) zoom (1)
}
//Rotate the image shown on the program interface
public void makeImageRotate(BufferedImage image,int w,int h){
BufferedImage bi=(BufferedImage)createImage(w,h);
Graphics2D g2d=(Graphics2D)bi.createGraphics();
radian=(float)Math.PI/2; //angle
g2d.translate(w/2,h/2); move to coordinate (w/2,h/2)
g2d.rotate(radian); //rotate the image
g2d.translate(-h/2,-w/2); //move the coordinate back
g2d.drawImage(image,0,0,null); //draw the rotated image
bimg=bi; //update the image so now you see the rotated image
g2d.dispose();
}
//The rotateImage invokes the makeImageRotate method to rotate the image
public void rotateImage(){
BufferedImage bi;
//rotate update image
if(actionSlided || actionResized || actionTransparent || actionRotated || drawn){
bi=bimg;
}
//rotate the original image
else{
bi=orBufferedImage;
}
makeImageRotate(bi,bi.getHeight(),bi.getWidth());
}
//The makeCompression method has code to compress the image
//In java, you can specify the compression quality value by using the IIOImage class
public void makeCompression(File outFileName){
try{
ImageWriter imgWriter =(ImageWriter)
ImageIO.getImageWritersByFormatName("jpg").next();
//wrap the image data (read from the file) in IIOImage object
IIOImage iioImg;
if(actionSlided || actionResized){ //bimg not a blank buffered image
iioImg = new IIOImage(bimg, null,null);
}
else{
iioImg = new IIOImage(orBufferedImage, null,null); //otherwise compress the original
buffered image
}
//clean objects
imgOutStrm.close();
imgWriter.dispose();
}catch(Exception e){}
}
//The resizeImage method has code to resize the image
//This method is invoked when the user clicks OK button on the image resize window
//The image resize window is displayed when you select the Image resize sub-menu
item
public void resizeImage(int w,int h){
BufferedImage bi=(BufferedImage)createImage(w,h);
Graphics2D g2d=(Graphics2D)bi.createGraphics();
//resize the update image
//The filterImage method applies brightness to the image when the knob of the image
slider is
//making changed.
//When the value of the image slider changes it affects the e variable
//so the image is brighter or darker
public void filterImage(){
float[] elements = {0.0f, 1.0f, 0.0f, -1.0f,e,1.0f,0.0f,0.0f,0.0f};
Kernel kernel = new Kernel(3, 3, elements); //create keynel object to encapsulate the
elements array
ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
//create ConvolveOp to encapsulate
//the kernel
bimg= new
BufferedImage(orBufferedImage.getWidth(),orBufferedImage.getHeight(),BufferedImag
e.TYPE_INT_RGB);
cop.filter(orBufferedImage,bimg); //start filtering the image
//the filtered image is stored in the bimg buffered image
//now the image increases or decreases its brightness
}
//set a value to e variable
//this method is invoked when the user makes change to the image slider
public void setValue(float value){
e=value;
}
//The makeTransparency method makes the selected background area of the image
transparent
//This method works well if the background of the image has a single color value (e.g.
white, black, blue, etc.)
public void makeTransparency(final Color col){
ImageFilter filter = new RGBImageFilter() {
int imageRGB = col.getRGB();
Image img=getToolkit().createImage(ip);
try{
mt.addImage(img,0);
mt.waitForID(0);
bimg=createBufferedImageFromImage(img,img.getWidth(null),img.getHeight(null),tru
e);
actionTransparent=true;
repaint();
}catch(Exception e){}
}
ImgArea ia;
JFileChooser chooser;
JMenuBar mainmenu;
JMenu menu;
JMenu editmenu;
JMenuItem mopen;
JMenuItem msaveas;
JMenuItem msave;
JMenuItem mexit;
JMenuItem mbright;
JMenuItem mcompress;
JMenuItem mresize;
JMenuItem mrotate;
JMenuItem mtransparent;
JMenuItem maddtext;
JMenuItem mcancel;
String filename;
Main(){
ia=new ImgArea();
Container cont=getContentPane();
cont.add(ia,BorderLayout.CENTER );
mainmenu=new JMenuBar();
menu=new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
mopen=new JMenuItem("Open...");
mopen.setMnemonic(KeyEvent.VK_O);
mopen.addActionListener(this);
msave=new JMenuItem("Save");
msave.setMnemonic(KeyEvent.VK_V);
msave.addActionListener(this);
mexit=new JMenuItem("Exit");
mexit.setMnemonic(KeyEvent.VK_X);
mexit.addActionListener(this);
menu.add(mopen);
menu.add(msaveas);
menu.add(msave);
menu.add(mexit);
editmenu=new JMenu("Edit");
editmenu.setMnemonic(KeyEvent.VK_E);
mbright=new JMenuItem("Image brightness");
mbright.setMnemonic(KeyEvent.VK_B);
mbright.addActionListener(this);
editmenu.add(maddtext);
editmenu.add(mbright);
editmenu.add(mcompress);
editmenu.add(mresize);
editmenu.add(mrotate);
editmenu.add(mtransparent);
editmenu.add(mcancel);
mainmenu.add(menu);
mainmenu.add(editmenu);
setJMenuBar(mainmenu);
setTitle("Image Editor");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
setVisible(true);
ImageBrightness(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
Container cont=getContentPane();
slider=new JSlider(-10,10,0);
slider.setEnabled(false);
slider.addChangeListener(this);
cont.add(slider,BorderLayout.CENTER);
slider.setEnabled(true);
setTitle("Image brightness");
setPreferredSize(new Dimension(300,100));
setVisible(true);
pack();
enableSlider(false);
}
public void enableSlider(boolean enabled){
slider.setEnabled(enabled);
}
public void stateChanged(ChangeEvent e){
ia.setValue(slider.getValue()/10.0f);
ia.setActionSlided(true);
ia.filterImage();
ia.repaint();
enableSaving(true);
btOK=new JButton("OK");
btOK.setBackground(Color.BLACK);
btOK.setForeground(Color.BLUE);
btOK.addActionListener(this);
txtWidth=new JTextField(4);
txtWidth.addKeyListener(new KeyList());
txtHeight=new JTextField(4);
txtHeight.addKeyListener(new KeyList());
panel=new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JLabel("Width:"));
panel.add(txtWidth);
panel.add(new JLabel("Height:"));
panel.add(txtHeight);
panel.add(btOK);
panel.setBackground(Color.GRAY);
add(panel, BorderLayout.CENTER);
setVisible(true);
pack();
enableComponents(false);
}
//This method can be invoked to enable the text boxes of image width and height
public void enableComponents(boolean enabled){
txtWidth.setEnabled(enabled);
txtHeight.setEnabled(enabled);
btOK.setEnabled(enabled);
}
//This method works when you click the OK button to resize the image
public void actionPerformed(ActionEvent e){
if(e.getSource()==btOK){
ia.setActionResized(true);
ia.resizeImage(Integer.parseInt(txtWidth.getText()),Integer.parseInt(txtHeight.getText(
)));
enableSaving(true);
ia.repaint();
}
}
//Restrict the key presses
//Only number, backspace, and delete keys are allowed
public class KeyList extends KeyAdapter{
public void keyTyped(KeyEvent ke){
char c = ke.getKeyChar();
int intkey=(int)c;
if(!(intkey>=48 && intkey<=57 || intkey==8 || intkey==127))
{
ke.consume(); //hide the unwanted key
}
}////end of the ImageResize class
btOK=new JButton("OK");
btOK.setBackground(Color.BLACK);
btOK.setForeground(Color.BLUE);
btOK.addActionListener(this);
txtText=new JTextArea(1,30);
cbFontNames=new JComboBox();
cbFontSizes=new JComboBox();
panel=new JPanel();
panel.setLayout(new GridLayout(4,1));
panel.add(new JLabel("Text:"));
panel.add(txtText);
panel.add(new JLabel("Font Name:"));
panel.add(cbFontNames);
panel.add(new JLabel("Font Size:"));
panel.add(cbFontSizes);
panel.add(btSetColor);
panel.add(btOK);
panel.setBackground(Color.GRAY);
add(panel, BorderLayout.CENTER);
setVisible(true);
pack();
listFonts();
}
}
}
//The listFonts method get all available fonts from the system
public void listFonts(){
//get the available font names and add them to the font names combobox
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts=ge.getAvailableFontFamilyNames();
for(String f:fonts)
cbFontNames.addItem(f);
//Initialize font sizes
for(int i=8;i<50;i++)
cbFontSizes.addItem(i);
}
} ////end of the TextAdd class
}
else if(source.getText().compareTo("Save as...")==0)
{
showSaveFileDialog();
}
else if(source.getText().compareTo("Save")==0)
{
ia.saveToFile(filename);
}
else if(source.getText().compareTo("Add text on image")==0)
{
new TextAdd();
}
if(ImgArea.imageLoaded){
ia.rotateImage();
enableSaving(true);
}
}
else if(source.getText().compareTo("Exit")==0)
System.exit(0);
//The setImage method has code to open the file dialog so the user can choose
//the file to show on the program interface
public void setImage(){
//The showSaveFileDialog method has code to display the save file dialog
//It is invoked when the user select Save as... sub-menu item
public void showSaveFileDialog(){
int returnVal = chooser.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
String filen=chooser.getSelectedFile().toString();
ia.saveToFile(filen);
}
}
//The enableSaving method defines code to enable or disable saving sub-menu items
public void enableSaving(boolean f){
msaveas.setEnabled(f);
msave.setEnabled(f);
For code explanation of Image Editor program, please read the comments along with
the code.
34 comments:
Replies
Reply
Replies
Reply
Replies
Reply
Replies
Reply
http://forseoblogger.blogspot.com/2016/07/high-pr-image-sharing-sites-
list.html July 23, 2016 at 6:06 AM
Thanks for sharing this post
Reply
Reply
Replies
Reply
Publish Preview
ABOUT ME
Hi, my name is Yuk Sovandara. I am an IT officer and lecturer. In my free time, I like to write something about programming. In 2011, i created a
website www.worldbestlearningcenter.com that provides free tutorials, exercises, and tips of the most popular programming and web scripting
languages. Seven months ago i created the javatheprogram.blogspot.com blog providing Java tutorials and example programs. Recently, i also
created a blog called dev-androidapps.blogspot.com for people who want to become Android developers by learning from example programs. To
contact me, please email [email protected]. You also can connect with me on Facebook , and Google+.
TRANSLATE
Select Language
Powered by Translate
Copyright © javatheprogram.blogspot.com 2013. All rights reserved. Picture Window theme. Powered by Blogger.