This is my code
shell.setFullScreen(true);
shell.setMaximized(true);
shell.setText("SD Cyber Cafe");
shell.setLayout(new FormLayout());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Image oriImage = new Image(display, "C:\\Users\\LAPTOP-SYAMSOUL\\Desktop\\lockscreen_app\\main_bg.jpeg"); //should get from database
//System.out.println(screenSize.width);
Image newImage = new Image(display, oriImage.getImageData(100).scaledTo(screenSize.width, screenSize.height));
shell.setBackgroundImage(newImage);
When I run the app via eclipse, it works fine...
But after I exported to Runnable JAR the background Image is not scaled... why??
This is what I expected:
..
..
..
But currently it appear like below: ( I don't want this):
It is hard to say for sure from this code but Toolkit is a Swing/AWT method and should not be used with SWT. It may well be giving the wrong values.
Get the primary display size using something like:
Rectangle displayArea = shell.getDisplay().getPrimaryMonitor().getBounds();
which tells you about the main (primary) monitor or
Rectangle displayArea = shell.getMonitor().getBounds();
which tells you about the monitor on which the shell will appear (may be different if there are several monitors).
Related
So I am trying to add an image to a JLabel object which is added to a JPanel called "topPanel" which is part of a JFrame called
"primaryWindow". I have already declared the "topPanel" and "primaryWindow". I found on other forms that you have to create a separate source folder and add the image file in that folder in order to access it and I did so.
However, when I execute the following, the image does not appear to be on the Label. I know that it has nothing to do with adding the JLabel to the panel properly because when I enter a String into the JLabel constructor, the String appears on the panel. An image however does not appear. Am I properly adding the image? I am using a mac if this helps.
private JLabel image = new JLabel();
image.setIcon(new ImageIcon("Check.png"));
topPanel.add(image);
primaryWindow.add(topPanel, BorderLayout.NORTH);
You can also use BufferedImage
BufferedImage myPicture = ImageIO.read(new File("C:\\xx\\xxx\\Check.png.jpg"));
Image scaled = myPicture.getScaledInstance(100,70,Image.SCALE_SMOOTH);
image = new JLabel(new ImageIcon(scaled));
topPanel.add(image);
primaryWindow.add(topPanel, BorderLayout.NORTH);
Note that "C:\\xx\\xxx\\Check.png.jpg" is the path where you save Check.png.
Hope this helped.
The ImageIcon that you passed into the setIcon method of the JLabel could be null. Have you tried to check if it is null before calling setIcon? If you create a "res" resource folder in the root directory of your project, you could try the following:
image.setIcon(new ImageIcon(ImageIO.read(new File("res/Check.png"))));
Additionally, if you are using Eclipse, you should try and refresh the project directory by right clicking and pressing refresh; sometimes Eclipse doesn't register files added.
Lastly, try setting the background of the JPanel to a certain color to see if it is displaying it and make sure its width and height are not 0 (it is possible that the layout you are using changed its size).
What i want to achieve:
I am using a FileChooser and the user selects an appropriate .jpg image file . Then i am copying that image , renaming it background.jpg to a known folder and trying to set it as the background image of the application using .setStyle(...); There is not problem of copying the image [ i am checking it]
The Problem that occurs:
I have a Stage with a BorderPane . The BorderPane has an background Image , i do that using
borderPane.setStyle("-fx-background-image:url('filepath')");
!It works well the first time!
->Then i am deleting that file [background.jpg] and i am replacing it with another file named also [background.jpg] . The background image of the BorderPane isn't changing ....
I have tried also resetting the same style using again :
borderPane.setStyle("-fx-background-image:url('filepath')");
Finally when i am changing the filename , for example to [background-12.jpg] and reseting the style using the above it changes the background image.
Which exactly is the problem ? I mean i am sure that the background.jpg has been created , i am checking it and also when i am changing the name to something other again and again it works .
Is the Java CSS Parser lazy to parse the new style which is the same but has other -fx-background-image resource ?
As for the File path i am sure it is well , i am using the code below :
//Maou is the File URL in appropriate format for CSS
String maou = file.getAbsoluteFile().toURI().toString()
//Here i add the appropriate file separator, if not JavaFX will report error
maou = maou.replaceAll("\\Q\\\\E", "//");
//Print maou
System.out.println("Maou=\n" + maou);
Solution :
I found as best solution using James_D answer , a little bit modified so it covers the whole window:
BackgroundImage bgImg = new BackgroundImage(image, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
new BackgroundSize(window.getWidth(), window.getHeight(), true, true, true, true));
Rather than using an inline style, I would recommend setting the background via the background property directly:
Image img = new Image(file.getAbsoluteFile().toURI().toString());
BackgroundImage bgImg = new BackgroundImage(img,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
borderPane.setBackground(new Background(bgImg));
The Background class provides Java API programmatic access to all the same properties that can be set by CSS.
While I don't know exactly what's going on, presumably it's some form of caching which JavaFX is doing to try to be "helpful". I may look into the source code later.
To be honest, though, setting a background via CSS feels like the wrong approach to me. I always avoid setting any styles explicitly, like:
borderPane.setStyle("something");
and prefer to add and remove style classes:
borderPane.getStyleClass().add("foo");
borderPane.getStyleClass().remove("foo");
I don't think this is possible in your situation, so I would instead use a StackPane
to layer your content over an ImageView.
ImageView img = new ImageView(new Image(new URL("path")));
StackPane stack = new StackPane();
stack.getChildren.addAll(img, /*overlaid content*/);
I need to change the Jdialog box title bar icon. By default it uses a java coffee image.
I have searched in internet and used many codes
1. Image im = Toolkit.getDefaultToolkit().getImage("/org/qmon/generate/Images/JDialog -2.ico");
dialog.setIconImage(im);
2. Toolkit kit = Toolkit.getDefaultToolkit ();
Image img = kit.getImage ("/org/qmon/generate/Images/Create File Tag-16x16.png");
dialog.setIconImage(img);
nothing works properly.. Kindly help me.. Thanks in Advance
Firtsly, ico is not a support image format for Java.
The likely reason you're having issues with the second approach is that getImage is expecting a file reference and the image you seem to referencing looks like it's embedded (stored within your application)
Try using something more like...
Image img = kit.getImage (getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
Instead.
Personally, I prefer ImageIO.read as it throws a IOException when something goes wrong...
Image img = ImageIO.read(getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
But that's me...
You should also consider taking a look at Convert List<BufferedImage> to Image which demonstrates the use of ico file (from a 3rd party API) and setIconImages method
Image image = ImageIO.read(new URL(
"http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
dialog.setIconImage( image );
dialog.setVisible(true);
I am using this in my application and working fine
java.net.URL url = ClassLoader.getSystemResource("res/java.png");
ImageIcon icon = new ImageIcon(url);
JOptionPane.showMessageDialog(null, jep, "UroSync",JOptionPane.INFORMATION_MESSAGE, icon);
To improve what MadProgrammer has said, I met the problem and I solved it instantiating a JDialog but using the static class Toolkit method getDefaultToolkit().getImage(Image img).
JDialog dialog = new JDialog();
dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(MyMainClass.class.getResource("/myIcon.png")));
To do that you need to add before the image into the build path of the Project.
I have tried to devise a way to get a screenshot of a Java applet running in a browser, but I can't seem to get it working. I managed successfully to use cutycapt to get screenshots fine from "normal" websites, but I soon found out that qtwebkit which it seems to rely on for the rendering does not support java. I also tried IEcapt thinking that it would somehow inherit the Java rendering capabilities of IE on the system, but it does not work. Flash also does not seem to be working in IEcapt, and it has no flags for enabling plugins, so I am assuming the functionality is not there either.
Does anyone have any thoughts on how you could render something like an /index.jsp to an image from a Windows or Linux command line?
Selenium webdriver might be useful here:
http://docs.seleniumhq.org/projects/webdriver/
It is used primarily for test automation but it might be helpful.
It could be used, for example, like this:
import org.openqa.selenium.*;
WebDriver driver = new FirefoxDriver(); // create a Firefox webdriver instance
driver.get("http://www.google.com/"); // navigate to page
File screenshotFile = ((Screenshot)driver).getScreenshotAs(file); // capture screenshot
// and save to a file
// here you can trigger any necessary actions on the website:
Webelement element = driver.findElement(By.name("q")).sendKeys("xxxxx");
element.click();
new WebDriverWait(driver, 10)).until(ExpectedConditions.titleContains("xxxxx"));
// and capture a new screenshot once the content has changed
File xxxScreenshotFile = ((Screenshot)driver).getScreenshotAs(file);
Have you tried using java.awt.Robot?
Rectangle rect = yourGragphicsConfiguration.getBounds();
BufferedImage image = new Robot().createScreenCapture(rect);
If you know the position of your applet you might be able to get it with
BufferedImage dest = image.getSubimage(appletX, appletY, appletHeight, appletWidth);
You can take a screenshot of Swing/AWT component.
This can be done in 2 ways. In both cases the component must be visible.
Without the robot use:
BufferedImage image = new BufferedImage(component.getWidth(),
component.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
component.paint(g);
With the robot use:
In this case the screenshot of area in which this component is situated will be made. That is, if the component overlaps another application window then the screenshot will contain an area of this another window.
Point point = new Point(0, 0);
SwingUtilities.convertPointToScreen(point, component);
Rectangle region = component.getBounds();
region.x = point.x;
region.y = point.y;
BufferedImage image= new Robot().createScreenCapture(region);
This information is taken from the article: Frequently Asked Questions during Java applet development
so this is how my code looks in NetBeans:
and this is how it looks after I complied it
I think it has to do with this
ImageIcon background = new ImageIcon(getClass().getResource("/Graphics/BackgroundConcept.jpg"));
img = background.getImage();
I put all the picture in the src folder and it wont load BUT it load other picture for the title screen.
But I'm using this type of input
Image image=new ImageIcon(getClass().getResource("/Graphics/titleScreen.jpg")).getImage();
g.drawImage(image,0,0,this);
and
Image GameCredits=new ImageIcon(getClass().getResource("/Graphics/credits.gif")).getImage();
The problem is I'm not adding it to a JFrame.
It is long code so I'll post it in pastebucket, basically its moving the background images as I move.
It works in NetBeans but I needed a compiled version.
http://www.pastebucket.com/14192