Click here to Skip to main content
15,891,204 members
Home / Discussions / Java
   

Java

 
AnswerRe: port scanner Pin
Richard MacCutchan31-Mar-14 23:33
mveRichard MacCutchan31-Mar-14 23:33 
Questionport scanner Pin
aghori31-Mar-14 22:16
aghori31-Mar-14 22:16 
AnswerRe: port scanner Pin
Marco Bertschi31-Mar-14 22:24
protectorMarco Bertschi31-Mar-14 22:24 
Questionjquery dialog box to show dynamic page data Pin
swarjava31-Mar-14 11:01
swarjava31-Mar-14 11:01 
AnswerRe: jquery dialog box to show dynamic page data Pin
TorstenH.31-Mar-14 21:01
TorstenH.31-Mar-14 21:01 
QuestionClient to Client data transfer in (Client Server Model) in java Pin
Member 942751130-Mar-14 3:05
Member 942751130-Mar-14 3:05 
AnswerRe: Client to Client data transfer in (Client Server Model) in java Pin
Richard MacCutchan30-Mar-14 3:47
mveRichard MacCutchan30-Mar-14 3:47 
Questiongetting NullPointerException Pin
Member 1043154927-Mar-14 22:06
Member 1043154927-Mar-14 22:06 
Hi
Here i am posting my code that i have written
package com.evora.practice.swing;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;






public class PhotoViewer {

public static void main(String args[]){

new Main();
}
}

class ImageDraw extends Canvas{
/**
*
*/
private static final long serialVersionUID = 1L;
BufferedImage img;
double radian=0.0;
Dimension ds;
int iw;
int ih;
int x;
int y;
int adjX;
int adjY;
int adjW;
int adjH;
int mX;
int mY;
int orWidth;
int orHeight;
boolean rotate;
HashMap<integer, string=""> imgFiles;
String filename;
int curImgIndex;
ImageDraw(){
ds=getToolkit().getScreenSize(); //get the screen size
mX=(int)ds.getWidth()/4; //half of the screen width
mY=(int)ds.getHeight()/4; //half of the screen height
filename=null; //initialize filename variable
rotate=false; //initialize rotate variable
//initialize variables used to increase or decrease photo size
adjX=0;
adjY=1;
adjW=1;
adjH=1;

}

public void paint(Graphics g){
if(filename!=null){

if(rotate) //show rotated photo
rotateImage(g);
else
showImage(g); //show normal and zoom photo

}
}

//The rotateImage rotates the photo in clockwise or counterclockwise
//direction based on the radian value
public void rotateImage(Graphics g){
Graphics2D g2d=(Graphics2D)g;
g2d.translate(mX,mY); //move the original coordinate to point(mX,mY)
g2d.rotate(radian); //rotate the photo
g2d.translate(-iw/2,-ih/2); //move the coordinate back to point (iw/2,ih/2)
g2d.drawImage(img,0,0,iw,ih,null);//show the rotate photo
g2d.dispose(); //clean the g2d object
rotate=false; //reset the rotate to false after the photo rotates
}

//Increase the radian value by Math.PI/2
//so the photo is rotated in clockwise direction
public void rotateClockwise(){
radian+=Math.PI/2;
}
//Decrease the radian value by Math.PI/2
//so the photo is rotated in countclockwise direction
public void rotateCounterClockwise(){
radian-=Math.PI/2;
}

//The showImage method has code to display the photo when it is in
//normal view or zoom view
public void showImage(Graphics g){

Graphics2D g2d=(Graphics2D)g;
g2d.translate(x,y);
g2d.drawImage(img,0,0,iw,ih,null);
g2d.dispose();

}

public void readImage(String filename){
try{
//read the photo file so it is ready to display
img=ImageIO.read(new File(filename));
//get the original width and height of the photo
orWidth=img.getWidth();
orHeight=img.getHeight();
//Make sure the photo is not too large to fit the screen
if(mX<orwidth 2)
="" orwidth="mX*2;
" if(my<orheight="" orheight="mY*2;
" setimagebounds();
=""
="" }catch(ioexception="" e){};
="" }
="" determine="" the="" appropriate="" photo="" size="" and="" location="" to="" place="" photo
="" public="" void="" setimagebounds(){
="" try{
="" x="mX-orWidth/2+adjX;
" y="mY-orHeight/2+adjY;
" size
="" iw="orWidth+adjW;
" ih="orHeight+adjH;
" }catch(nullpointerexception="" npe){system.exit(-1);}
="" zoom="" in
="" decrease="" x-axis="" y-axis="" values="" at="" same="" time
="" increase="" width="" height
="" zoomin(){
="" adjx-="10;
" adjy-="10;
" adjw+="20;
" adjh+="20;
" out
="" zoomout(){
="" if(iw="">40 && ih>40){
adjX+=10;
adjY+=10;
adjW-=20;
adjH-=20;
setImageBounds();
}

}

//The storeImages method is invoked when the user select the Open... sub-menu
//item from the program interface
//It displays a file chooser dialog for multiple files selection
//The selected photos file are stored in a HashMap object imgList for
//later show
public void storeImages(){
imgFiles=new HashMap<integer, string="">();
JFileChooser chooser = new JFileChooser("C:\\Users\\Public\\Pictures\\Sample Pictures\\");

FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif","png","bmp");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File[] Files=chooser.getCurrentDirectory().listFiles();


for( int i=0;i<files.length;i++){
="" imgfiles.put(i,files[i].tostring());
="" }
="" the="" movefirst="" method="" has="" code="" to="" move="" first="" photo="" in="" imglist
="" public="" void="" movefirst(){
="" if(!imgfiles.isempty()){
="" curimgindex="0;" filename="imgFiles.get(curImgIndex);
" readimage(filename);
="" repaint();
="" movelast="" last="" movelast(){
="" moveprevious="" back="" moveprevious(){
="" if(!imgfiles.isempty()="" &&="" curimgindex!="0){
" curimgindex--;="" movenext="" forward="" movenext(){
="" curimgindex<imgfiles.size()-1){
="" curimgindex++;="" }

="" getimgfiles="" allows="" access="" list="" of="" photos--imglist
="" hashmap<integer,string=""> getImgFiles(){
return imgFiles;
}

}


//The Main class represents the interface of the PhotoView program
//On the interface, you see a File menu that has two sub-menu items: Open..., and Exit
//When the photo is displayed, the program will display a panel that contains
//all buttons you need to rotate, zoom, move, and play the slideshow of the photos
class Main extends JFrame implements ActionListener{






/**
*
*/
private static final long serialVersionUID = 1L;
JButton btrotateClockwise;
JButton btrotateCounterClockwise;
JButton btZoomIn;
JButton btPrevious;
JButton btNext;
JButton btZoomOut;
JButton btSlideShow;
JPanel panel;
ImageDraw imgDraw;
boolean stop=true;
ImageSlide Slider;
Main(){

setTitle("Photo Viewer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
setBackground(Color.CYAN);

JMenuBar mainmenu=new JMenuBar();
JMenu menu=new JMenu("File");
JMenuItem mopen=new JMenuItem("Open...");
mopen.setMnemonic(KeyEvent.VK_O);
mopen.addActionListener(this);
JMenuItem mexit=new JMenuItem("Exit");
mexit.setMnemonic(KeyEvent.VK_X);
mexit.addActionListener(this);
menu.add(mopen);
menu.add(mexit);
mainmenu.add(menu);
setJMenuBar(mainmenu);

btrotateClockwise=new JButton("ROTATE RIGHT");
btrotateClockwise.setBackground(Color.GREEN);
btrotateClockwise.addActionListener(this);

btrotateCounterClockwise=new JButton("ROTATE LEFT");
btrotateCounterClockwise.setBackground(Color.YELLOW);
btrotateCounterClockwise.addActionListener(this);

btZoomIn=new JButton("ZOOMIN");
btZoomIn.setBackground(Color.MAGENTA);
btZoomIn.addActionListener(this);

btZoomOut=new JButton("ZOOMOUT");
btZoomOut.setBackground(Color.PINK);
btZoomOut.addActionListener(this);

btPrevious=new JButton("BACK");
btPrevious.setBackground(Color.ORANGE);
btPrevious.addActionListener(this);

btNext=new JButton("NEXT");
btNext.setBackground(Color.GRAY);
btNext.addActionListener(this);

btSlideShow=new JButton("PLAYALL");
btSlideShow.setBackground(Color.WHITE);
btSlideShow.addActionListener(this);


panel=new JPanel();
panel.setLayout(new FlowLayout());
panel.add(btrotateClockwise);
panel.add(btrotateCounterClockwise);
panel.add(btZoomIn);
panel.add(btZoomOut);
panel.add(btNext);
panel.add(btPrevious);
panel.add(btSlideShow);
panel.setBackground(Color.BLACK);
add(panel, BorderLayout.SOUTH);

imgDraw=new ImageDraw();
add(imgDraw,BorderLayout.CENTER );
setVisible(true);
panel.setVisible(false);
}
//*handling buttons and sub-menu items clicks
public void actionPerformed(ActionEvent e){
if(e.getSource()==btrotateClockwise)
{
imgDraw.rotate=true;
imgDraw.rotateClockwise();
imgDraw.repaint();
}
else if(e.getSource()==btrotateCounterClockwise)
{
imgDraw.rotate=true;
imgDraw.rotateCounterClockwise();
imgDraw.repaint();
}
else if(e.getSource()==btZoomIn)
{
imgDraw.zoomIn();
imgDraw.repaint();
}
else if(e.getSource()==btZoomOut)
{
imgDraw.zoomOut();
imgDraw.repaint();
}
else if(e.getSource()==btPrevious)
{
imgDraw.movePrevious();
}
else if(e.getSource()==btNext)
{
imgDraw.moveNext();
}
else if(e.getSource()==btSlideShow)
{
if(stop){
startClick();
}
else{
stopClick();
}


}
else{

JMenuItem source = (JMenuItem)(e.getSource());
if(source.getText().compareTo("Open...")==0)
{
imgDraw.storeImages();
imgDraw.moveFirst();
panel.setVisible(true);
}
else if(source.getText().compareTo("Exit")==0)
System.exit(0);

}
}
//The stopClick method invoked when you click the Pause button
//*to stop the slideshow
public void stopClick(){
btSlideShow.setIcon(new ImageIcon("playall.png"));
stop=true;
Slider.stopShow();
}

//The startClick method invoked when you click the Play button
//to start the slideshow
public void startClick(){
btSlideShow.setIcon(new ImageIcon("pause.png"));
stop=false;
Slider=new ImageSlide();
Slider.start();
}

//The ImageSlide class handle the photo slideshow process
//When you click the Play button the run method of this class is invoked
//When you click the Pause button the stopShow method of this class is invoked
class ImageSlide extends Thread{
boolean started;
HashMap<integer,string> map;
ImageSlide(){
started=true;
map=imgDraw.getImgFiles();
}
public void run(){
int i;
try{
for(i=0;i
SuggestionRe: getting NullPointerException Pin
Richard MacCutchan27-Mar-14 22:29
mveRichard MacCutchan27-Mar-14 22:29 
AnswerRe: getting NullPointerException Pin
TorstenH.31-Mar-14 1:17
TorstenH.31-Mar-14 1:17 
Questionproblem in hibernate mysql connecting Pin
chiku2427-Mar-14 19:57
chiku2427-Mar-14 19:57 
AnswerRe: problem in hibernate mysql connecting Pin
Member 1071490931-Mar-14 23:33
Member 1071490931-Mar-14 23:33 
AnswerRe: problem in hibernate mysql connecting Pin
Bhalchandra Sawant9-Apr-14 20:55
Bhalchandra Sawant9-Apr-14 20:55 
QuestionHow to connect into database and show the form from database Pin
Ah Nam Ge27-Mar-14 18:22
Ah Nam Ge27-Mar-14 18:22 
AnswerRe: How to connect into database and show the form from database Pin
Richard MacCutchan27-Mar-14 22:26
mveRichard MacCutchan27-Mar-14 22:26 
GeneralAbstract Classes in Java Pin
akcreative25-Mar-14 0:29
akcreative25-Mar-14 0:29 
GeneralRe: Abstract Classes in Java Pin
Richard MacCutchan25-Mar-14 1:30
mveRichard MacCutchan25-Mar-14 1:30 
GeneralRe: Abstract Classes in Java Pin
jschell25-Mar-14 13:29
jschell25-Mar-14 13:29 
GeneralRe: Abstract Classes in Java Pin
Bernhard Hiller25-Mar-14 21:39
Bernhard Hiller25-Mar-14 21:39 
GeneralRe: Abstract Classes in Java Pin
Member 1043154927-Mar-14 0:21
Member 1043154927-Mar-14 0:21 
GeneralRe: Abstract Classes in Java Pin
Member 1071490931-Mar-14 23:27
Member 1071490931-Mar-14 23:27 
Questionabout pointers Pin
Member 1030303423-Mar-14 17:39
Member 1030303423-Mar-14 17:39 
AnswerRe: about pointers Pin
Richard MacCutchan23-Mar-14 23:04
mveRichard MacCutchan23-Mar-14 23:04 
AnswerRe: about pointers Pin
Member 1043154927-Mar-14 0:25
Member 1043154927-Mar-14 0:25 
QuestionBrute force Convex Hull Pin
not_20-Mar-14 11:57
not_20-Mar-14 11:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.