Results 1 to 7 of 7
Thread: Basic Face Application
- 03-11-2008, 12:54 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
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:
There are a couple bits I am unsure about the fist is changing the deafult size of the face.// <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();
}
}
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, 02:24 AM #2
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(); } }
- 03-11-2008, 03:26 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
Hey! Thank you so much for your help, thats exatcly how i wanted it to work!
:D
- 03-13-2008, 01:54 AM #4
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
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 01:53 PM.
- 03-13-2008, 11:19 PM #5
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);
- 03-14-2008, 06:18 PM #6
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
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 06:33 PM.
- 03-14-2008, 06:21 PM #7
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Basic question about EJB
By javaplus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 05:44 PM -
basic java help
By adred in forum New To JavaReplies: 0Last Post: 03-08-2008, 12:36 PM -
Basic Program Please Help!!
By VinceGuad in forum New To JavaReplies: 3Last Post: 02-01-2008, 03:35 PM -
Basic Graphic
By jkswebsite in forum Java 2DReplies: 6Last Post: 11-26-2007, 02:19 AM -
help with basic example
By fred in forum New To JavaReplies: 1Last Post: 07-20-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks