Java. Estimation of total number of printing pages - java

I have question how to check how many pages will be printed when i use my own Printable class.
I need it, because i want to have "page_number/total_pages" in footer of each page.
In my case problem with estimation of rows is that rows are wrapped. Additionaly the are empty rows beetwen some of the lines of text and there are some other cases which can prevent regular spreading of text. Generally this is not uniform printing.
As you know, rendering process is done after calling all print dialog windows.
Is any way to deal with this issue or should i somehow launch printing simulation , to receieve real number of pages? Or maybe should i implement some other class?
Regards

You can use following to get no. of pages:
int linesPerPage; // No. of lines per Page to be drawn.
static int numPages; // No. of pages too be rendered.
public int getNumberOfPages() // Override method to get Number Of Pages.
{
return numPages;
}
in main..
linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
numPages = (DATA_to_Print.length - 1)/linesPerPage + 1;
in override print method..
if ((pagenum < 0) | (pagenum >= numPages))
{
return NO_SUCH_PAGE;
}
Each time when print method will be call, value of the numPages will increment.

I figured out how to do it. Maybe this is no elegant, but it works (i tested estimation with document with 1200 pages and estimation is accurate). I will show you rather concept supported with couple line of code because my Printable classes are complex.
This is class which calls all operations connected with printing:
package print_manager;
import icd_searcher.ResultContainer;
import java.awt.Point;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import others.MeasuredBox;
public class PrintManager
{
private PrinterJob printerJob;
private SimpleResultPrinter srp;
/**
* Print printable object.
*
* #param toPrint
*/
public void initPrint(ResultContainer resultToPrint)
{
printerJob = PrinterJob.getPrinterJob();
PageFormat selectedArea = printerJob.pageDialog(printerJob.defaultPage());
MeasuredBox margin = new MeasuredBox(new Point((int) selectedArea.getImageableX(), (int) selectedArea.getImageableY()), (int) selectedArea.getImageableWidth(), (int) selectedArea.getImageableHeight() - 72);
srp = new SimpleResultPrinter(resultToPrint, margin);
SimpleResultPrinterSimulator srpSimulation = new SimpleResultPrinterSimulator(resultToPrint, margin);
// total pages simulation
int totalPages = 0;
try
{
while (srpSimulation.print(selectedArea, totalPages) != Printable.NO_SUCH_PAGE)
{
totalPages++;
}
}
catch (PrinterException e)
{
e.printStackTrace();
}
printerJob.setPrintable(srp, widenedPage);
if (printerJob.printDialog())
{
try
{
printerJob.print();
}
catch (PrinterException exc)
{
System.out.println(exc);
}
}
}
}
Method print in SimpleResultPrinter
public int print(Graphics g, PageFormat page, int pageIndex) throws
PrinterException {...}
Method print in SimpleResultPrinterSimulator
public int print(PageFormat page, int pageIndex) throws PrinterException
{...}
You need to know about this:
MeasuredBox and ResultContainer are my custom classes that have no meaning in this conception - treat them as a "some classes".
Class SimpleResultPrinter which is Printable has method print(...) which is called by printerJob.print().
SimpleResultPrinterSimulator has the same method print(...) like SimpleResultPrinter except there is no draw actions in it for better performance and of course SimpleResultPrinterSimulator has only this method. Also SimpleResultPrinterSimulator extends Component because i need Graphics object to measure font height.
My solution is just to launch simulation of printing and print(...) makes the same actions like will be done soon during printing except of real drawing to any Graphics object. Finally i recieves total number of pages.
I know this is not elegant and i make the same action twice (lost of performance), but i don't see any other way to estimate number of pages in complex printing.

Related

Get user input in a JFrame Java file before main program starts

I will need to do various test runs with the KDTreeTester.java file attached below. It'd be pretty inefficient to change the values (w, h, n, x) every time I finish a run and recompile the file.
That's when I thought it would be pretty handy to just type the values before the main program opens.
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Main program that starts the KDTree visualization
*/
public class KDTreeTester extends JFrame{
int w=400; //width of the window
int h=400; //height of the window
int n=20; //number of points
int x=5; //number of points to be searched (has to be smaller than n)
KDTreeVisualization vis; //the visualization
public KDTreeTester(){
// Initialize the visualization and add it to the main frame.
vis = new KDTreeVisualization(w, h, n);
add(vis);
The code goes on, but I think only that's the relevant part.
I need to have a box asking for the four values (width, height, number of points, number of points to be searched). The would have to be parsed from String to int I guess.
What's the best way to implement this?
As #XtremeBaumer told you :
you cant do anything before the main method starts, only if you start
via cmd
However, if you mean ask for input values before your main frame is displayed, you may use JOptionPane to collect the values.
Here is an example based on Multiple input in JOptionPane.showInputDialog and, to limit the format to integer type, on How to make JFormattedTextField accept integer without decimal point (comma)?
Just call this before instantiating your KDTreeVisualization, and call it with those input parameters once you have them.
I wouldn't call this from the constructor of your JFrame though, but rather before instantiating the JFrame, or even through a custom menu/button/whatever you may add to your frame.
NumberFormat integerFieldFormatter = NumberFormat.getIntegerInstance();
JFormattedTextField nField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField xField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField widthField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField heightField = new JFormattedTextField(integerFieldFormatter);
Object[] message = {
"Width :", widthField,
"Height :", heightField,
"n :", nField,
"x :", xField
};
int option = JOptionPane.showConfirmDialog(null, message, "Enter values", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
try {
int n = Integer.parseInt(nField.getText());
int x = Integer.parseInt(xField.getText());
int width = Integer.parseInt(xField.getText());
int height = Integer.parseInt(xField.getText());
// do stuff with the values, like instantitate your object with those parameters
// new KDTreeVisualization(width, height, n)
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "At least one of the values is invalid", "Error",
JOptionPane.ERROR_MESSAGE);
}
} else {
System.out.println("Input cancelled");
}

Issues with passing arguments to the print function in Java

public int print(Graphics g, PageFormat pf, int page, String customer_name)
throws PrinterException {
System.out.println("The value of customer name:"+customer_name);
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
String x = layout.get("");
System.out.println("The value of x is\n"+x);
/* Now we perform our rendering */
g.drawString("Customer Name: "+customer_names, 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
I want to call this method from another class, while passing an additional argument to it, customer_name. I call this method from another class as follow:
Printer print = new Printer(); //making an object to access that class Printer.java
PageFormat page = job.defaultPage();
print.print(<I have no idea what to put here for graphics>, page, 5, customer_name_field.getText());
When I call the method print.print, I gives the message that it requires Graphics, PageFormat, int, String. But what should I put for Graphics, I have no idea?
It's not working because it looks like you're going about it wrong:
Your print method has an extra String parameter tacked to the end which prevents it from being a true method override for a class that implements Printable.
You're trying to call your print method directly scrounging around for a Graphics context when you shouldn't consider doing this.
If you just want to print some text then you need to follow the first sections of the Printing Tutorial. Your print method above does not conform with a Printable's print(...) method override. Please do yourself a favor and follow the tutorial. I've given you the link.
Consider creating a class that implements Printable, passing your String as a single parameter to the class's constructor, and use this to set an instance field. The print(...) method should match that found in the tutorial, should have an #Override annotation, and most important will never be called directly by you. Your PrinterJob instance will do the printing behind the scenes.
Note, that if your goal is to print a Swing GUI, then the steps are different, since Swing GUI's carry much of the innate machinery for printing within them.
Depends on the class you're in, there's a high chance that the UI framework element you are currently using already provides you with the getGraphics() function.
You might even want to override the paint() function of the class you're using, and call this method you have written from that. It depends on your use-case.
This is my github project path where i did the code for same. https://github.com/knikam/Mobile_shop_management/blob/master/src/mobile_shop_mangment/Sell_mobile.java
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
String x = layout.get("");
System.out.println("The value of x is\n"+x);
/* Now we perform our rendering */
String customer_namescustomer_names.getText();**This is work for me**
g.drawString("Customer Name: "+customer_names, 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}

Java printing stretched on Konica - Minolta PagePro printers

I am using JasperReports to print from my Java application. All the prints were OK for all costumers for quite a bit of time, but recently we have installed the software for couple of costumers that all have Konica Minolta PagePro 1200 or 1350W printers. And all those costumers get the same result - the printing is "stretched" - as if I printed A4 report on A5 paper. Except that I am printing A4 on A4 and on all other printers it works fine.
Does anyone have any idea what might be the cause?
This questions seems similar to https://stackoverflow.com/questions/15854722/jasper-report-printing-stretched but the workaround presented there (messing with printer paper size etc.) did not lead to desired result.
Here is an example of the print:
And this is what the print should look like (there are slighlty different data, but I guess the problem is clear)
Thanks for any suggestions.
EDIT:
The issue can be reproduced with a simple direct call to printing API:
import java.awt.*;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
/**
*
* #author MaCe
*/
public class PrintTest {
static Printable printable = new Printable() {
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D grx = (Graphics2D)graphics;
grx.drawLine(20,20, getPageWidth() / 2, 20);
return Printable.PAGE_EXISTS;
}
};
protected static int getPageWidth() {
//width of A4 in 1/72 of inches
return 595;
}
protected static int getPageHeight() {
//height of A4 in 1/72 of inches
return 842;
}
public static void main(String args[]) throws PrinterException {
Frame f = new Frame();
f.show();
//Build a buggy print job using PrinterJob class
PrinterJob printJob = PrinterJob.getPrinterJob();
/**
* Fix for bug ID 6255588 from Sun bug database
*/
try {
printJob.setPrintService(printJob.getPrintService());
} catch (PrinterException e) {
}
PageFormat pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
printJob.setJobName("Buggy output");
pageFormat.setOrientation(PageFormat.PORTRAIT);
paper.setSize(getPageWidth() , getPageHeight());
paper.setImageableArea(
0,
0, getPageWidth(), getPageHeight());
pageFormat.setPaper(paper);
Book book = new Book();
book.append(printable, pageFormat, 1);
printJob.setPageable(book);
if (printJob.printDialog()) {
printJob.print();
}
//Build a good print job using PrintJob class
PrintJob pjob = f.getToolkit().getPrintJob(f, "Good output", null);
if (pjob!=null) {
Graphics g = pjob.getGraphics();
g.drawLine(20, 20, pjob.getPageDimension().width / 2, 20);
pjob.end();
}
System.exit(0);
}
}
Now this code produces two prints that on most printers both print a line from the top left corner of the paper to the middle of the paper. However on the aforementioned printers the first print creates a thick line across the whole paper (200% scale) - the second variant is however OK even on Minolta printers.
Seems like a Java bug, since all other programs print normally with the printer. The bug was accepted by Sun, but closed as they could not get their hands on the correct printer (https://bugs.openjdk.java.net/browse/JDK-804159)
Is your platform windows/mac/linux?
I've had trouble with printing with label printers when the print job's media hint did not communicate fully through to the driver. Its actually very similar to your problem, stretched prints, skipped pages etc,.
I dont know if you have code to modify, but we had to switch to System Printing instead of Java Printing. Then with the System Printing settings, (windows spooler in my case), I had to create a printer with the exact driver settings needed to print properly. Its rarely anything you can really do in JasperReports other than specify the width/height/orientation.
I assume your report prints fine when saved to PDF, then print from a PDF Viewer.
I have found a workaround to the problem. The solution is to scale the graphics to half before printing. This is not easy to do with JasperReports, as the graphics object is not exposed anywhere. However, my app uses #AspectJ so I managed to fiddle with the graphics through a custom aspect:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class JRMinoltaAspect {
#Before("execution(int net.sf.jasperreports.engine.print.JRPrinterAWT.print(java.awt.Graphics, java.awt.print.PageFormat , int )) && args(g, pf, ..)")
public void scalePrint(Graphics g, PageFormat pf){
if(pf.getOrientation() == PageFormat.LANDSCAPE){
g.translate(0, (int)pf.getHeight() / 2 );
}
((Graphics2D)g).scale(0.5, 0.5);
}
}

Printing multiple JTables as one job -- Book object only prints 1st table

For a number of reasons, I'm trying to combine the output of multiple JTables into a single print job. After tearing my hair out trying to build PDFs, and combing the Java API, I settled on the Book class. My printing code currently looks like this.
try {
PrinterJob printer = PrinterJob.getPrinterJob();
//Set 1/2 " margins and orientation
PageFormat pf = printer.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper();
double margin = 36; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
pf.setPaper(paper);
Book printJob = new Book();
// Note for next line: getAllTables() returns an ArrayList of JTables
for (JTable t : getAllTables() )
printJob.append(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf,2);
printer.setPageable(printJob);
System.out.println(printJob.getNumberOfPages());
if (printer.printDialog())
printer.print();
} catch (PrinterException e) {
e.printStackTrace();
}
Primary Problem: Only the output from the "first" table is printing. I've made sure the for loop iterates correctly, and debugging statements have shown that every table is being added to the Book. Changing order of tables has no effect. Changing the print mode to PrintMode.NORMAL, does appear result in pieces of the other tables being printed. However, I run into a slew horizontal pagination problems, as the table width frequently exceeds page width (and it still doesn't explain why PrintMode.FIT_WIDTH isn't working)
A secondary question: How can I detect the correct number of pages in each printable? Most of my tables are two pages long, so for the moment, I'm just adding 2 pages each time I append. I read "somewhere" that using Book.UNKNOWN_NUMBER_OF_PAGES as the page number will fix this problem, but that only leads to an IndexOutOfBounds exception in the API's code. I've considered calling print myself until I get NO_PAGE_EXISTS, but I'd need a Graphics object with the proper page dimensions (and I have no idea how to get that).
Lastly: If the Book approach is hopeless, how else can I combine the output of multiple JTables (ie. multiple printables) into a single job? I looked into exporting the table as a PDF, but JTable's built-in pagination is so nice, I'd rather not have to do it myself. My last resort is to just give up and use iText's built in table function to construct a copy of the table.
Edit 3: Per my comment below, I got this working by generating a printable, determining the # of pages, and then generating a new one. I also modified Durendal's wrapper to spare us the hassle of iterating over each page. The code from the wrapper is
class PrintableWrapper implements Printable
{
private Printable delegate;
private int offset;
public PrintableWrapper(Printable delegate, int offset) {
this.offset = offset;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, pageIndex-offset);
}
}
I put Durendal's code for determining number of pages in its own function
public int getNumberOfPages(Printable delegate, PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS) {
++numPages;
} else {
break;
}
}
return numPages;
}
After I create the book object, my printing code looks like this
int totalPages = 0;
for (DragNDropTable t : getAllTables() )
{
int pages = getNumberOfPages(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf);
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
printJob.append(new PrintableWrapper(p,totalPages), pf, pages);
totalPages += pages;
}
printer.setPageable(printJob);
if (printer.printDialog())
printer.print();
And it works like a charm!
Edit 2: (you can skip this) I tried Durendal's answer. While I'm printing enough pages, multipage printables are printing the last page multiple times (once for every page in the printable). This is the same problem I discussed in my 1st edit (below), and I have no idea why this is happening, and my debugging statements are saying that it's printing all of the pages correctly, but the last page of a multipage printable is printed in place of each page. Code is attached. Insight is appreciated (and repayed with virtual cookies)
try {
PrinterJob printer = PrinterJob.getPrinterJob();
//Set 1/2 " margins and orientation
PageFormat pf = printer.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper();
double margin = 36; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
pf.setPaper(paper);
Book printJob = new Book();
// Note for next line: getAllTables() returns an ArrayList of JTables
for (JTable t : getAllTables() )
{
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
int pages = getNumberOfPages(p, pf);
for (int i=0; i < pages; i++)
printJob.append(new PageWrapper(p,i), pf);
}
printer.setPageable(printJob);
System.out.println(printJob.getNumberOfPages());
if (printer.printDialog())
printer.print();
} catch (PrinterException e) {
e.printStackTrace();
}
public int getNumberOfPages(PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS)
++numPages;
else
break;
}
return numPages;
}
I'm using the unmodified PageWrapper that Durendal gave below.
Edit 1: (You can skip this) Dovetailing off of Durendal's answer, I tried to make a wrapper that spares us the chore of iterating over the pages ourselves. Unfortunately, it doesn't seem to work correctly on multipage printables, printing the same page multiple times in the document. I post it, simply because someone may get it to work, and it's slightly more convenient to use.
class PrintableWrapper implements Printable
{
private Printable delegate;
private int offset;
public PrintableWrapper(Printable delegate, int offset) {
this.offset = offset;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, pageIndex-offset);
}
public int getNumberOfPages(PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS)
++numPages;
else
break;
}
return numPages;
}
}
My printing code now looks like this (after I set the page format)
Book printJob = new Book();
int totalPages = 0;
for (DragNDropTable t : getAllTables() )
{
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
PrintableWrapper pw = new PrintableWrapper(p, totalPages);
totalPages += pw.getNumberOfPages(pf);
printJob.append(pw, pf,pw.getNumberOfPages(pf));
}
printer.setPageable(printJob);
if (printer.printDialog())
{
printer.print();
}
I had the very same Problem recently. The Book class by itself is useless, because if you add Printables to it, when the Book is printed it will pass the pageIndex from the Book to the Printable at pageIndex.
That is in most cases not what you want.
Create a simple Printable Wrapper that can remember a pageIndex to be used for the Printable delegate and add those to the Book:
class PageWrapper implements Printable {
private Printable delegate;
private int localPageIndex;
public PageWrapper(Printable delegate, int pageIndex) {
this.localPageIndex = pageIndex;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, localPageIndex);
}
}
Now you need to iterate trough each page of each Printable/Pageable and add a wrapper instance (that knows the pageIndex into its delegate) to the Book. That solves the problem that Book passes the wrong pageIndex to the printables added to it (its easier for Pageables than Printables).
You can detect the number of Pages in a Printable by printing it ;) Yes, I'm serious:
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = printable.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS) {
++numPages;
} else {
break;
}
}
Its important you obtain your PageFormat instance from the actual PrinterJob you want to print to, because the number of pages depends on the page format (paper size).

Java: Checking if PC is idle

This is a rather tricky question as I have found no information online. Basically, I wish to know how to check if a computer is idle in Java. I wish a program to only work if the computer is in active use but if it is idle then to not.
The only way i can think of doing this is hooking into the mouse/keyboard and having a timer.
MSN Messenger has that "away" feature, I wish for something similar to this.
Java has no way of interacting with the Keyboard, or Mouse at the system level outside of your application.
That being said here are several ways to do it in Windows. The easiest is probably to set up JNI and poll
GetLastInputInfo
for keyboard and mouse activity.
Im not a professional, but i have an idea:
you can use the java's mouse info class to check mouse position at certian intervals say like:
import java.awt.MouseInfo;
public class Mouse {
public static void main(String[] args) throws InterruptedException{
while(true){
Thread.sleep(100);
System.out.println("("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
}
}
}
replace the print statement with your logic, like if for some interval say 1 min the past position of mouse is the same as new position (you can simply compare only the x-coordinates), that means the system is idle, and you can proceed with your action as you want (Hopefully it is a legal activity that you want to implement :-)
Besure to implement this in a new thread, otherwise your main program will hang in order to check the idle state.
You can solve this with the help of Java's robot class.
Use the robot class to take a screenshot, then wait for lets say 60 seconds and take another screenshot. Compare the screenshots with each other to see if any changes
has happened, but don't just compare the screenshots pixel by pixel. Check for the percentage of the pixels that has changed. The reason is that you don't want small differences like Windows clock to interfere with the result. If the percentage is less that 0.005% (or whatever), then the computer is probably idling.
import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
public class CheckIdle extends Thread {
private Robot robot;
private double threshHold = 0.05;
private int activeTime;
private int idleTime;
private boolean idle;
private Rectangle screenDimenstions;
public CheckIdle(int activeTime, int idleTime) {
this.activeTime = activeTime;
this.idleTime = idleTime;
// Get the screen dimensions
// MultiMonitor support.
int screenWidth = 0;
int screenHeight = 0;
GraphicsEnvironment graphicsEnv = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice[] graphicsDevices = graphicsEnv.getScreenDevices();
for (GraphicsDevice screens : graphicsDevices) {
DisplayMode mode = screens.getDisplayMode();
screenWidth += mode.getWidth();
if (mode.getHeight() > screenHeight) {
screenHeight = mode.getHeight();
}
}
screenDimenstions = new Rectangle(0, 0, screenWidth, screenHeight);
// setup the robot.
robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
idle = false;
}
public void run() {
while (true) {
BufferedImage screenShot = robot
.createScreenCapture(screenDimenstions);
try {
Thread.sleep(idle ? idleTime : activeTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
BufferedImage screenShot2 = robot
.createScreenCapture(screenDimenstions);
if (compareScreens(screenShot, screenShot2) < threshHold) {
idle = true;
System.out.println("idle");
} else {
idle = false;
System.out.println("active");
}
}
}
private double compareScreens(BufferedImage screen1, BufferedImage screen2) {
int counter = 0;
boolean changed = false;
// Count the amount of change.
for (int i = 0; i < screen1.getWidth() && !changed; i++) {
for (int j = 0; j < screen1.getHeight(); j++) {
if (screen1.getRGB(i, j) != screen2.getRGB(i, j)) {
counter++;
}
}
}
return (double) counter
/ (double) (screen1.getHeight() * screen1.getWidth()) * 100;
}
public static void main(String[] args) {
CheckIdle idleChecker = new CheckIdle(20000, 1000);
idleChecker.run();
}
}
Nothing in the platform-independent JRE will answer this question. You might be able to guess by measuring clock time for a calculation, but it wouldn't be reliable. On specific platforms, there might be vendor APIs that might help you.
1) Make a new thread.
2) Give it a super super low priority (the lowest you can)
3) Every second or two, have the thread do some simple task. If super fast, at least 1 CPU is prolly idle. If it does it slow, then at least 1 core is prolly not idle.
Or
Just run your program at a low priority. That will let the OS deal with letting other programs run over your program.

Categories