package org.apache.wicket.examples.images;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import org.apache.wicket.examples.WicketExamplePage;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.InlineImage;
import org.apache.wicket.markup.html.image.Picture;
import org.apache.wicket.markup.html.image.Source;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.image.resource.DefaultButtonImageResource;
import org.apache.wicket.markup.html.image.resource.RenderedDynamicImageResource;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.request.resource.SharedResourceReference;
@author
public final class Home extends WicketExamplePage
{
@link
private final class CircleDynamicImageResource extends RenderedDynamicImageResource
{
private CircleDynamicImageResource(int width, int height)
{
super(width, height);
}
@Override
protected boolean render(Graphics2D graphics, Attributes attributes)
{
drawCircle(graphics);
return true;
}
}
private static final ResourceReference RESOURCE_REF = new PackageResourceReference(Home.class,
"Image2.gif");
public Home()
{
add(new Image("image2", new PackageResourceReference(Home.class, "Image2.gif")));
add(new Image("image3", new CircleDynamicImageResource(100, 100)));
add(new Image("image4", new Model<>("Image2.gif")));
add(new Image("image5", getImage5Resource()));
add(new Image("okButton", getOkButtonImage()));
add(new Image("cancelButton", new SharedResourceReference("cancelButton")));
add(new Image("imageModelResourceReference", new Model<>(RESOURCE_REF)));
add(new Image("imageModelResource", new Model<>(new CircleDynamicImageResource(100, 100))));
Image respImage = new Image("image6", new PackageResourceReference(this.getClass(),
"Image2_small.gif"), new PackageResourceReference(this.getClass(), "Image2_small.gif"),
new PackageResourceReference(this.getClass(), "Image2_medium.gif"),
new PackageResourceReference(this.getClass(), "Image2_large.gif"));
respImage.setXValues("320w", "2x", "900w");
respImage.setSizes("(min-width: 50em) 33vw", "(min-width: 28em) 50vw", "100vw");
this.add(respImage);
Picture picture = new Picture("picture");
Source large = new Source("sourcelarge", new PackageResourceReference(this.getClass(),
"Image2_large.gif"));
large.setMedia("(min-width: 650px)");
large.setSizes("(min-width: 50em) 33vw");
picture.add(large);
large.setOutputMarkupId(true);
Source medium = new Source("sourcemedium", new PackageResourceReference(this.getClass(),
"Image2_medium.gif"));
medium.setMedia("(min-width: 465px)");
picture.add(medium);
Image image3 = new Image("image7", new PackageResourceReference(this.getClass(),
"Image2_small.gif"));
picture.add(image3);
this.add(picture);
add(new InlineImage("inline", new PackageResourceReference(getClass(),"Image2.gif")));
}
@return
public ResourceReference getImage5Resource()
{
return new ResourceReference(Home.class, "image5")
{
@Override
public IResource getResource()
{
final BufferedDynamicImageResource resource = new BufferedDynamicImageResource();
final BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
drawCircle((Graphics2D)image.getGraphics());
resource.setImage(image);
return resource;
}
};
}
@param
void drawCircle(Graphics2D graphics)
{
final Random random = new Random();
int dx = Math.abs(10 + random.nextInt(80));
int dy = Math.abs(10 + random.nextInt(80));
int x = Math.abs(random.nextInt(100 - dx));
int y = Math.abs(random.nextInt(100 - dy));
graphics.setStroke(new BasicStroke(5));
graphics.drawOval(x, y, dx, dy);
}
final ResourceReference getOkButtonImage()
{
return new ResourceReference("okButton")
{
@Override
public IResource getResource()
{
return new DefaultButtonImageResource("Ok");
}
};
}
}