|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

03-11-2008, 01:54 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
|
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
|
|

03-11-2008, 03:24 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
// <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();
}
}
|
|

03-11-2008, 04:26 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
Hey! Thank you so much for your help, thats exatcly how i wanted it to work!

|
|

03-13-2008, 02:54 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
|
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.
|
|

03-14-2008, 12:19 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
// 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);
|
|

03-14-2008, 07:18 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
|
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.
|
|

03-14-2008, 07:21 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
|
Best site for beginers Q's
her to best beginers site
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|