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

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
Asked:
nywebsol
1 Solution
 
CEHJCommented:
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
 
objectsCommented:
Have a look at the following methods in the Graphics class:

drawPolygon()
drawPolyLine()
fillPolygon()

And in Graphics2D class look at:

draw(Shape s)


0
 
functionpointerCommented:
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++) {
      poly.addPoint(random.nextInt(width),     random.nextInt( height) );
    }
    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();
    poly.addPoint( 30, 30 );
    poly.addPoint( 30, 50 );
    poly.addPoint( 45, 45 );
    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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

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