I have tried to set my Image icon but I get the error "getDefaultToolkit() has a private access on toolkit", can anybody help me?
#override
Public Image getIconImage() {
Image retValue = Toolkit.getDefaulttoolkit().getImage(
ClassLoader.getSystemResource("images/icon.png"));
return retValue;
}
Based on source here looks like you need:
#override
public Image getIconImage() {
Image retValue = Toolkit.getToolkit().getImage(
ClassLoader.getSystemResource("images/icon.png"));
return retValue;
}
Or as mentioned by #CarlosHeuberger, try java.awt.Toolkit.
Related
i am learning to write code in java and recently started to write a game as per my assignment.
i have completed almost entire game but stuck with the animation part of the game.
here is what i have done so far,
this is the class that load the image ti display,
public class dpmImage {
private BufferedImage dpm1;
private BufferedImage setDpm1;
public dpmImage() { //this is a constructor
try {
dpm1= ImageIO.read(new File("dpm1Load.png"));
} catch (IOException e) {
e.printStackTrace();
}
setDpm1 = dpm1;
}
private BufferedImage dpm1ImageGet() {
return setDpm1;
}
}
the following code is from main class (Main.java)
private Graphics cGraphcs;
cGraphcs.drawImage(dpmImageInstance.dpm1ImageGet(), 0, 0, null);
The code is working fine and displays the image.
Now, I am allowed to modify anything in dpmImage class but not allowed to modify anything in Main.java and still make this image animate. So I create an array of BufferedImage in dpmImage class and add a second image in the array as follows,
public class dpmImage {
private BufferedImage [] dpm1 = new BufferedImage[2];
private BufferedImage setDpm1;
public dpmImage() { //this is a constructor
try {
dpm1[0]= ImageIO.read(new File("dpm1Load.png"));
dpm1[1]= ImageIO.read(new File("dpm1Load1.png"));
} catch (IOException e) {
e.printStackTrace();
}
setDpm1 = dpm1[0];
setDpm1 = dpm1[1];
}
private BufferedImage dpm1ImageGet() {
return setDpm1;
}
}
But i couldn't get to animate it from first image to 2nd. Can someone please give me any hints on that ? i am not allowed to change the Main.java class
You always return the same BufferedImage from the dpm1ImageGet method. You need to get the instance from the array. Based on the frequency of the update, you can simply use an index like
private int indexImage = 0;
private BufferedImage dpm1ImageGet() {
indexImage = ( indexImage + 1 ) % dpm1.length; //increment and use % to prevent any ArrayOutOfBoundsException
return dpm1[indexImage];
}
Each call will return the next image. Of course, this depends on when you want to get the other image. It could be anything.
I am stuck with images. I need to upload an image and then display it on my page.
What I am doing now is uploading file like this:
private Picture picture; *// picture model*
private FileUploadField fileUpload;
public PictureUploader() {
Form<?> form = new Form<Void>("uploadForm") {
/**
*
*/
private static final long serialVersionUID = -694488846250739923L;
protected void onSubmit() {
FileUpload uploadedFile = fileUpload.getFileUpload();
File newFile = new File(uploadedFile.getClientFileName());
picture.setImage(newFile);
PageParameters pageParameter = new PageParameters();
pageParameter.put("file", picture.getImage());
setResponsePage(DataPage.class,pageParameter);
}
};
add(form);
form.setMultiPart(true);
form.add(setFileUpload(new FileUploadField("fileUpload")));
}
Then I submit it and go to DataPage ( I show only the constructor):
public DataPage(final PageParameters parameters) {
File file;
if (parameters.containsKey("file")) {
// WHAT TO DO HERE? SINCE GET() FROM PAGEPARAMETERS DOES NOT WORK FOR IT!
}
final Label result = new Label("result", ?????);
add(result);
}
Who knows how to make it, please, help me figure it out.
I'm fairly new to CQ5, and I've got the following problem.
I'm trying to display a list with the images behind the childpages. I managed to get the list itself working, but there's something wrong with the images, the path to it isn't correct .
So far I've got this
<div class="img-list">
<img src="${list.img}" />
</div>
and
public class ImageList {
private String img = "";
private Page listItem;
String extension;
Resource r;
public ImageList(Page p ) {
this.listItem = p;
this.r = listItem.getContentResource("image");
}
public String getImg() {
if (r != null) {
//Image image = new Image(r);
img = r.getPath();
}
return img;
}
public void setImg (String i) {
this.img = i;
}
}
But this isn't working. Can anybody help me out?
Thanks in advance
Edit, got it working with the following solution:
public String getImg() {
String imagePath = "";
if (r != null) {
Image image = new Image(r);
imagePath = (String)listItem.getPath()+".img.png"+ image.getSuffix();
}
return imagePath;
}
If I understand correctly, you are trying to get a list of images from the child pages.
Why not try creating a Page object, which is mapped to your current page (essentially, a current page object) and then use the listChildren method to list out all of the child pages. Then you can use the page.getContentResorce("image") method, create an image object and then return the path for images associated with each child page..
Something like this:-
Iterator<Page> childPageIterator = Currentpage.listChildren(new PageFilter(request));
while(pageIterator.hasNext())
{
Page childPage = pageIterator.next();
Resource r = page.getContentResource("image");
if (r != null) {
Image image = new Image(r);
String imagePath = (String)contentpage.getPath()+".img.png"+ image.getSuffix();
}
}
You may need to tailor this as per your needs, but hopefully this should get you started
You are trying to get the path of the resource(image node) and provide it to the src attribute of the image tag, instead of the providing the actual path of the image.
One way of approaching this by changing the getImg() method as shown below
public String getImg() {
if (r != null) {
Node node = r.adaptTo(Node.class);
if(node.hasProperty("fileReference"){
img = node.getProperty("fileReference").getString();
}
}
return img;
}
Or you can use the com.day.cq.wcm.foundation.Image class to draw the image by passing the resource object, instead of using the img tag.
I'm using JSF with primefaces and want to display an image from java code.
I already saw the tutorial on http://www.primefaces.org/showcase/ui/dynamicImage.jsf
But I'm not clear on how I can get the path to my image file correctly:
Code:
Bean:
#ManagedBean
public class ABean {
private StreamedContent bStatus;
public ABean() {
try {
Boolean connected = false;
if (connected == true) {
bStatus = new DefaultStreamedContent(new FileInputStream(new File("/images/greendot.png")), "image/jpeg");
} else {
bStatus = new DefaultStreamedContent(new FileInputStream(new File("/images/reddot.png")), "image/jpeg");
}
} catch(Exception e) {
e.printStackTrace();
}
}
public StreamedContent getBStatus() {
return bStatus;
}
public void setBStatus(StreamedContent bStatus) {
this.bStatus = bStatus;
}
}
xhtml:
<p:graphicImage value="#{ABean.bStatus}" />
returns:
java.io.FileNotFoundException: \images\reddot.png
I would appreciate best practices on where to store my image when displaying it form code and how to do it.
Since your images are in your web folder, you don't really need to use DefaultStreamedContent. I'd leave that only for images generated on the fly.
For your case, I'd just create a simple method that returns the image path (in your web folder) based on the boolean variable. Something like this:
public String getImagePath(){
return connected ? "/images/greendot.png" : "/images/reddot.png";
}
And on the graphicImage, you can just reference that:
<p:graphicImage value="#{yourBean.imagePath}"/>
Note that you might have to adjust the graphicImage tag if your web context is not root.
EDIT
You can actually make this even simpler:
<p:graphicImage value="#{yourBean.connected ? '/images/greendot.png' : '/images/reddot.png'}"/>
Just make sure to have a getter for the connected property.
Create your StreamedContent as follows:
bStatus = new DefaultStreamedContent(FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/images/greendot.png"), "image/jpeg");
When you are creating new File() this will be absolute path in your disk, not just in your application.
I'm making an app in which I open a BrowserField and the user can navigate further to links present in the web page. The problem is that when I press the physical back button, the previous screen is presented while I want to present the previous page in the BrowserField itself. How to do that? Is that even possible?
Thanks.
You need to use BrowserFieldHistory to go back to previous pages using the canGoBack() and goBack() methods. Just over-ride the keyChar method to control the ESCAPE key input and put your own logic like so:
public boolean keyChar(char key, int status, int time) {
if ( key == Characters.ESCAPE) {
if(yourBrowserField.getHistory().canGoBack()){
yourBrowserField.getHistory().goBack();
}else{
UiApplication.getUiApplication().popScreen(this);
return true;
}
}
}
Yes It is Possible.
Try this code:
public class NewsBrowserScreen extends MainScreen
{
int current_index,popup;
String url;
VerticalFieldManager vertical;
BrowserField browserField;
BrowserFieldConfig browserFieldConfig;
BrowserFieldHistory browserFieldHistory;
public NewsBrowserScreen(int current_index,int popup,String url)
{
this.current_index=current_index;
this.popup=popup;
this.url=url;
createGUI();
}
private void createGUI()
{
vertical=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR|HORIZONTAL_SCROLL|HORIZONTAL_SCROLLBAR);
browserFieldConfig=new BrowserFieldConfig();
browserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_POINTER);
browserField=new BrowserField(browserFieldConfig);
browserFieldHistory=browserField.getHistory();
vertical.add(browserField);
add(vertical);
browserField.requestContent(url);
}
public boolean onClose()
{
if(browserFieldHistory.canGoBack())
{
browserFieldHistory.goBack();
return true;
}
else
{
browserFieldHistory.clearHistory();
return super.onClose();
}
}
}
Enough;