/    Sign up×
Community /Pin to ProfileBookmark

How to create an applet from the java file?

I am trying to create an applet from the java file, the code has to take an image from clipboard and save it into the file. The programm is working fine when executed via “java file_name”. But my goal is to implement this in to the web for helpdesk purpose. So user can click the button the programm executes saves the image from the clipboard, and then via javascript I upload it to the server.

From some googleing I understood that to make Java work on the web some modification should be done to the source code. Can someone point me in to the right direction? Or even better provide any sample of an easy applet or something?

source of java file:

[code]
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java
.awt
.datatransfer
.Transferable;
import java
.awt
.datatransfer
.UnsupportedFlavorException;

import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;

public class ImageFromClipboard {
public static void main(String[] args) throws UnsupportedFlavorException,
IOException {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = clipboard.getContents(null);

if (transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
System.out.println(“transferable supports image flavor”);
}

Image image = (Image) transferable.getTransferData(DataFlavor.imageFlavor);

BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();

ImageIO.write(bimage, “jpg”, new File(“C:/jv0001.jpg”));
}
}
[/code]

On the HTML page i have this tag:

[code]
<applet code=”ImageFromClipboard.class” codebase = “../../../new_design/test_folder” width=”1″ height=”1″ alt=”qqq” title=”qaz”>
</applet>
[/code]

Thanks in advance.

to post a comment
Java

5 Comments(s)

Copy linkTweet thisAlerts:
@agent_x91Jan 26.2007 — Anything extending java.applet.Applet can be put on a website as an applet.
Copy linkTweet thisAlerts:
@sovikauthorJan 26.2007 — I have managed to make an applet from this source, and puting it on the web page. But I just need a confirmation of one litle thingy that I have noticed.

Java applets cannot create files on client machines, or create files on the server using client files? Right?

If so then all my effort is clueless, exept of learning new things of course. ?
Copy linkTweet thisAlerts:
@agent_x91Feb 05.2007 — As applets can be accessed straight from the web, they do not have the ability to modify files on the client's computer: you could potentially create a virus which infects every visitor of a website if this were possible.
Copy linkTweet thisAlerts:
@KostyaMar 29.2007 — Hi.

I tried to use your source code for solving my tasks.

I add button. When I click on it I call your main method that I ccalled doMain().

It works perfect when I run it in JDeveloper in AppletViewer.

But when I tried to use it in Browser i find that file with expected picture is empty.

I run it in cmd using appletviewer command and saw the next error:

D:softProgrammersJavaJDeveloperjdkbin>appletviewer Applet2.html

Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.awt.AWTPermission accessClipboard)

at java.security.AccessControlContext.checkPermission(AccessControlConte

xt.java:264)

at java.security.AccessController.checkPermission(AccessController.java:

427)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)

at java.lang.SecurityManager.checkSystemClipboardAccess(SecurityManager.

java:1377)

at sun.awt.windows.WToolkit.getSystemClipboard(WToolkit.java:611)

at Applet2.doMain(Applet2.java:35)

at Applet2.button1_actionPerformed(Applet2.java:80)

at Applet2.access$000(Applet2.java:32)

at Applet2$1.actionPerformed(Applet2.java:64)

at java.awt.Button.processActionEvent(Button.java:388)

at java.awt.Button.processEvent(Button.java:356)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh

read.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre

ad.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

I don't know what to do next.

Best Regards, Kostya Proskudin!

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Help me to solve my problem. If you create this applet with the same aim you have to knew an answer.

Best Regards, Kostya!
Copy linkTweet thisAlerts:
@KostyaMar 29.2007 — Hi I tried to use your source code for my aims.

I add one button. When I clecked on it i call your main method. I called it doMain().

It works perfect when I run it in JDeveloper in AppletViewer.

But when I tried to use it in Browser i find that file with expected picture is empty.

I run it in cmd using appletviewer command and saw the next error:

D:softProgrammersJavaJDeveloperjdkbin>appletviewer Applet2.html

Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.awt.AWTPermission accessClipboard)

at java.security.AccessControlContext.checkPermission(AccessControlConte

xt.java:264)

at java.security.AccessController.checkPermission(AccessController.java:

427)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)

at java.lang.SecurityManager.checkSystemClipboardAccess(SecurityManager.

java:1377)

at sun.awt.windows.WToolkit.getSystemClipboard(WToolkit.java:611)

at Applet2.doMain(Applet2.java:35)

at Applet2.button1_actionPerformed(Applet2.java:80)

at Applet2.access$000(Applet2.java:32)

at Applet2$1.actionPerformed(Applet2.java:64)

at java.awt.Button.processActionEvent(Button.java:388)

at java.awt.Button.processEvent(Button.java:356)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh

read.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre

ad.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

I don't know what to do. If you created this applet for the same goal you have to knew the answer for my question.

Best Regards, Kostya!
×

Success!

Help @sovik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...