use of java.awt.Graphics2D in project jmonkeyengine by jMonkeyEngine.
the class LwjglWindow method imageToGLFWImage.
/**
* Convert the {@link BufferedImage} to the {@link GLFWImage}.
*/
private GLFWImage imageToGLFWImage(BufferedImage image) {
if (image.getType() != BufferedImage.TYPE_INT_ARGB_PRE) {
final BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
final Graphics2D graphics = convertedImage.createGraphics();
final int targetWidth = image.getWidth();
final int targetHeight = image.getHeight();
graphics.drawImage(image, 0, 0, targetWidth, targetHeight, null);
graphics.dispose();
image = convertedImage;
}
final ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
for (int i = 0; i < image.getHeight(); i++) {
for (int j = 0; j < image.getWidth(); j++) {
int colorSpace = image.getRGB(j, i);
buffer.put((byte) ((colorSpace << 8) >> 24));
buffer.put((byte) ((colorSpace << 16) >> 24));
buffer.put((byte) ((colorSpace << 24) >> 24));
buffer.put((byte) (colorSpace >> 24));
}
}
buffer.flip();
final GLFWImage result = GLFWImage.create();
result.set(image.getWidth(), image.getHeight(), buffer);
return result;
}
use of java.awt.Graphics2D in project jersey by jersey.
the class SparklinesResource method smooth.
@Path("smooth")
@GET
public Response smooth(@DefaultValue("2") @QueryParam("step") final int step, @DefaultValue("true") @QueryParam("min-m") final boolean hasMin, @DefaultValue("true") @QueryParam("max-m") final boolean hasMax, @DefaultValue("true") @QueryParam("last-m") final boolean hasLast, @DefaultValue("blue") @QueryParam("min-color") final ColorParam minColor, @DefaultValue("green") @QueryParam("max-color") final ColorParam maxColor, @DefaultValue("red") @QueryParam("last-color") final ColorParam lastColor) {
final BufferedImage image = new BufferedImage(data.size() * step - 4, imageHeight, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = image.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.gray);
final int[] xs = new int[data.size()];
final int[] ys = new int[data.size()];
final int gap = 4;
final float d = (limits.width() + 1) / (float) (imageHeight - gap);
for (int i = 0, x = 0; i < data.size(); i++, x += step) {
final int v = data.get(i);
xs[i] = x;
ys[i] = imageHeight - 3 - (int) ((v - limits.lower()) / d);
}
g.drawPolyline(xs, ys, data.size());
if (hasMin) {
final int i = data.indexOf(Collections.min(data));
g.setColor(minColor);
g.fillRect(xs[i] - step / 2, ys[i] - step / 2, step, step);
}
if (hasMax) {
final int i = data.indexOf(Collections.max(data));
g.setColor(maxColor);
g.fillRect(xs[i] - step / 2, ys[i] - step / 2, step, step);
}
if (hasMax) {
g.setColor(lastColor);
g.fillRect(xs[xs.length - 1] - step / 2, ys[ys.length - 1] - step / 2, step, step);
}
return Response.ok(image).tag(tag).build();
}
use of java.awt.Graphics2D in project Openfire by igniterealtime.
the class GraphServlet method writePDFContent.
private void writePDFContent(HttpServletRequest request, HttpServletResponse response, JFreeChart[] charts, Statistic[] stats, long starttime, long endtime, int width, int height) throws IOException {
try {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
writer.setPageEvent(new PDFEventListener(request));
document.open();
int index = 0;
int chapIndex = 0;
for (Statistic stat : stats) {
String serverName = XMPPServer.getInstance().getServerInfo().getXMPPDomain();
String dateName = JiveGlobals.formatDate(new Date(starttime)) + " - " + JiveGlobals.formatDate(new Date(endtime));
Paragraph paragraph = new Paragraph(serverName, FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD));
document.add(paragraph);
paragraph = new Paragraph(dateName, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.PLAIN));
document.add(paragraph);
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
Paragraph chapterTitle = new Paragraph(++chapIndex + ". " + stat.getName(), FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD));
document.add(chapterTitle);
// total hack: no idea what tags people are going to use in the description
// possibly recommend that we only use a <p> tag?
String[] paragraphs = stat.getDescription().split("<p>");
for (String s : paragraphs) {
Paragraph p = new Paragraph(s);
document.add(p);
}
document.add(Chunk.NEWLINE);
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphs2D = template.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
charts[index++].draw(graphs2D, rectangle2D);
graphs2D.dispose();
float x = (document.getPageSize().width() / 2) - (width / 2);
contentByte.addTemplate(template, x, writer.getVerticalPosition(true) - height);
document.newPage();
}
document.close();
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength is needed for MSIE!!!
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
} catch (DocumentException e) {
Log.error("error creating PDF document: " + e.getMessage());
}
}
use of java.awt.Graphics2D in project jmonkeyengine by jMonkeyEngine.
the class CombinedTexture method scale.
/**
* This method scales the given texture to the given size.
*
* @param texture
* the texture to be scaled
* @param width
* new width of the texture
* @param height
* new height of the texture
*/
private void scale(Texture2D texture, int width, int height) {
// first determine if scaling is required
boolean scaleRequired = texture.getImage().getWidth() != width || texture.getImage().getHeight() != height;
if (scaleRequired) {
Image image = texture.getImage();
BufferedImage sourceImage = ImageToAwt.convert(image, false, true, 0);
int sourceWidth = sourceImage.getWidth();
int sourceHeight = sourceImage.getHeight();
BufferedImage targetImage = new BufferedImage(width, height, sourceImage.getType());
Graphics2D g = targetImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(sourceImage, 0, 0, width, height, 0, 0, sourceWidth, sourceHeight, null);
g.dispose();
Image output = new ImageLoader().load(targetImage, false);
image.setWidth(width);
image.setHeight(height);
image.setData(output.getData(0));
image.setFormat(output.getFormat());
}
}
use of java.awt.Graphics2D in project jna by java-native-access.
the class DragHandler method createDragImage.
/** Create an image from the given icon. The image is provided to the
* native handler if drag images are supported natively.
* @param gc current graphics configuration.
* @param icon Icon on which to base the drag image.
* @return image based on the given icon.
*/
protected Image createDragImage(GraphicsConfiguration gc, Icon icon) {
int w = icon.getIconWidth();
int h = icon.getIconHeight();
BufferedImage image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, w, h);
// Ignore pixels in the buffered image
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, ghostAlpha));
icon.paintIcon(dragSource, g, 0, 0);
g.dispose();
return image;
}
Aggregations