0

I am experimenting with Java graphics AWT version, not swing (for the moment). I have "Hair" on one side of the head, how in the world, do I get that same bit on the otherside, curved the other direction?

I've tried many combinations and it never turns out like it does over there and im frustrated.

 public class main extends Applet {
public void init() {
    // Customized Colors
    Color wallColor = new Color(76, 70, 70);

    // Applet Size
    setSize(1500, 900);
    // Set Applet Background Color
    setBackground(wallColor);
}

public void stop() {

}

public void paint(Graphics g)
{
    // Custom Colors
    Color pictureFrame = new Color(188, 198, 204);


    // Picture Frames

    g.setColor(pictureFrame);
    g.fillRect(50,50,300,450);
    g.setColor(Color.BLUE);
    g.fillRect(75,75,250,400);


    //Face
    g.setColor(Color.WHITE);
    g.fillOval(120,120,170,170);

    //Hair
    g.setColor(Color.BLACK);
    g.drawArc(195, 125, 70, 553, 0, 90);
    g.drawArc(195, 125, 75, 553, 0, 90);
    g.drawArc(195, 125, 80, 553, 0, 90);
    g.drawArc(195, 125, 85, 553, 0, 90);
    g.drawArc(195, 125, 90, 553, 0, 90);
    g.drawArc(195, 125, 95, 553, 0, 90);
    g.drawArc(195, 125, 100, 553, 0, 90);
    g.drawArc(195, 125, 105, 553, 0, 90);
    g.drawArc(195, 125, 110, 553, 0, 90);
    g.drawArc(195, 125, 77, 553, 0, 90);
    g.drawArc(195, 125, 77, 553, 0, 90);
    g.drawArc(195, 125, 83, 553, 0, 90);
    g.drawArc(195, 125, 87, 553, 0, 90);
    g.drawArc(195, 125, 93, 553, 0, 90);
    g.drawArc(195, 125, 97, 553, 0, 90);
    g.drawArc(195, 125, 103, 553, 0, 90);
    g.drawArc(195, 125, 107, 553, 0, 90);
    g.drawArc(195, 125, 113, 553, 0, 90);

}
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Scale the `Graphics` context by `-1` in the horizontal plan, demonstrated [here](http://stackoverflow.com/questions/24522458/flipping-rotations-and-images/24523176#24523176) and [here](http://stackoverflow.com/questions/13676364/flipping-shape-not-image/13676513#13676513) and [here](http://stackoverflow.com/questions/11911610/affinetransform-rotate-how-do-i-xlate-rotate-and-scale-at-the-same-time/11911758#11911758) – MadProgrammer Nov 05 '15 at 04:18
  • I'd prefer to do it by doing the g.drawArc method... is that possible? –  Nov 05 '15 at 04:28
  • Sure, invert all your coordinates – MadProgrammer Nov 05 '15 at 04:29
  • In which way? Invert as in switch the x's and switch the y's? add a negative? –  Nov 05 '15 at 04:38
  • Presumably you x axis – MadProgrammer Nov 05 '15 at 04:44
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Nov 05 '15 at 21:34

1 Answers1

0
g.fillOval(120, 120, 170, 170);

The top/left of the oval is 120 and the width of the oval is 170, so the "x" center point of your oval is 120 + (170 / 2) = 205.

Next you try to draw hair on the right side of the "x" center point using an arc width of 70.

g.drawArc(195, 125, 70, 553, 0, 90);

Before doing that, lets start by drawing an oval at the center point. To calculate the "x" position of the oval you need to center the oval around 205. So you would have:

int ovalX = 205 - (70 / 2) = 170.

However, in your case you want to draw the arc offset from the center. You hardcoded 195, which means your offset is 25 from the center. So you will also need to draw the left side of the hair -25 from the center, or at an x offset of 170 - 25 = 145.

When you draw the right side of the hair you start at angle 0, for 90 degrees. So when you draw the left side you want to start with an angle of 90 for 90 degrees.

Putting together all the calculations you can create a method to do the dirty work for you:

private void drawHair(Graphics g, int ovalX, int offset, int width, int height)
{
    int arcX = ovalX - (width / 2);

    //  draw left hair

    int leftX = arcX - offset;
    g.drawArc(leftX, 125, width, height, 90, 90);

    // draw right hair

    int rightX = arcX + offset;
    g.drawArc(rightX, 125, width, height, 0, 90);
}

So to use the method your code might be something like:

//Face
g.setColor(Color.WHITE);
g.fillOval(120, 120, 170, 170);
int centerX = 120 + (170 / 2);
int arcHeight = 553;

//Hair
g.setColor(Color.BLACK);
drawHair(g, centerX, 25, 70, arcHeight);
drawHair(g, centerX, 25, 74, arcHeight);
// fill in the rest of your hair strands here
// I would make the width divisible by two for complete symmetry.
camickr
  • 321,443
  • 19
  • 166
  • 288