package happysadface1;
import java.awt.*;
import javax.swing.JColorChooser;
import javax.swing.Timer;
import java.awt.event.*;
public class Form1 extends javax.swing.JFrame
{
private Color eyeColor;
private Timer timer;
public Form1()
{
initComponents();
eyeColor = Color.blue;
timer = new Timer(DELAY, new otherListener());
}
private boolean isHappy = true;
@Override
public void paint(Graphics g)
{
super.paint(g);
// drawing in the canvas...
Graphics g2 = canvas.getGraphics();
// draw the head
int x = 20;
int y = 20;
int width = canvas.getWidth() - 2*x;
int height = canvas.getHeight() - 2*y;
g2.drawOval(x, y, width, height);
// draw the eyes, left then right
int x1 = x + width/5;
int y1 = y + height/4;
int width1 = 60;
int height1 = 30;
g2.setColor(eyeColor);
g2.fillOval(x1, y1, width1, height1);
x1 += 2*width/5;
g2.fillOval(x1, y1, width1, height1);
// draw the nose as 2 lines
Point P1 = new Point(x + width/2, y1 + height1+20);
Point P2 = new Point(P1.x - 30, P1.y + 35);
g2.setColor(Color.black);
g2.drawLine(P1.x, P1.y, P2.x, P2.y);
Point P3 = new Point(P1.x, P2.y);
g2.drawLine(P2.x, P2.y, P3.x, P3.y);
// draw the mouth depending on isHappy
int x3 = x + 50;
int y3 = P2.y + 20;
int width3 = width - 100;
int height3 = 50;
int startAngle;
int sweep;
timer.start();
if (isHappy)
{
// draw smiling face
startAngle = 0;
sweep = -180;
}
else
{
// draw sad face
startAngle = 0;
sweep = 180;
}
g2.drawArc(x3, y3, width3, height3, startAngle, sweep);
// draw the caption
Font myFont = new Font("SansSerif",
Font.BOLD | Font.ITALIC, 14);
g2.setColor(Color.BLACK);
g2.setFont(myFont);
if (isHappy)
{
g2.drawString("Woo Hoo!", 5, 20);
}
else
{
g2.drawString("Bleah!", 5, 20);
}
private class otherListener implements Action Listener
{
public void actionPerformed (ActionEvent event)
{
repaint();
}
}
}
*Edited: please
This post has been edited by pbl: 29 November 2012 - 06:25 PM
Reason for edit:: Fixed code tags

New Topic/Question
Reply



MultiQuote





|