Directory

← Back

Graphics2DImages

Server side Java2d images for Vaadin

Author

Rating

Base classes to draw your images on server side with standard java Graphics2D. Provides a StreamResource to use your images with Vaadin 7. Provides images with fixed and variable size.

Sample code

	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		
		setContent(layout);

		FixedWidthGraphics2DImage img2d1 = new FixedWidthGraphics2DImage("myimage1" +
                System.currentTimeMillis() + ".png", 16, 16) {

			@Override
			public void paint(Graphics2D g) {
		        g.setColor(Color.LIGHT_GRAY);
		        g.fillRect(0, 0, width-1, height-1);
		        g.setColor(Color.BLUE);
		        g.fillOval(2, 2, width-5, height-5);
		        g.setColor(Color.YELLOW);
		        g.drawOval(2, 2, width-5, height-5);
		        g.setColor(Color.BLACK);
		        g.drawRect(0, 0, width-1, height-1);
			}
			
		};
		
		Button bu1 = new Button("Button with Java2d Image");
		bu1.setIcon(img2d1.getResource());
		layout.addComponent(bu1);
		
		VariableWidthGraphics2DImage img2d2 = new VariableWidthGraphics2DImage("myimage2" + 
                System.currentTimeMillis() + ".png") {

			Font font = new Font("Arial", Font.BOLD, 24);
			String text = "This is not a text!";
			Color bkColor1 = Color.YELLOW;
			Color bkColor2 = Color.ORANGE;
			Color borderColor = Color.BLACK;
			Color textColor = Color.BLACK;
			
			@Override
			protected void computeSize(Graphics2D g) {
				
		        FontMetrics fm = g.getFontMetrics(font);
		        width = fm.stringWidth(text) + 4;
		        height = 30;
			}

			@Override
			public void paint(Graphics2D g) {
				
		        GradientPaint gradient = new GradientPaint(0, 0, bkColor1, width-1, height-1, bkColor2, true);
		        g.setPaint(gradient);
		        g.fillRect(0, 0, width-1, height-1);
		        g.setColor(borderColor);
		        g.drawRect(0, 0, width-1, height-1);
		        g.setColor(textColor);
		        g.setFont(font);
		        g.drawString(text, 2, 23);
			}
		};

		Embedded emb = new Embedded("Image created with Java2d:", img2d2.getResource());
		layout.addComponent(emb);

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Initial release

Released
2013-03-17
Maturity
EXPERIMENTAL
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.0+
Browser
Browser Independent
Online