Java JSmileFace and JsmileFace2

meltyrain

Junior Member
Joined
Jun 30, 2013
Messages
6
Reaction score
0
This was asked:
a. Write an application that extends JFrame and that displays a yellow smiling face on
the screen. Save the file as JSmileFace.java.
b. Add a JButton to the JSmileFace program so the smile changes to a frown when
the user clicks the JButton. Save the file as JSmileFace2.java.
and my code for JSmileFace


// JSmileFace.java
import javax.swing.*;
import java.awt.*;

public class JSmileFace{
public static void main(String[] args) {
JSmileFace j = new JSmileFace();
}

public JSmileFace(){
JFrame frame = new JFrame("JSmileFace");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyComponent());
frame.setSize(400,400);
frame.setVisible(true);
}

public class MyComponent extends JComponent{
public void paint(Graphics g){

g.setColor(Color.yellow);
g.drawOval(100, 50, 200, 200);
g.setColor(Color.yellow);
g.fillOval(100,50,200,200);

g.setColor(Color.BLACK);
g.fillOval(155, 100, 10, 20);
g.fillOval(230, 100, 10, 20);



g.setColor(Color.BLACK);
g.drawArc(150, 160, 100, 50, 180, 180);
}}
}
public static void main(String[] args)
{
}

Now, my problem is the JSmileFace2 I have no idea on how I would start with it.
I need advice, ideas on how I will solve this problem and tips also where I need to learn about this that
can be applied also out here.

Thank you
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
This was asked:
a. Write an application that extends JFrame and that displays a yellow smiling face on
the screen. Save the file as JSmileFace.java.
b. Add a JButton to the JSmileFace program so the smile changes to a frown when
the user clicks the JButton. Save the file as JSmileFace2.java.
and my code for JSmileFace


...

Now, my problem is the JSmileFace2 I have no idea on how I would start with it.
I need advice, ideas on how I will solve this problem and tips also where I need to learn about this that
can be applied also out here.

Thank you

Your question falls under typical Java GUI development using Swing.

JFrame is the graphical representation of a Window on the screen You extend it so that you can have a canvas for your application with your own set of logics.

All graphical components in Swing inherited from Componet, or a subclass JComponent(in the context of Swing). In order to draw something, you overwrite the void paint(Graphics g) method. This method is invoked by the UI Manager each time it requires something on the screen.

"Window" in the context of UI is nothing more than a rectangular space. The Window you see in most graphical OS include borders, title, resizing handles and so forth. All these graphical elements are drawn by either the native UI implementation, or can also be part of Java graphical stack. Something need to be in charge of the drawing. Here you can perform your own drawing after Swing has drawn the necessary elements on their own. This is how graphical images appear in graphical canvas. The graphical system ask your code to draw and you draw on a space. The space is often described using a Graphical Context. In the case of Swing, the graphical Context is Graphics2D, a concrete subclass of Graphics.

There are also other ways to have a graphic in JFrame. Add an Image object, a JPanel or JView or anything that can draw eventually :)
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top