Detect repainted area of screen in java - java

I am creating an applet which will do something like screen sharing . For that I am using Robot class to capture screen area . I want to capture the dirty / repainted areas after first screen shot. I think RepaintManager manager can be used for that. but i dont know how can I use it. I am a novice so a java code will be helpfull. thanks

Check out this custom repaint manager for checking if repaints happen on the EDT to see how it overrides addDirtyRegion.

Related

Change size of JFrame according to user selected image

I'm about to write a program using Java and i want it to have the next behavior:
Start with a small screen, just one button (i'm going for the JMenuBar) for the user to select a image file (a country or state map)
Once selected the image file, i'll need to resize the frame to the size of the selected image, and put the image as background.
when the user clicks somewhere inside the frame (click on a state or city) the program will have to create a visual object there, a circle, square or any form in that coordinates.
will need also a listener in those objects to know when they are clicked.
Summary: User has to select an image and trace a graph on it.
I am not asking for the code to do this. I would like to have some ideas about which components use to achieve this since i have been reading and there are plenty of ways to set the background image and stuff. But, considering the requirements, can you recommend me which components to use? I am a bit short of time since i've been given only about a week to code this, otherwise i would try all the alternatives by myself.
Some answer like:
"use a label to set the background and then resize the frame by this way: (some stuff) and then you can create a class extending from JLabel to create the circles with the listeners...." that would be enough help
I hope I was clear, any idea is welcome
Many thanks!
If you're going to stick with Swing I would use a JFileChooser to select the image. Once you got the image you can easily resize the JFrame by using the frame.setSize(image.getWidth(), image.getHeight());
To listen for mouse clicks inside your JFrame you need to use a MouseListener, make sure to add it to the frame, I always forget doing that.
Not sure whether you've succeeded drawing images/shapes at all. If not, you need to use a JPanel, check this topic if you need extra help.
If you are going to use a "JFrame " then you should definitely use Swing JFrames JPanels, and JLabels (as well as any other JComponents you need.) to accomplish this. Use only one JFrame. Use JPanel as the content pane/background for your JFrame and add everything else to it. But I would also suggest learning and using JavaFX because its the newest and I think it would be the easiest to use to do something like this. But if you only have a week and you know some swing use what you know. If you need more information post some code. Or ask a more direct question.

.pack() causes the components to be in the wrong place but works after window resize

I am creating a game in java using Swing.
I am using
mainJFrame.setExtendedState(mainJFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
on my JFrame to start it as a maximized window.
After this I am using
mainJFrame.pack();
to be able to get the correct x & y coordinates of components in the GUI. I need those coordinates since i'm painting custom images to the GUI. So far everything is fine.
When a user has passed a level, i rebuild the GUI to updated it to the new level settings and then i call pack() again.
When calling pack() the components are placed in the wrong place, slightly below where they are supposed to be.
However, manually resizing the window causes the components to go to the right place again.
I have tried calling mainJFrame.revalidate() and mainJFrame.repaint() after the pack() but this gives no effect.
If needed I can supply printscreens and code.
Thanks for your time.
Manually resizing the window validates the enclosing container and all of its subcomponents. To avoid the effect you describe, call setVisible() only after you have added components and invoked pack(). More related suggestions may be found here. If the problem persists, please edit your question to include an mcve that exhibits the problem.

SWT Based GUI getting black on maximizing from minimizing staus in Full Size

I've a GUI based on Eclipse SWT/RCP . When that GUI is in Full Size and I minimize it and then maximize it , I see a dark/black over the Ui for a second or more and then it becomes normal. I want to know, what might be the reason for same
Following is the screenshot -
This usually indicates that you have a code that runs longer than it should on an event listener and the paint events are not dispatched until that code is done. Hence you see the black areas until they are dispatched and painted. I recommend checking the logic on listeners especially for resize and focus events if you have any.

How to make the desktop pane that Netbeans has

So when I start up Netbeans, they create a little panel on the desktop for showing the progress of loading. I'm pretty sure Microsoft Office 2010 uses this too. I was curious how to make one of those in java?
I looked through the API and saw JDesktopPane. But I don't think that's what I'm looking for unless you can take that and put in on the actual desktop, but I'm unsure. Thanks in advance!
you can actually do it using JPanel.. you don't need to do anything else..no need of desktop pane
all you need to do is design a JPanel and put a progress bar inside it that will link to a process and show how much it has been completed.
JPanel doesn't have normal frame functionality like minimize, Close etc and will act exactly as you are trying to make up.
Update : Just tried doing what you wanted.
You need to start working on JFrame. and set its decoration to false.
in Netbeans, you can just go to Frame properties and set Undecorated to true
or inside code you can just write setUndecorated(true);
then you have to design your frame, put a progress bar inside it, link it to a function, set its onTop value to true (which means it will always be on top) and set its position to center of screen. done!! you are ready with your window!!

How to show loading

I want to know how to show the loading of an application. Normally (let's say Netbeans IDE,)
the application will show how far it has loaded and how far to load. Also, loading classes as well can be seen in welcome page. How is this is done and how we can show our classes loading and loaded status (in a progress bar) in our applications? Also let's say that we have used Hibernate, and there is a login on first page, it takes time to start but only for the starting (I think it is because that the Hibernate factory is getting started and load its classes). The answer is hoped in Java.
I want to know how to show the loading of an application.
See the java.awt.SplashScreen class. But..
..show how far it has loaded and how far to load.
..with a customized image. Call SplashScreen.createGraphics() to get a Graphics object (from the image defined as a splash in the manifest) which can be drawn on. Draw the progress bar at an appropriate location on the Graphics object. For the progress bar, either use a JProgressBar as already mentioned, or to keep it lean (using pure AWT), draw one big Rectangle to represent the bar, and fill a smaller Rectangle to represent the progress.
See also How to Create a Splash Screen in the Java Tutorial. (Where you can see that image above.)
That sounds for using JProgressBar, combined with Splash Screen or JDialog.
Be sure that JProgressBar must be updated on the Event Dispatch Thread, more about that in Concurrency in Swing,
Then you have two choices as to how to update a JProgressBar correctly - by wrapping code into:
SwingWorker
Runnable/Thread
There is some support built into swing for this. See the tutorial on How to Use Progress Bars to get started.
Here is a very basic code:
JProgressBar progressBar;
progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(33); // put here the percentage you want to display...
progressBar.setStringPainted(true);

Categories

Resources