Face

/** * Draws a face of a little bald guy with a hat. * * @Laura * @1.0 */ import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.Timer; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.imageio.*; import java.awt.image.*; import java.io.*; import java.util.Scanner; public class BoardFace extends JPanel implements Runnable, MouseListener { private Dimension d; int BOARD_WIDTH=500; int BOARD_HEIGHT=500; BufferedImage img; int x = 0; Color skin = new Color(255, 243, 204); Color skin2 = new Color(255, 236, 181); Color lips = new Color(237, 139, 139); Color brown = new Color(101,0,0); Color orange = new Color(255,128,0); Color purple = new Color(102,0, 102); Color transparent = new Color(255,255,255, 130); private Thread animator; public BoardFace() { addMouseListener(this); setFocusable(true); d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); if (animator == null ) { animator = new Thread(this); animator.start(); } setDoubleBuffered(true); try { img = ImageIO.read(this.getClass().getResource("TopHat.jpg")); } catch (IOException e) { System.out.println("Image could not be read"); // System.exit(1); } } public void paint(Graphics g){ super.paint(g); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); //face g.setColor(skin); // yellow is a field of class Color g.fillOval(100,100, 350, 350); //eyes g.setColor(Color.white);// ...so is black... g.fillOval(175,175, 50, 50); g.fillOval(320,175,50, 50); //irises g.setColor(Color.red); g.fillOval(187,185,25,25); g.fillOval(332,185,25,25); //pupils g.setColor(Color.black); g.fillOval(195,192,10,10); g.fillOval(340,192,10,10); //mouth g.setColor(lips); g.fillArc(205,300,150,100,180,180); //nose g.setColor(Color.black); g.drawLine(275,225,325,300); g.drawLine(275,300,325,300); //hat //g.drawImage(img,225,60,300,110,0,0,237,208,null); g.drawImage(img,215,22,315,102,0,0,237,208,null); //glasses g.setColor(Color.black); g.drawRect(150,170,100,60); g.drawRect(295,170,100,60); g.drawLine(250,195,295,195); //eyebrows g.fillRect(160,150,75,10); g.fillRect(305,150,75,10); //writing- keep smiling //and to add text to the drawing... g.setFont (new Font("TimesRoman", Font.PLAIN, 100)); g.drawString("Keep Smiling" , 20, 600); Toolkit.getDefaultToolkit().sync(); g.dispose(); } public void run() { long beforeTime, timeDiff, sleep; beforeTime = System.currentTimeMillis(); int animationDelay = 500; long time = System.currentTimeMillis(); while (true) {//infinite loop // spriteManager.update(); repaint(); try { time += animationDelay; Thread.sleep(Math.max(0,time - System.currentTimeMillis())); } catch (InterruptedException e) { System.out.println(e); }//end catch }//end while loop }//end of run public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } }//end of class

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.