Results 1 to 7 of 7
  1. #1
    adam87 is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default Basic Face Application

    Hey, I'm currently at University and been given a task to make a simple face application that you change the colour of face and also change the size of the face. So far i've got this:

    // <applet code=RedGreen1.class
    // width=200 height=150></applet>

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;

    public class RedGreen1 extends Applet
    implements ActionListener, AdjustmentListener
    {
    private int diameter = 40;
    private Color color = Color.red;
    private Button red, green;
    private Scrollbar slider;
    private int sliderValue;

    public void init()
    {
    Label sliderLabel = new Label("Size:");
    add(sliderLabel);
    slider = new Scrollbar(Scrollbar.HORIZONTAL,
    20, 1, 0, 101);
    add(slider);
    slider.addAdjustmentListener(this);
    sliderValue = slider.getValue();
    red = new Button("Red");
    add(red);
    red.addActionListener(this);
    green = new Button("Green");
    add(green);
    green.addActionListener(this);
    }

    public void paint(Graphics g)
    {
    g.setColor(color);
    g.fillOval(40, 40, sliderValue, sliderValue);
    g.setColor(color.black);
    g.fillOval(45, 45, 5, 5);
    g.fillOval(52, 45, 5, 5);
    g.fillOval(50, 50, 2, 2);

    }

    public void actionPerformed(ActionEvent event)
    {
    if (event.getSource() == red)
    color = Color.red;
    if (event.getSource() == green)
    color = Color.green;
    repaint();
    }

    public void
    adjustmentValueChanged(AdjustmentEvent e)
    {
    sliderValue = slider.getValue();
    repaint();
    }
    }
    There are a couple bits I am unsure about the fist is changing the deafult size of the face.

    Secondly I would like the slider thange change all parts of the face and keep it in ratio.

    Any help will be appreciated

    Adam Huxtable

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    // <applet code=RG1 width=200 height=150></applet>
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    
    public class RG1 extends Applet implements ActionListener,
                                               AdjustmentListener
    {
        Point faceCenter;
        private int diameter = 40;
        private Color color = Color.red;
        private Button red, green;
        private Scrollbar slider;
        private int sliderValue;
    
        public void init() 
        {
            Label sliderLabel = new Label("Size:");
            add(sliderLabel);
            slider = new Scrollbar(Scrollbar.HORIZONTAL, 20, 1, 0, 101);
            add(slider);
            slider.addAdjustmentListener(this);
            sliderValue = slider.getValue();
            red = new Button("Red");
            add(red);
            red.addActionListener(this);
            green = new Button("Green");
            add(green);
            green.addActionListener(this);
        }
    
        public void paint(Graphics g) 
        {
            if(faceCenter == null) {
                int w = getWidth();
                int h = getHeight();
                faceCenter = new Point(w/2, h*2/3);
            }
            int x = faceCenter.x - sliderValue/2;
            int y = faceCenter.y - sliderValue/2;
            g.setColor(color);
            g.fillOval(x, y, sliderValue, sliderValue);
            g.setColor(color.black);
            double scale = sliderValue/20.0;  // initial value
            int offsetX = (int)(scale*5);
            int offsetY = (int)(scale*5);
            int eyeDia  = (int)(scale*5);
            int noseDia = (int)(scale*3);
            x = faceCenter.x - offsetX - eyeDia/2;
            y = faceCenter.y - offsetY;
            g.fillOval(x, y, eyeDia, eyeDia);
            x = faceCenter.x + offsetX - eyeDia/2;
            g.fillOval(x, y, eyeDia, eyeDia);
            x = faceCenter.x - noseDia/2;
            y = faceCenter.y - noseDia/2;
            g.fillOval(x, y, noseDia, noseDia);
        }
    
        public void actionPerformed(ActionEvent event) 
        {
            if (event.getSource() == red)
                color = Color.red;
            if (event.getSource() == green)
                color = Color.green;
            repaint();
        }
    
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            sliderValue = slider.getValue();
            repaint();
        }
    }

  3. #3
    adam87 is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    Hey! Thank you so much for your help, thats exatcly how i wanted it to work!

    :D

  4. #4
    adam87 is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    Hey, I have had a go at adding the mouth but when I add it, it doesnt show up! any suggestions on how i would add a mouth?
    Last edited by adam87; 03-13-2008 at 02:53 PM.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
            // Mouth.
            offsetY = (int)(scale*7);
            int w = 2*offsetX;
            int h =  (int)(scale*5);
            x = faceCenter.x - offsetX;
            y = faceCenter.y + offsetY - h;
            g.fillArc(x, y, w, h, 175, 190);

  6. #6
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    You can find another solution from <URL removed> (windyawarnapura: Next time give exact url to the solution and be careful while promoting your own site!)
    Last edited by JavaBean; 03-14-2008 at 07:33 PM.

  7. #7
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:44 PM
  2. basic java help
    By adred in forum New To Java
    Replies: 0
    Last Post: 03-08-2008, 01:36 PM
  3. Basic Program Please Help!!
    By VinceGuad in forum New To Java
    Replies: 3
    Last Post: 02-01-2008, 04:35 PM
  4. Basic Graphic
    By jkswebsite in forum Java 2D
    Replies: 6
    Last Post: 11-26-2007, 03:19 AM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •