Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-11-2008, 01:54 AM
Member
 
Join Date: Mar 2008
Posts: 3
adam87 is on a distinguished road
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:

Quote:
// <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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-11-2008, 03:24 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-11-2008, 04:26 AM
Member
 
Join Date: Mar 2008
Posts: 3
adam87 is on a distinguished road
Hey! Thank you so much for your help, thats exatcly how i wanted it to work!

Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-13-2008, 02:54 AM
Member
 
Join Date: Mar 2008
Posts: 3
adam87 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-14-2008, 12:19 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-14-2008, 07:18 PM
Member
 
Join Date: Mar 2008
Posts: 3
windyawarnapura is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-14-2008, 07:21 PM
Member
 
Join Date: Mar 2008
Posts: 3
windyawarnapura is on a distinguished road
Best site for beginers Q's
her to best beginers site
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic question about EJB javaplus Enterprise JavaBeans 2 07-15-2008 06:44 PM
basic java help adred New To Java 0 03-08-2008 01:36 PM
Basic Program Please Help!! VinceGuad New To Java 3 02-01-2008 04:35 PM
Basic Graphic jkswebsite Java 2D 6 11-26-2007 03:19 AM
help with basic example fred New To Java 1 07-20-2007 06:45 PM


All times are GMT +3. The time now is 08:31 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org