Click here to Skip to main content
15,888,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import java.awt.*;
import java.awt.geom.*;
public class SimpleLetterC extends Frame{

   //Constructor
  public SimpleLetterC()
  {
    //Enables the closing of the window.
    addWindowListener(new MyFinishWindow());
  }


  public void paint(Graphics g)
  {

    Graphics2D g2d = (Graphics2D) g;

    //Use of antialiasing to have nicer lines.
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    //The lines should have a thickness of 3.0 instead of 1.0.
    BasicStroke bs = new BasicStroke(3.0f);
    g2d.setStroke(bs);


    //The control points for defining the letter.
    int xc1 =  50;
    int yc1 = 250;

    int xc2 = 250;
    int yc2 =  50;

    int xc3 =  50;
    int yc3 =  50;

    int xc4 =  250;
    int yc4 =  450;

    int xc5 =  50;
    int yc5 = 450;



    //Definition and drawing of the two curves that define the letter.
    QuadCurve2D.Double d1 = new QuadCurve2D.Double(xc1,yc1,xc3,yc3,xc2,yc2);
    g2d.draw(d1);

    QuadCurve2D.Double d2 = new QuadCurve2D.Double(xc1,yc1,xc5,yc5,xc4,yc4);
    g2d.draw(d2);


    //Draw the control points.
    drawSmallRect(xc1,yc1,g2d);
    drawSmallRect(xc11,yc11,g2d);
    drawSmallRect(xc2,yc2,g2d);
    drawSmallRect(xc3,yc3,g2d);
    drawSmallRect(xc4,yc4,g2d);
    drawSmallRect(xc5,yc5,g2d);

    //Label the control points.
    g2d.setFont(new Font("serif",Font.PLAIN,24));
    g2d.drawString("P11'",xc1-10,yc1-5);
    g2d.drawString("P2'",xc2+10,yc2+10);
    g2d.drawString("P3'",xc3-35,yc3+4);
    g2d.drawString("P4'",xc4+10,yc4+10);
    g2d.drawString("P5'",xc5+10,yc5+10);
  }


 /**
  * Draws a small square around the centre (x,y).
  *
  * @param x        x-coordinate of the centre
  * @param y        y-coordinate of the centre
  * @param g2d      Graphics2D object for drawing
  */
   public static void drawSmallRect(int x, int y, Graphics2D g2d)
  {
    Rectangle rect = new Rectangle(x-4,y-3,8,8);
    g2d.fill(rect);
  }
    public static void main(String[] args) {
         
  
    SimpleLetterC f = new SimpleLetterC();
    f.setTitle("The letter C");

    f.setSize(420,500);
    f.setVisible(true);
  
    }
    
}


What I have tried:

This code draws the shape of the letter C using Java 2D graphics, I need to draw the shape of A, H, F letters using same code but I do not know where can I change or modify in the code.. I tried my best to understand the role of control points and how they can change the shape of the letter
Please help
Posted
Updated 2-Oct-20 13:56pm
v2
Comments
Sandeep Mewara 2-Oct-20 18:48pm    
Would suggest you read and understand about topic. It's clear you got this code from somewhere and have no idea how it does what it does.

start from something like: https://books.trinket.io/thinkjava/appendix-b.html
Richard MacCutchan 3-Oct-20 4:29am    
The control points define the limits of each graphic. So you just need to draw the existing ones on paper to see how to create different sets. Use graph (squared) paper to see how they are constructed.

1 solution

I would think that H and F are simpler than a C; no curves.

Try "I", for starters, then "L", etc.

Make a copy of the program and start changing x's and y's and watch what happens.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900