Error showing images in applet in browser - java

I'm new to Java Applets and I have a little problem.
I need to load and display images in applet loaded in browser.This is part of the code i'm using to display image:
Panel first=new JPanel();
URL url = null;
try {
url = new URL(new URL(path),"Dell_laptop.jpg");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Image img = getImage(url);
ImageIcon fimg = new ImageIcon(img);
JLabel jLabel1=new JLabel(fimg);
first.add(jLabel1);
So when it's working in eclipse, it's working flawless.But when i'm trying to load this same applet in HTML page, it's not working.Applet is loaded,but images aren't shown.Path is correct, checked it.

Related

codename one signatueComponent image upload

i have be trying to upload the signature captured from the codename one signature to my php server.the problem is that the image uploaded is a black image.Below is my code.how can i fix this
SignatureComponent sig = new SignatureComponent();
sig.addActionListener((evt)-> {
try{
img = sig.getSignatureImage();
}catch(Exception ex){
ex.printStackTrace();
}
// Now we can do whatever we want with the image of this signature.
});
Button sv = new Button("save");
sv.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt) {
try {
Label it = new Label();
it.setIcon(img);
orderHome.add(it);
ImageIO imgIO= ImageIO.getImageIO();
ByteArrayOutputStream out = new ByteArrayOutputStream();
imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
byte[] ba = out.toByteArray();
MultipartRequest request = new MultipartRequest();
String url = Global.url1 + "upload_photo.php";
request.setUrl(url);
request.addData("file",ba,"image/jpeg");
request.addArgument("order_id", order_id);
request.addArgument("customer_id", customer_id);
NetworkManager.getInstance().addToQueue(request);
and the php code
[![image uploaded][1]][1]
<?php
#SESSION_START();
require_once("../includes/functions.php");
$target_path="../uploads/";
$customer_id=$_REQUEST['customer_id'];
$order_id=$_REQUEST['order_id'];
$uid = uniqid();
$file =$uid.".jpg";
$sucess=move_uploaded_file($_FILES["file"]["tmp_name"], $target_path.$file);
the black img is the file which is uploaded to the server.the other shows the screenshot of the running app.i would like to upload the signature as shown in the screenshot
The signature generates a translucent image. JavaSE has some issues with saving translucent images as JPEGs and thus PNG works well. Another alternative would be to create an opaque image and save that as a JPEG e.g.:
Image myImage = Image.create(img.getWidth(), img.getHeight());
myImage.getGraphics().drawImage(img, 0, 0);
The new myImage will be opaque with the white color background.

Loading images from jar files (Using Eclipse IDE)

I've got a simple swing application with a JFrame that holds various components. On the JFrame I have a custom toolbar class that extends JPanel. On the JPanel I plan on adding buttons with image icons. My directory structure is as follows:
Project/src/gui (Package holds source files for application)
Project/src/images (Package holds a jar file jlfgr-1_0.jar with button icons and /or individual images files)
The issue is that I want to avoid copying the individual image files to the images package. I'd rather somewhow just load the images directly from the jar file. I've got private method that returns the appropriate icon. This method works, for example if I drag an image file to the images package and call:
button.setIcon(createIcon("/images/Save16.gif"));
private ImageIcon createIcon(String path) {
URL url = getClass().getResource(path);
ImageIcon icon = new ImageIcon(url);
if(url == null) {
System.err.println("Unable to load image: " + path);
}
return icon;
I know this is basic, but how can I get my images directly from the jar file in my current setup?
Thanks.
You can read the image from the stream, thanks to javax.imageio.ImageIO:
private ImageIcon createIcon(String path) {
BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));
ImageIcon icon = new ImageIcon(image);
return icon;
}
This is another way. The images are at location src/images
BufferedImage myPicture = null;
try {
myPicture = ImageIO.read(this.getClass().getResource("images/Picture2.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
try {
myPicture = ImageIO.read(this.getClass().getResource("images/broken_image.jpg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
panel.add(picLabel);

How to get resources working in runnable jar?

I have a Image Icon in my Jframe.I am using a Image in it.When i export into a Runnable Jar.The Image is Not displayed.
String iPath = "res/images/Mobile.png";
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setBounds(0, 0, 315, 610);
InputStream stream = (InputStream) getClass().getResourceAsStream(iPath);
JLabel mobileImageLabel;
mobileImageLabel = new JLabel(new ImageIcon(iPath));
//mobileImageLabel = new JLabel(new ImageIcon(ImageIO.read(stream)));
// mobileImageLabel = new JLabel(new ImageIcon(AppFrame.class.getResource(iPath)));
mobileImageLabel.setBounds(0, 0, 315, 610);
mobileImageLabel.setVisible(true);
layeredPane.add(mobileImageLabel, Integer.valueOf(0));
I googled & found the getResourceAsStream method.But it seems to throw NullPointerException.
So Help me in the Right Direction :)
Thanks for your Help ...
Note
The Method i Tried has been Commented
To set a image in a button I did this:
JButton yourButton = new JButton("text button");
try {
yourButton.setIcon(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/images/icon-for-your-button.png"))));
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
You must have the images inside your project like this:
In your case, the answer I think is to do this:
new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/images/icon-for-your-button.png")))
Try to replace
String iPath = "res/images/Mobile.png";
By
String iPath = "C:/..../res/images/Mobile.png";
Whenever you are working with images try to put the complete path for the image. So when you will generate your .jar file and try to run it from different place(may be possible that you want the jar should be run from desktop) you will get all the images.

How to show icon file in system tray?

I am adding my application in system tray when I close it. But it is not displaying the icon. When I try to show picture file then it works fine but when I try with an icon file it does not work. How can I display an icon instead picture?
Image image = new ImageIcon("src/resources/busylogo.jpg").getImage();
final TrayIcon trayIcon = new TrayIcon(image);
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
Better use Toolkit to load an icon.
That's a low size file, and an asynchronous load will give you less problems.
Try this code, which is recommanded by Sun.
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
final TrayIcon trayIcon = new TrayIcon(image);
try {
tray.add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
More informations here : http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/

Hows to open an .bmp/.jpeg image using Java

I am working on a JFrame/panel that will contain a button. When the user clicks the button, I want an image (which will be stored in the computer hard disk beforehand) to open on the front screen.
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//here i want a code that will somehow open the image from a given directory
}});
Any suggestions on how to go about this ? I have to tell where the image is stored and trigger a virtual 'double click' for the image to pop up on the front screen. Is that even possible using java to synchronize such computer functions?
I don't know a very short way, but I would use something like this (as qick hack to get an impression):
try {
// this is a new frame, where the picture should be shown
final JFrame showPictureFrame = new JFrame("Title");
// we will put the picture into this label
JLabel pictureLabel = new JLabel();
/* The following will read the image */
// you should get your picture-path in another way. e.g. with a JFileChooser
String path = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg";
URL url = new File(path).toURI().toURL();
BufferedImage img = ImageIO.read(url);
/* until here */
// add the image as ImageIcon to the label
pictureLabel.setIcon(new ImageIcon(img));
// add the label to the frame
showPictureFrame.add(pictureLabel);
// pack everything (does many stuff. e.g. resizes the frame to fit the image)
showPictureFrame.pack();
//this is how you should open a new Frame or Dialog, but only using showPictureFrame.setVisible(true); would also work.
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
showPictureFrame.setVisible(true);
}
});
} catch (IOException ex) {
System.err.println("Some IOException accured (did you set the right path?): ");
System.err.println(ex.getMessage());
}
I think this will work ...
Code:
process = new ProcessBuilder("mspaint","yourFileName.jpeg").start();
This will open your image file with mspaint.....
and also use *Java Advanced Imaging (JAI)*
Try this code
try
{
// the line that reads the image file
BufferedImage image;
// work with the image here ...
image = ImageIO.read(new File("C://Users//Neo//Desktop//arduino.jpg"));
jLabel1.setIcon(new ImageIcon(image));
}
catch (IOException e)
{
// log the exception
// re-throw if desired
}
I'm not sure but try this...
try
{
JLabel picture=new JLabel();
ImageIcon ic=new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("C:\\Users\\Desktop\\xyz.jpg")));
picture.setIcon(ic);
}
catch(Exception)
{
}

Categories

Resources