如何使用方法帮助在Java中绘图?

时间:2016-04-12 15:10:34

标签: java graphics

我有一个任务,我必须画一些东西但是这样做我必须使用方法。我将如何在JApplet中执行此操作,这是我的代码我试图转向方法我试图用双手遮住耳朵,眼睛和嘴巴绘制三个面孔。

import javax.swing.JApplet;
import java.awt.Graphics;

public class HappyFace extends JApplet {
 int xOffSetOfFace = 100;
 int xOffSetOfEyes = 150;
 int xPos = 300;
 int yPosOfFace = 300;
 int yPosOfEye = 350;
 int heightOfEyes = 25;
 int widthOfEyes = 25;
 int height = 200;
 int width = 200;
 int xOffSetOfMouth = 160;
 int yOffSetOfMouth = 420;
 int widthOfMouth = 110;
 int heightOfMouth = 50;
 int startArcOfMouth = 180;
 int endArcOfMouth = 180;
 int xOffSetOfEar = 80;
 int yPosOfEar = 278;
 int heightOfEar = 68;
 int widthOfEar = 50;
 int dxHandHear = 175;
 int dyHandHear = 175;
 int arcWidth = 12;
 int arcHeight = 42;
 int arcStart = 90;
 int arcEndOne = 150;
 int arcEndTwo = -120;

 public void paint(Graphics canvas) {
  super.paint(canvas);
  for (int i = 0; i < 3; i++) {
   canvas.drawOval((xOffSetOfFace) + (xPos * i), yPosOfFace, width, height); //draws Face
   canvas.fillOval((xOffSetOfEyes) + (xPos * i), yPosOfEye, widthOfEyes, heightOfEyes); //draws eye1
   canvas.fillOval((xOffSetOfEyes) + (xPos * i) + 90, yPosOfEye, widthOfEyes, heightOfEyes); //draws eye2
   canvas.drawArc((xOffSetOfMouth) + (xPos * i), yOffSetOfMouth, widthOfMouth, heightOfMouth, startArcOfMouth,
    endArcOfMouth); //draws mouth
   canvas.drawOval((xOffSetOfEar) + (xPos * i), yPosOfEar, widthOfEar, heightOfEar); //draws leftmost ears
   canvas.drawOval((xOffSetOfEar) + (xPos * i) + 190, yPosOfEar, widthOfEar, heightOfEar); //draws right most ears

  }


  canvas.fillOval(85, 350, 45, 25); //draws palm
  canvas.fillArc(87, 335, arcWidth, arcHeight, arcStart, arcEndOne); //draws finger1
  canvas.fillArc(87, 335, 12, 42, 90, -120); //draws finger1
  canvas.fillArc(105, 330, 12, 42, 90, 150); //draws finger2
  canvas.fillArc(105, 330, 12, 42, 90, -120); //draws finger2
  canvas.fillArc(117, 335, 12, 42, 90, 150); //draws finger3
  canvas.fillArc(117, 335, 12, 42, 90, -120); //draws finger3
  //sep
  canvas.fillOval(85 + dxHandHear, 350, 45, 25); //draws palm
  canvas.fillArc(87 + dxHandHear, 335, 12, 42, 90, 150); //draws finger1
  canvas.fillArc(87 + dxHandHear, 335, 12, 42, 90, -120); //draws finger1
  canvas.fillArc(105 + dxHandHear, 330, 12, 42, 90, 150); //draws finger2
  canvas.fillArc(105 + dxHandHear, 330, 12, 42, 90, -120); //draws finger2
  canvas.fillArc(117 + dxHandHear, 335, 12, 42, 90, 150); //draws finger3
  canvas.fillArc(117 + dxHandHear, 335, 12, 42, 90, -120); //draws finger3

  canvas.fillOval((85 + dxHandHear) * 2, 350, 45, 25); //draws palm
  canvas.fillArc((87 + dxHandHear) * 2, 335, 12, 42, 90, 150); //draws finger1
  canvas.fillArc((87 + dxHandHear) * 2, 335, 12, 42, 90, -120); //draws finger1
  canvas.fillArc((95 + dxHandHear) * 2, 330, 12, 42, 90, 150); //draws finger2
  canvas.fillArc((95 + dxHandHear) * 2, 330, 12, 42, 90, -120); //draws finger2
  canvas.fillArc((102 + dxHandHear) * 2, 335, 12, 42, 90, 150); //draws finger3
  canvas.fillArc((102 + dxHandHear) * 2, 335, 12, 42, 90, -120); //draws finger3

  canvas.fillOval(((85 + dxHandHear) * 2) - 90, 350, 45, 25); //draws palm
  canvas.fillArc(((87 + dxHandHear) * 2) - 90, 335, 12, 42, 90, 150); //draws finger1
  canvas.fillArc(((87 + dxHandHear) * 2) - 90, 335, 12, 42, 90, -120); //draws finger1
  canvas.fillArc(((95 + dxHandHear) * 2) - 90, 330, 12, 42, 90, 150); //draws finger2
  canvas.fillArc(((95 + dxHandHear) * 2) - 90, 330, 12, 42, 90, -120); //draws finger2
  canvas.fillArc(((102 + dxHandHear) * 2) - 90, 335, 12, 42, 90, 150); //draws finger3
  canvas.fillArc(((102 + dxHandHear) * 2) - 90, 335, 12, 42, 90, -120); //draws finger3

  canvas.fillOval(((85 + dxHandHear) * 3) - 25, 350 + 110, 45, 25); //draws palm
  canvas.fillArc(((87 + dxHandHear) * 3) - 30, 335 + 110, 12, 42, 90, 150); //draws finger1
  canvas.fillArc(((87 + dxHandHear) * 3) - 30, 335 + 110, 12, 42, 90, -120); //draws finger1
  canvas.fillArc(((95 + dxHandHear) * 3) - 35, 330 + 110, 12, 42, 90, 150); //draws finger2
  canvas.fillArc(((95 + dxHandHear) * 3) - 35, 330 + 110, 12, 42, 90, -120); //draws finger2
  canvas.fillArc(((102 + dxHandHear) * 3) - 45, 335 + 110, 12, 42, 90, 150); //draws finger3
  canvas.fillArc(((102 + dxHandHear) * 3) - 45, 335 + 110, 12, 42, 90, -120); //draws finger3

  canvas.fillOval(((85 + dxHandHear) * 3) + 30, 350 + 110, 45, 25); //draws palm
  canvas.fillArc(((87 + dxHandHear) * 3) + 25, 335 + 110, 12, 42, 90, 150); //draws finger1
  canvas.fillArc(((87 + dxHandHear) * 3) + 25, 335 + 110, 12, 42, 90, -120); //draws finger1
  canvas.fillArc(((95 + dxHandHear) * 3) + 20, 330 + 110, 12, 42, 90, 150); //draws finger2
  canvas.fillArc(((95 + dxHandHear) * 3) + 20, 330 + 110, 12, 42, 90, -120); //draws finger2
  canvas.fillArc(((102 + dxHandHear) * 3) + 10, 335 + 110, 12, 42, 90, 150); //draws finger3
  canvas.fillArc(((102 + dxHandHear) * 3) + 10, 335 + 110, 12, 42, 90, -120); //draws finger3

 }
}

1 个答案:

答案 0 :(得分:0)

创建一个绘制手牌并接受一组int值来绘制手牌的方法

或者创建一个包含手的所有信息的类,即

int[] finger1 = [87+dxHandHear, 335, 12, 42, 90, 150];

然后在绘制手的方法中,它会传递一个手对象

private void drawHand(Hand h) {
  canvas.fillArc(h.getFinger1()[0], h.getFinger1()[1], h.getFinger1()[2], h.getFinger1()[3], h.getFinger1()[4], h.getFinger1()[5]); //draws finger1
  .....
}