Desktop Sharing in java - java

I want to create a very simple RMI code which just share the desktop.
I have created my classes and also remote interface.in the Share class ,I have an execute method which will return the image of the client's desktop.but I don't know that how can I get that image ?or how can i store it?
please help me,thanks.
Share class:
class Share implements Task<DesktopPaneUI>,Serializable{
public Share(){
}
public DesktopPaneUI execute() {
}
}
Task class:
public interface Task<T> {
T execute();
}

import java.awt.*
import java.awt.image.*
BufferedImage screenShot = new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())
)

Related

Vaadin Grid Custom Renderer not working

I want to build a custom renderer for some of my grids columns to hide the text if the user doesn't have the right to read it. It's still important that the data is accessible even if the user is not able to read it.
So I wrote a custom renderer which looks like this:
package <package>.util.renderer;
import com.vaadin.client.renderers.Renderer;
import com.vaadin.client.widget.grid.RendererCellReference;
import <package>.util.CustomSecurityConstants;
import <package>.BaseUI;
public class BlockedStringRendererClient implements Renderer<String> {
private boolean canReadBlocked = BaseUI.getCurrentPrincipal().get().getAuthorities().contains(CustomSecurityConstants.READ_PERMISSION_BLOCKED);
#Override
public void render(RendererCellReference rendererCellReference, String s) {
if (canReadBlocked) {
rendererCellReference.getElement().setInnerText(s);
} else {
rendererCellReference.getElement().setInnerText("");
}
}
}
Then I wrote the server side of the renderer, following this tutorial https://vaadin.com/docs/-/part/framework/clientsidewidgets/clientsidewidgets-grid.html
package <package>.util.renderer;
import com.vaadin.ui.Grid;
public class BlockedStringRendererServer extends Grid.AbstractRenderer<String> {
public BlockedStringRendererServer() {
super(String.class);
}
}
And finally the connector to connect these components:
package <package>.util.renderer;
import com.vaadin.client.connectors.AbstractRendererConnector;
import com.vaadin.shared.ui.Connect;
#Connect(BlockedStringRendererServer.class)
public class BlockedStringRendererConnector extends AbstractRendererConnector<String> {
#Override
public BlockedStringRendererClient getRenderer() {
return (BlockedStringRendererClient) super.getRenderer();
}
}
But now when I try to use the connector like follows:
grunddatenGrid.getColumn("name").setRenderer(new BlockedStringRendererServer());
The grid doesn't show any columns that contains strings anymore.
I really don't know what I'm doing wrong but I think it might has to do with the Connector-Annotation not working as expected because when I try to debug the client side logic it doesn't even get called.
Can somebody point me to what steps I'm missing?
Kind regards,
Fabian

How to make google datastore persistent in java

It's very slow to test datastore api in cloud in my country, I hope a way to test datastore code in local. so I found following code:
package ro.gae
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig
import com.google.appengine.tools.development.testing.LocalServiceTestHelper
/**
* Created by roroco on 8/23/15.
*/
trait LcDatastore {
LocalServiceTestHelper h = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
def iiLcDs() {
h.setUp()
}
def closeLcDs() {
h.tearDown()
}
}
But above code only save records in memory, and these records will disappear after code finish. I hope a way to save records in my disk to simplify my code
update
I hope the way don't need to start dev_server since dev_server need a long startup in java
did you try something like this?
public class MyTestClass {
LocalServiceTestHelper localServiceTestHelper;
#BeforeClass
public void beforeClass(){
LocalDatastoreServiceTestConfig localDatastoreServiceTestConfig = new LocalDatastoreServiceTestConfig();
localDatastoreServiceTestConfig.setBackingStoreLocation("storagefilelocation");
localDatastoreServiceTestConfig.setNoStorage(false);
localServiceTestHelper = new LocalServiceTestHelper(localDatastoreServiceTestConfig);
}
#Test
public void testSomething(){
}
}
edit: By the way, I aggree with #Igor Artamonov that this is very likely a bad idea. It should work though. I can also think of a few things that this could help with; like importing huge datasets into the dev local storage by running a unit test (or any external application).

Call DLL from Java using JNA

I am new to accessing DLLs from Java using JNA. I need to access methods from a class within a DLL(written in .net). Form this sample DLL below, I am trying to get AuditID and Server ID. I am ending with the following error while I am running my code. Any guidance really appreciated.
/// Error ///
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetEnrollcontext': The specified procedure could not be found.
//DLL File Code//
SampleDLL.ProfileEnroll enrollcontext = new SampleDLL.ProfileEnroll();
enrollcontext.Url =” url”;
enrollcontext.AuditIdType = SampleDLL.ProfileId;
enrollcontext.AuditId = “22222222 “;
enrollcontext.ServerId = “server1”;
/// Java Code ///
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import dllExtract.DLLExtractTest.SampleDLL.Enrollcontext;
public class SampleDLLExtract {
public interface SampleDLL extends Library {
SampleDLL INSTANCE = (SampleDLL) Native.loadLibrary("SampleDLL",
SampleDLL.class);
public static class Enrollcontext extends Structure {
public String auditId;
public String serverId;
}
void GetEnrollcontext(Enrollcontext ec); // void ();
}
public static void main(String[] args) {
SampleDLL sdll = SampleDLL.INSTANCE;
SampleDLL.Enrollcontext enrollContext = new SampleDLL.Enrollcontext();
sdll.GetEnrollcontext(enrollContext);
System.out.println(sdll.toString(sdll.GetEnrollcontext(enrollContext)));
}
}
in fact there is a solution for you to use C#, VB.NET or F# code via JNA in Java (and nothing else)! and it is also very easy to use:
https://www.nuget.org/packages/UnmanagedExports
with this package all you need to do is, add [RGiesecke.DllExport.DllExport] to your methods like that:
C# .dll Project:
[RGiesecke.DllExport.DllExport]
public static String yourFunction(String yourParameter)
{
return "CSharp String";
}
Java Project:
public interface jna extends Library {
jna INSTANCE = (jna) Native.loadLibrary("yourCSharpProject.dll", jna.class);
public String yourFunction(String yourParameter);
}
use it in the code:
System.out.println(jna.INSTANCE.yourFunction("nothingImportant"));
Viola!
As already mentioned it works very easy, but this solution has some limitations:
only available for simple datatypes as parameter & return values
no MethodOverloading available. yourFunction(String yourParameter) and yourFunction(String yourParameter, String yourSecondParameter) does not work! you have to name them differently
Use arrays as parameter or return values. (JNA offers StringArray, but I am not able to use them in C#) (maybe there is a solution, but I couldn't come up with one so far!)
if you export a method you can't call it internally in your C# code (simple to bypass that by the following:
.
[RGiesecke.DllExport.DllExport]
public static Boolean externalAvailable(String yourParameter)
{
return yourInternalFunction(yourParameter);
}
With C# it works great, with VB.NET and F# I have no experience.
hope this helps!

How do I construct and use an object inside the inner class of a nested class?

I posted this earlier, but it wasn't quite up to par with stackoverflow standards. I cleaned up the code and articulated my question a bit more, so here goes:
I'm making a two player asteroids game in an applet for a CS1 project. I'm trying to figure out how to implement sound effects using methods I can call at certain times. I found this tutorial (http://www.dreamincode.net/forums/topic/14083-incredibly-easy-way-to-play-sounds/) on doing just that, but I'm having some trouble with the nested loop syntax.
How do I construct the 'Sound' object within the Sound class from a different class (in my case, AsteroidsGame.java) ?
Because of the messiness of the tutorial code, here's mine for improved readability.
//Import Statements
import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Audioapp extends JApplet
{
public class Sound // Holds one audio file
{
private AudioClip song; // Sound player
private URL songPath; // Sound path
Sound(String filename)
{
try
{
songPath = new URL(getCodeBase(),filename); // Get the Sound URL
song = Applet.newAudioClip(songPath); // Load the Sound
}
catch(Exception e){} // Satisfy the catch
}
//Loops audio file
public void playSound()
{
song.loop();
}
//Stops audio file
public void stopSound()
{
song.stop();
}
//Plays audio file once
public void playSoundOnce()
{
song.play();
}
} //Closes Sound()
public void init()
{
Sound testsong = new Sound("song.mid");
testsong.playSound();
}
} //Closes Audioapp()
Edit 1: Just remembered someone from the last thread suggested I post what this does when I compile/run it. This .java on it's own does nothing; but it does indeed compile without errors when placed with the rest of my project.
Edit 2: Thanks a lot Zim-Zam for all your help, if anyone finds this thread and has the same issue, please consult his comments.
I recommend that you change your inner class to public static class Sound - this will let you construct instances of the class without needing an instance of Audioapp.
Then, to create an instance of Sound, you simply treat it as though its name were Audioapp.Sound, e.g. Audioapp.Sound sound = new Audioapp.Sound()
If the inner class isn't static, then you would use Audioapp.Sound sound = audioApp.new Sound(), where audioapp is an instance of Audioapp

Overriding a method in a subclass

I'm trying some alterations in minecraft src. I'm trying to override a method in a class so I don't have to edit the original class.
In the regular class I want to alter this method:
public void sendChatMessage(String par1Str)
{
this.sendQueue.addToSendQueue(new Packet3Chat(par1Str));
}
So in my subclass I have this code:
package cobalt.gui;
import cobalt.hacks.*;
import net.minecraft.client.Minecraft;
import net.minecraft.src.EntityClientPlayerMP;
import net.minecraft.src.NetClientHandler;
import net.minecraft.src.Session;
import net.minecraft.src.World;
public class Console extends EntityClientPlayerMP {
public Console(Minecraft par1Minecraft, World par2World,
Session par3Session, NetClientHandler par4NetClientHandler) {
super(par1Minecraft, par2World, par3Session, par4NetClientHandler);
}
#Override
public void sendChatMessage(String par1Str) {
if (par1Str.startsWith(".help")) {
//Do stuff
return;
}
}
}
From my understanding, anytime a method is called, it should be "redirected" for the subclass to handle? (Tell me if I'm wrong ha)
The if statement does work correctly if I modify the original class.
Thank you very much!
This would only work if somehow the rest of the minecraft code starts using your class, Console, where it meant to use EntityClientPlayerMP. Without that, your function will not be called.
If you want to change the behavior of the game, the easiest way would be to change EntityClientPlayerMP itself. If you want to use the modified class Console elsewhere in the code, then what you have done is fine.
It depends on the actual object type. If the object is of type Console e.g. EntityClientPlayerMP obj = new Console(..) and obj.sendChatMessage(..) it'll work. But if the object itself is of type EntityClientPlayerMP like new EntityClientPlayerMP(..) It won't work

Categories