Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Face Changer Application for school

  1. #1
    Junior Member Spencer4908's Avatar
    Join Date
    Jul 2014
    Location
    UTAH, USA
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 1 Time in 1 Post

    Thumbs up Face Changer Application for school

    I'm currently in a Object Oriented Programming class in school, and need help figuring out a few issues with an assignment.

    The assignment asked us to make a program that looks like this.
    Want it to look like.jpg

    My application currently looks like this.
    Currently Looks Like.jpg

    The update button and check boxes all work, I'm just having issues getting it to look like its suppose to. I also can't figure out how to draw a JLabel on top of another JLabel, essentially stacking them on top of each other, that's why the back of the head isn't being drawn.

    This is the source code of my program, I use Eclipse in case your wondering.
    faceAppSource.rar

    So what I need help with is the repositioning of the left menu bar so it looks similar to the example picture, and I need to create a filled circle for the shape of the head in the right panel either with a ImageIcon or a fillOval().

    I apologize in advance if what I'm asking for doesn't make much sense. However if you need additional information, I would be glad to oblige.
    Last edited by Spencer4908; July 6th, 2014 at 05:25 PM. Reason: Question Was solved.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Face Changer Application for school

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Users prefer to see relevant code posted properly as described at the above link.

    For your left panel, you appear to have used a different layout manager that divides the available space equally, like GridLayout, while the target view uses another layout manager. Experiment with different layout managers to obtain the desired appearance. Also set the preferred width of the left panel to obtain the desired width.

    For the face, only one component can occupy a space at a time, so there's no "draw a JLabel on top of another JLabel." It seems to me you should be drawing the facial features as graphics elements, but I don't know what you're working with and could be wrong.

    Post your code as I've suggested, and we can help more.

  3. #3
    Junior Member Spencer4908's Avatar
    Join Date
    Jul 2014
    Location
    UTAH, USA
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Face Changer Application for school

    Thanks for the advice, I tried a few other layouts but I'll keep trying to get it to look right.

    Here's the source code.
    package application.face;
     
    import java.awt.BorderLayout;
     
    @SuppressWarnings("serial")
    public class FaceApp extends JFrame
    {
     
    	private JPanel contentPane;
     
    	public static void main(String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				try
    				{
    					FaceApp frame = new FaceApp();
    					frame.setVisible(true);
    				} catch (Exception e)
    				{
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	public FaceApp()
    	{
     
    	//Create Icons
    		final Icon eyesChoiceA = new ImageIcon(FaceApp.class.getResource("/res/eyeChoiceA.png"));
    		final Icon eyesChoiceB = new ImageIcon(FaceApp.class.getResource("/res/eyeChoiceB.png"));
    		final Icon eyesChoiceC = new ImageIcon(FaceApp.class.getResource("/res/eyeChoiceC.png"));
     
    		final Icon noseChoiceA = new ImageIcon(FaceApp.class.getResource("/res/noseChoiceA.png"));
    		final Icon noseChoiceB = new ImageIcon(FaceApp.class.getResource("/res/noseChoiceB.png"));
    		final Icon noseChoiceC = new ImageIcon(FaceApp.class.getResource("/res/noseChoiceC.png"));
     
    		final Icon mouthChoiceA = new ImageIcon(FaceApp.class.getResource("/res/smileChoiceA.png"));
    		final Icon mouthChoiceB = new ImageIcon(FaceApp.class.getResource("/res/smileChoiceB.png"));
    		final Icon mouthChoiceC = new ImageIcon(FaceApp.class.getResource("/res/smileChoiceC.png"));
     
     
    	//Frame
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(700, 520);
    		setLocationRelativeTo(null);
    		contentPane = new JPanel();
    		contentPane.setBackground(Color.BLUE);
    		contentPane.setLayout(new BorderLayout());
    		setContentPane(contentPane);
     
    	//Center Panel (Picture)	
    		JPanel drawBox = new JPanel();
    		drawBox.setBorder(new EmptyBorder(5, 5, 5, 5));
    		drawBox.setBackground(Color.BLUE);
    		drawBox.setLayout(new FlowLayout());
    		contentPane.add(drawBox, BorderLayout.CENTER);
     
    		final JLabel eyesLabel = new JLabel();
    		eyesLabel.setIcon(eyesChoiceA);
    		final JLabel noseLabel = new JLabel();
    		noseLabel.setIcon(noseChoiceA);
    		final JLabel mouthLabel = new JLabel();
    		mouthLabel.setIcon(mouthChoiceA);
     
    		drawBox.add(eyesLabel);
    		drawBox.add(noseLabel);
    		drawBox.add(mouthLabel);
     
    	//West Panel (Buttons)	
    		JPanel panel = new JPanel();
    		panel.setBorder(new CompoundBorder());
    		panel.setBackground(Color.LIGHT_GRAY);
    		panel.setLayout(new GridLayout(5, 1, 5, 5));
    		contentPane.add(panel, BorderLayout.WEST);
     
     
     
    		JLabel lblYouChoose = new JLabel("You Choose:");
    		panel.add(lblYouChoose);
     
    		final JCheckBox chckbxEyes = new JCheckBox("Eyes:");
    		chckbxEyes.setBackground(new Color(192, 192, 192));
    		panel.add(chckbxEyes);
     
    		final JCheckBox chckbxNose = new JCheckBox("Nose:");
    		chckbxNose.setBackground(new Color(192, 192, 192));
    		panel.add(chckbxNose);
     
    		final JCheckBox chckbxMouth = new JCheckBox("Mouth:");
    		chckbxMouth.setBackground(new Color(192, 192, 192));
    		panel.add(chckbxMouth);
     
    		JButton btnUpdate = new JButton("Update");
    		btnUpdate.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent arg0) 
    			{
    				if (chckbxEyes.isSelected())
    				{
    					if(eyesLabel.getIcon() == eyesChoiceA )
    						eyesLabel.setIcon(eyesChoiceB);
    					else if(eyesLabel.getIcon() == eyesChoiceB)
    						eyesLabel.setIcon(eyesChoiceC);
    					else
    						eyesLabel.setIcon(eyesChoiceA);
    				}
     
    				if (chckbxNose.isSelected())
    				{
    					if(noseLabel.getIcon() == noseChoiceA )
    						noseLabel.setIcon(noseChoiceB);
    					else if(noseLabel.getIcon() == noseChoiceB)
    						noseLabel.setIcon(noseChoiceC);
    					else
    						noseLabel.setIcon(noseChoiceA);
    				}
     
    				if (chckbxMouth.isSelected())
    				{
    					if(mouthLabel.getIcon() == mouthChoiceA )
    						mouthLabel.setIcon(mouthChoiceB);
    					else if(mouthLabel.getIcon() == mouthChoiceB)
    						mouthLabel.setIcon(mouthChoiceC);
    					else
    						mouthLabel.setIcon(mouthChoiceA);
    				}
     
    			}
    		});
     
    		panel.add(btnUpdate);
     
     
     
     
     
    	}
    }

    The learning objectives of the assignment are:
    -Paint images on a JPanel
    -Dynamically update the images
    -Continue your practice of event handling
    - Use check boxes
    - Practice composition

    hopefully that gives you a fair idea of what is required from the assignment. It said to paint the images on JPanel, so I assumed I needed to add the images as icons. However isn't it possible to add a graphics object to a panel.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Face Changer Application for school

    I assumed I needed to add the images as icons. However isn't it possible to add a graphics object to a panel
    Yes. Since the assignment says "Paint images on a JPanel," I agree with you and would expect to see your program override JPanel.paintComponent() and paint or draw the graphics images directly on the JPanel's graphics object rather than adding the graphics to a JLabel and adding those to a JPanel using a layout manager.

    If you're unfamiliar with painting on a graphics object, there's a great Oracle lesson, Performing Custom Painting, that should help.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Spencer4908 (July 6th, 2014)

  6. #5
    Junior Member Spencer4908's Avatar
    Join Date
    Jul 2014
    Location
    UTAH, USA
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Face Changer Application for school

    Thank you for your help I was able to make everything exactly how it needed to be.
    AllDone.jpg

    Thanks again!

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Face Changer Application for school

    Good job!

Similar Threads

  1. GUI inter face
    By mrgregglles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2013, 11:36 AM
  2. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM