[SOLVED] JButton with 2D Graphics help
Hello, I'm having some issues getting a JButton to show on a frame where I have created some 2D graphics objects. Objective is to have the button change the smile displayed to a frown when the button is pressed. Any help would be much appreciated.
I have tried adding the button both in the constructor and in the paint method. I have also tried repositioning it in both methods. ~X(
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class JSmileyFace2 extends JFrame implements ActionListener
{
private JButton mouth = new JButton("Smile");
public JSmileyFace2()
{
super("Smile");
setVisible(true);
setSize(300,320);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(mouth);
mouth.addActionListener(this);
}
public void paint(Graphics gr)
{
super.paint(gr);
Graphics2D gr2d = (Graphics2D)gr;
BasicStroke brush = new BasicStroke(5.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
gr2d.setStroke(brush);
//Draws outer smiley
gr2d.setPaint(Color.YELLOW);
gr2d.fill(new Ellipse2D.Float(30F, 80F, 200F, 200F));
gr2d.setPaint(Color.BLACK);
gr2d.draw(new Ellipse2D.Float(30F, 80F, 200F, 200F));
//draws eyes
gr2d.fill(new Ellipse2D.Float(90F, 110F, 20F, 60F));
gr2d.fill(new Ellipse2D.Float(150F, 110F, 20F, 60F));
//draws mouth based on button text
if(mouth.getText().equals("Smile")) //draws smile
gr2d.draw(new Arc2D.Float(70F, 140F, 120F, 120F, 180F, 180F, Arc2D.OPEN));
else //draws frown
gr2d.draw(new Arc2D.Float(70F, 180F, 120F, 120F, 180F, -180F, Arc2D.OPEN));
mouth.setLocation(20,20);
}
public void actionPerformed(ActionEvent e)
{
if(mouth.getText().equals("Smile"))
mouth.setText("Frown");
else
mouth.setText("Smile");
repaint();
}
public static void main(String[] args)
{
JSmileyFace2 frame = new JSmileyFace2();
}
}
Re: JButton with 2D Graphics help
try setting the button to be visible.
Re: JButton with 2D Graphics help
Re: JButton with 2D Graphics help
You never set the text of the JButton mouth to anything.
At least not in the constructor.
Re: JButton with 2D Graphics help
You never set the text of the button.
Is your starting one "Smile" or "Frown"?
Re: JButton with 2D Graphics help
Text for the smile is set in the line just before the constructor and outside of any methods.
Code :
private JButton mouth = new JButton("Smile");
That should set the starting text as "Smile"
And now I have discovered that I am a moron. My problem is not in my code, but a problem with NetBeans. This was originally started as just a smiley face, then the request for the smile/frown button was added. I created a second file for the button request, but created it in the same project. Every time I was running the code, it was running the original file with no button due to it being set as the main class for the project. Thanks for the help all. Sorry for wasting your time.
Re: JButton with 2D Graphics help
Also of note for anyone else doing this as a homework assignment out of the book "Java Programming" (Fifth Edition) by Joyce Farrell (Page 757, problem 10):
The above code works perfectly as soon as a layout manager is added. I used
Code :
setLayout(new FlowLayout());
and added it to the JSmileyFace2 constructor.
Re: JButton with 2D Graphics help
Custom painting in Swing is done by overriding paintComponent(...) in a JComponent / JPanel, not paint(...) in a top level window. Read the API for JComponent#paint(...) for better understanding.
Recommended reading:
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db