6 Replies - 465 Views - Last Post: 29 November 2012 - 09:38 PM Rate Topic: -----

#1 GAlba89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 12

Swing Timer for Smiley Face

Posted 29 November 2012 - 06:19 PM

Hi Guys, So I'm trying to put together a project that requires me to do a smiley face and in 2 seconds turn it in a frown face. I know I have to use swing timer but Ive tried everything and Im just not getting it to work. Any Suggestions?

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 :code:

This post has been edited by pbl: 29 November 2012 - 06:25 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Swing Timer for Smiley Face

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Swing Timer for Smiley Face

Posted 29 November 2012 - 06:29 PM

Why that
Graphics g2 = canvas.getGraphics();

use the Graphics object received as parameter it is the most up to date one

What starting the Timer in the paint() method... you can';t start it every time it is called

where is isHappy changed ?
what is DELAY ?
Was This Post Helpful? 0
  • +
  • -

#3 GAlba89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 12

Re: Swing Timer for Smiley Face

Posted 29 November 2012 - 07:11 PM

what is it suppose to look like as far the timer goes?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Swing Timer for Smiley Face

Posted 29 November 2012 - 07:17 PM

What do you mean ? I am completly lost
How does a Timer can have a look ?
And you didn't answer any of my question :v:
Was This Post Helpful? 0
  • +
  • -

#5 GAlba89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 12

Re: Swing Timer for Smiley Face

Posted 29 November 2012 - 07:23 PM

Graphics g2 = canvas.getGraphics(); // draws in the canvas

ishappy // is suppose to change to isSad every 2 seconds

Im not sure why the Delay i was looking through some old textbooks when I saw that it was used as part of the timer.

Sorry if this doesnt clarify much..but any help would be greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Swing Timer for Smiley Face

Posted 29 November 2012 - 07:28 PM

Would be stupid to use Canvas Graphics object when the piant() method provides you with the lastest updated one

You never change isHappy ... or I missed that part of the code

If you are not sure about DELAY is that your code ???
Was This Post Helpful? 1
  • +
  • -

#7 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: Swing Timer for Smiley Face

Posted 29 November 2012 - 09:38 PM

View Postpbl, on 30 November 2012 - 02:28 AM, said:

Would be stupid to use Canvas Graphics object when the piant() method provides you with the lastest updated one

You never change isHappy ... or I missed that part of the code

If you are not sure about DELAY is that your code ???


I don't think it's worth wasting your time. This person is not listening.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1