Solved

# Image creation

Posted on 2003-02-27
Medium Priority
173 Views
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
Question by:nywebsol
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 86

Expert Comment

ID: 8036665
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

LVL 92

Expert Comment

ID: 8037393
Have a look at the following methods in the Graphics class:

drawPolygon()
drawPolyLine()
fillPolygon()

And in Graphics2D class look at:

draw(Shape s)

0

LVL 2

Accepted Solution

functionpointer earned 600 total points
ID: 8039435
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
###### Suggested Courses
Course of the Month12 days, 15 hours left to enroll