repeadtedly execute the whole program!

21 views
Skip to first unread message

Amarjit Singh

unread,
Jul 14, 2010, 9:25:31 AM7/14/10
to bo...@googlegroups.com
Hi,\
          I am sending my desktop's screenshot to the server .I would like to repeatedly take screenshots ans send iot to the server.How do i implement it ??
     I am having a program that is taking the screenshot and sending the image to the server.and at the server side the image is being saved and uploaded to a url.
could you please tell me how to execute the whole process continuoulsly so that the desktop image is shared continuously?????
please help 



the follwing is the Main program of the whole project.
running this program does the above ....



class Main
{
public static void main(String args[]) 
{
ScreenCaptureMain scm = new ScreenCaptureMain();
ImageLossyCompress ilc = new ImageLossyCompress();
CompressingFileMain cfm = new CompressingFileMain();
scm.ScreenShot();
ilc.CompressImg();
cfm.Compression();
PostAFile paf =new PostAFile();
paf.PostFile();
}
}


--
Thanks & Regards
Amarjit Kumar Singh
3rd Year, Computer Science & Engineering
National Institute Of Technology,Durgapur
West Bengal,India




Sumanth P K

unread,
Jul 15, 2010, 9:15:55 AM7/15/10
to bo...@googlegroups.com
See if you can use: ScheduledExecutorService (http://www.ibm.com/developerworks/java/library/j-5things5.html)

--
You received this message because you are subscribed to the Google Groups "Bangalore Open Java Users Group- BOJUG" group.
To post to this group, send email to bo...@googlegroups.com.
To unsubscribe from this group, send email to bojug+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bojug?hl=en.

Amarjit Singh

unread,
Jul 15, 2010, 1:31:54 PM7/15/10
to bo...@googlegroups.com
Hi ,
     the link that uve sent... its working for me.... but how du i stop the whole process ???

Kiran Raja

unread,
Jul 16, 2010, 12:08:01 PM7/16/10
to bo...@googlegroups.com
Hi Amar,
Check this out:

import java.util.concurrent.*;

public class Ping
{
public static void main(String[] args)
{
ScheduledExecutorService ses =
Executors.newScheduledThreadPool(1);
Runnable pinger = new Runnable() {
public void run() {
//execute what you want here
// example lets consider you execute method as below.and a value is returned from server
String saved = Uploader.sendSnapShot();
}
};
ScheduledFuture sf = ses.scheduleAtFixedRate(pinger, 5, 5, TimeUnit.SECONDS);

   while(!sf.isDone()){

       System.out.print("Uploaded Snap shot");

     if(saved != null)

           continue;

     }

     ses.shutdown();


}
}

Regards,
Kiran Raja


--

Amarjit Singh

unread,
Jul 17, 2010, 2:27:35 PM7/17/10
to bo...@googlegroups.com
hey thnx..
i just want to know if the code can be used commercially... as i m an intern and doing a project for a company ???




Final Year, Computer Science & Engineering

Amarjit Singh

unread,
Jul 20, 2010, 6:41:20 AM7/20/10
to bo...@googlegroups.com
Hi everyone,

Thanks Kiran.It seems to work fine for a single function but when I try to start and stop sharing with event handlers for different buttons, the 'ses' object is not being recognized by the 'jButton2ActionPerformed'. I've made both functions public so I can't understand why it still won't work. Can anyone please see the code fragment I'm pasting here and help me out a little. Thank you for your patience. 


public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
        Runnable pinger = new Runnable() {
            public void run() {
                Start str = new Start();
                str.Start();
            }
            };
       ses.scheduleAtFixedRate(pinger, 0, 1, TimeUnit.SECONDS);
    
    
    // TODO add your handling code here:
}                                        

public void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     ses.shutdown();
}


Thanks & Regards

Yaswanth Ravella

unread,
Jul 21, 2010, 2:12:46 AM7/21/10
to bo...@googlegroups.com
ses object is defined inside jButton1ActionPerformed method and won't be available in jButton2ActionPerformed method. 

To use ses object in both the methods, define it as an instance variable. 

--
Yaswanth Ravella


Amarjit Singh

unread,
Jul 21, 2010, 2:35:39 AM7/21/10
to bo...@googlegroups.com


@Yashwant - thnkx-- It worked for me... but now theres another issue i.e when I am clicking on to jbutton1 item the process is starting for me and clicking on to jbutton2 item the process is stopping as well but as soon as I click onto jbutton1 item again it gives me the followiing error..

Exception in thread "AWT-EventQueue-0" java.util.concurrent.RejectedExecutionException
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1760)
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:767)
        at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:216)
        at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:398)
        at deskopsharing.DSUI.jButton1ActionPerformed(DSUI.java:95)
        at deskopsharing.DSUI$1.actionPerformed(DSUI.java:51)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6216)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


As mentioned by you , creating an instance variable worked but restarting the whole process dus not...

Yaswanth Ravella

unread,
Jul 21, 2010, 2:59:54 AM7/21/10
to bo...@googlegroups.com
on click of jbutton2 you are shutting down the executor. which means that the executor will no longer accept new tasks and you will get an RejectedExecutionException if u submit a new task . 

To avoid this try checking if the executor is shutdown, and if it's shutdown create a new executor before submitting a new task to it.. 

for example it may need to modify your code some thing like this in jButton1ActionPerformed method.  // If shutdown create a new executorservice ..

if(ses.isShutdown())
 ses = Executors.newScheduledThreadPool(1);

Please read the API for more details .

--
Yaswanth Ravella

Amarjit Singh

unread,
Jul 21, 2010, 3:11:48 AM7/21/10
to bo...@googlegroups.com
@Yashwant- thanx it worked!!!!!!!!!
--

Amarjit Singh

unread,
Jul 21, 2010, 3:40:19 PM7/21/10
to bo...@googlegroups.com
Hello once again ,
                          as I mentioned before my project is based on screen sharing and I am sending the screen shots to the server continuously. Using the above code as given earlier I am creating a GUI to take the screenshot & send it to the server continuously and then stop taking the screenshot using another button.


fragment of the code is here as follows for the GUI


           
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
        if(ses.isShutdown())
        {
           ses = Executors.newScheduledThreadPool(1);
        }

        Runnable pinger = new Runnable() {
            public void run() {
                Start str = new Start();
                str.Start();
            }
            };
       ses.scheduleAtFixedRate(pinger, 0, 1, TimeUnit.SECONDS);
    
    // TODO add your handling code here:
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ses.shutdown(); 
    // TODO add your handling code here:
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.exit(0);
    // TODO add your handling code here:
}                                        

   
Now the program that is taking the screenshot is taking the image of a particular area of the screen i.e using Rectangle area = new Rectangle(0,0,400,400); in the following program.
But I wish to create a GUI that will have the portion of the area to be shared highlighted within some kind of a frame and then as soon as i click on jbutton1 to start sharing , then the program should take the screenshot of that particular portion only.

e.g 1)like a snipping tool in windows but with a fixed dimension, the dimension of the area to be shared is made fixed.
      2)or like screen sharing in skype 
Please give some idea as I am not familiar with GUI's much.

Thanks for your patience

public class ScreenCaptureMain {
    public void ScreenShot() 
{
         new ScreenCap().captureFullScreen();
}

}
class ScreenCap 
{
int i=0,j=0;
Properties prop = new Properties();
String propFileName = "ScreenCapture.dat";
String saveLocation =null;
String imageType =null;
String imageName = null;
public ScreenCap() 
{
         loadProperties(propFileName);
}
private void loadProperties(String propFileName)
{
         try 
{
FileInputStream in = new FileInputStream(propFileName);
prop.load(in);
in.close();
       } 
catch(FileNotFoundException e) 
{
prop.setProperty("IMG_NAME", "SCREENSHOT");
prop.setProperty("IMG_TYPE", "jpg");
prop.setProperty("SAVE_LOCATION", "C:\\DesktopSharingApp\\" );
setProperties(prop);
catch(Exception e) 
                {
       e.printStackTrace();
}
imageName = prop.getProperty("IMG_NAME");
imageType = prop.getProperty("IMG_TYPE");
saveLocation = prop.getProperty("SAVE_LOCATION");
}
    
    private void setProperties(Properties pr) 
    {
        try 
{
         pr.store(new FileOutputStream(propFileName), null);
        }
catch(IOException e)
{
            System.err.println("Exception while saving properties file. " + e);
        }
    }    
    public void captureFullScreen()
    {  
        try 
{
Robot robot = new Robot();
         Rectangle area = new Rectangle(0,0,400,400);
         File target = new File(saveLocation, imageName+"." + imageType);
         saveImgToFile(robot.createScreenCapture(area), target);
        } 
catch (AWTException e) 
{
            System.err.println("Exception while capturing screen. " + e);
        }
    }
    private void saveImgToFile(RenderedImage renderedImage, File target) 
    {
        try 
{
             ImageIO.write(renderedImage, imageType, target);
        } 
catch (IOException e) 
{
         System.err.println(e);
        }
    }
}

Amarjit Singh

unread,
Jul 22, 2010, 3:56:34 PM7/22/10
to bo...@googlegroups.com

plz help as I need to make a rectangular area in a GUI so that I can take the screenshot of thAT PORTION of the screen... I have a program that can take the screenshot but need help with the GUI for the same
Reply all
Reply to author
Forward
0 new messages