Image creation

Posted on 2003-02-27
Medium Priority
Last Modified: 2011-04-14
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.
Question by:nywebsol
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
  • Learn & ask questions
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.
LVL 92

Expert Comment

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


And in Graphics2D class look at:

draw(Shape s)


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.fillRect(0, 0, width, height);
    Polygon poly = new Polygon();
    Random random = new Random();
    for (int i=0; i < 5; i++) {
      poly.addPoint(random.nextInt(width),     random.nextInt( height) );

    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();
    poly.addPoint( 30, 30 );
    poly.addPoint( 30, 50 );
    poly.addPoint( 45, 45 );
    g.drawPolygon( poly );

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

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question