Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. import java.awt.event.MouseListener;
  2. import java.awt.event.MouseEvent;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.awt.Toolkit;
  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import javax.swing.Timer;
  12. import java.util.ArrayList;
  13. import java.util.Iterator;
  14. import java.util.Random;
  15. import javax.swing.ImageIcon;
  16. import javax.swing.JPanel;
  17. import javax.imageio.*;
  18. import java.awt.image.*;
  19. import java.io.*;
  20. import java.util.Scanner;
  21.  
  22. public class BoardFace extends JPanel implements Runnable, MouseListener
  23. {
  24.  
  25. private Dimension d;
  26. int BOARD_WIDTH=500;
  27. int BOARD_HEIGHT=500;
  28. boolean angry = false;
  29. int x = 0;
  30. private Thread animator;
  31.  
  32. public BoardFace()
  33. {
  34. addMouseListener(this);
  35. setFocusable(true);
  36. d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
  37.  
  38. if (animator == null ) {
  39. animator = new Thread(this);
  40. animator.start();
  41. }
  42.  
  43. setDoubleBuffered(true);
  44. }
  45.  
  46.  
  47.  
  48. public void paint(Graphics g){
  49. super.paint(g);
  50.  
  51.  
  52. g.setColor(Color.white);
  53. g.fillRect(0, 0, d.width, d.height);
  54. //hair
  55. //brown becmones a new instance of the Color class...
  56. Color brown = new Color(101,0,0);
  57. //...& brown can now be used as parameter in method setColor
  58. g.setColor(brown);
  59. g.fillOval(60,130, 75, 75);
  60. g.fillOval(100,100, 75, 75);
  61. g.fillOval(140,70, 75, 75);
  62. g.fillOval(180,55, 75, 75);
  63. g.fillOval(220,55, 75, 75);
  64. g.fillOval(260,60, 75, 75);
  65. g.fillOval(300,80, 75, 75);
  66. g.fillOval(340,100, 75, 75);
  67. g.fillOval(380,110, 75, 75);
  68. //mouth
  69. if(!angry){
  70. //face
  71. g.setColor(Color.yellow); // yellow is a field of class Color
  72. g.fillOval(100,100, 350, 350);
  73. g.setColor(Color.red);// ...and red.
  74. g.fillOval(190,275, 175, 125);
  75. g.setColor(Color.yellow);
  76. g.fillOval(190,250, 175, 125);
  77. //hat
  78. Color purple= new Color(102,0, 102); //instance variable purple
  79. g.setColor(purple);
  80. g.fillRect(75, 75, 375, 50);
  81. g.fillRect(150, 25, 225, 100);
  82. //eyes
  83. g.setColor(Color.blue);// ...so is black...
  84. g.fillOval(175,175, 50, 50);
  85. g.setColor(Color.blue);
  86. g.fillOval(320,175, 50, 50);
  87. //nose
  88. Color orange = new Color(255,128,0);//instance variable orange
  89. g.setColor(orange);
  90. g.fillOval(275,280,10, 10);
  91. }
  92. else{
  93. g.setColor(Color.green);
  94. g.fillOval(100,100, 350, 350);
  95. Color red = new Color(255,0,0);
  96. Color black = new Color(0,0,0);
  97. g.setColor(red);
  98. g.fillOval(275,280,10, 10);
  99. g.setColor(Color.black);
  100. g.fillOval(175,175, 50, 50);
  101. g.setColor(Color.black);
  102. g.fillOval(320,175, 50, 50);
  103. g.setColor(Color.black);
  104. g.fillOval(190,275, 175, 125);
  105. g.setColor(Color.green);
  106. g.fillOval(190,250, 175, 125);
  107. g.setColor(red);
  108. g.fillOval(275,280,10, 10);
  109.  
  110. }
  111.  
  112.  
  113.  
  114. //writing- keep smiling
  115. //and to add text to the drawing..
  116. g.setColor(Color.black);
  117. g.setFont (new Font("TimesRoman", Font.PLAIN, 100));
  118. g.drawString("Agnus" , 125, 70);
  119.  
  120. Toolkit.getDefaultToolkit().sync();
  121. g.dispose();
  122. }
  123.  
  124. public void run() {
  125. long beforeTime, timeDiff, sleep;
  126. beforeTime = System.currentTimeMillis();
  127. int animationDelay = 500;
  128. long time =
  129. System.currentTimeMillis();
  130. while (true) {//infinite loop
  131. // spriteManager.update();
  132. repaint();
  133. try {
  134. time += animationDelay;
  135. Thread.sleep(Math.max(0,time -
  136. System.currentTimeMillis()));
  137. }catch (InterruptedException e) {
  138. System.out.println(e);
  139. }//end catch
  140. }//end while loop
  141.  
  142.  
  143. }//end of run
  144.  
  145. public void mousePressed(MouseEvent e) {
  146. int x = e.getX();
  147. int y = e.getY();
  148. if(angry == true)
  149. angry = false;
  150. else
  151. angry = true;
  152.  
  153. }
  154. public void mouseReleased(MouseEvent e) {
  155. }
  156. public void mouseEntered(MouseEvent e) {
  157. }
  158. public void mouseExited(MouseEvent e) {
  159. }
  160. public void mouseClicked(MouseEvent e) {
  161. }
  162. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement