• Status: Solved
• Priority: Medium
• Security: Public
• Views: 174

# Image creation

I need some examples of how to make certian images such as Square, triangle, circle, possible drawing up a star?????
I know there is a Rectangle() class, but I need a working example of how to do a few different things.
0
nywebsol
1 Solution

Commented:
Rectangle with equal sides=Square - that's two down
Circle = Ellipse in a square configuration - that's three.
A triangle is a polygon, as is a star. What i suggest you do is to take a piece of graph paper and plot out some coordinates. It will then be easy to transfer these to the logic of your program.
0

Commented:
Have a look at the following methods in the Graphics class:

drawPolygon()
drawPolyLine()
fillPolygon()

And in Graphics2D class look at:

draw(Shape s)

0

Commented:
This should look familiar:

private byte[] getImage()
throws IOException
{
int width= 100;
int height = 100;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
Polygon poly = new Polygon();
Random random = new Random();
for (int i=0; i < 5; i++) {
}
g.setColor(Color.cyan);
g.fillPolygon(poly);

g.setColor( Color.black );
g.drawRect ( 1, 1, 20, 20 );

g.setColor( Color.red );
g.drawOval( 25, 25, 15, 15 );

g.setColor( Color.blue );
poly = new Polygon();
g.drawPolygon( poly );

g.drawImage(image, 0, 0, null);
g.dispose();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(image);
byte[] b = outputStream.toByteArray();
outputStream.close();
return b;
}

My 2yr old drew big blue crayon animals all over the last of my graph paper. No star. :-)
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.