> I want to control each of them
What do you mean by 'control'?
Is this what you want?
/* event handler for your JButton */
public void actionPerformed(ActionEvent ae){
if (.....){ // check some condition from button
mode = smile;
}
else{
mode = sad;
}
smilie.repaint();
}
i am sorry my english is not so good. let me describe in points
1. I made a simling face class using arc and circle in J frame :-)
2. i made a sad face class using arc and circle in J frame :-(
3. i want to add two Jbuttons [SMILE] [SAD]
4. I click [SMILE] and :-) appears in Jframe.
5. I press [SAD] and :-( appears in Jframe.
here is my code
import java.awt.* ;
import javax.swing.* ;
class MManPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.red);
int xOffSet = 200;
g.drawOval(50,50,50,50);
g.setColor(Color.blue);
int yOffSet = 100;
g.drawArc(65,80,20,10,0,-180);
g.drawOval(60,65,7,7);
g.drawOval(80,65,7,7);
g.setColor(Color.red);
g.drawOval(90,50,50,50);
g.setColor(Color.blue);
g.drawArc(105,80,20,10,0,180);
g.drawOval(100,65,7,7);
g.drawOval(120,65,7,7);
}
}
class MMan extends JFrame
{
MManPanel mmp = new MManPanel();
public MMan()
{
setTitle("Faces");
add(mmp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
}
class MManDriver
{
public static void main(String args[])
{
MMan mm = new MMan();
}
}
Please help me if possible
I don't see any code related to JButtons. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/button.html]How to Use Buttons[/url].
The posted code won't work because your panel has a size of (10, 10) I believe so none of your custom painting shows up. You need to give the panel a preferred size.
A better solution is to extend Icon and do the custom painting in the icon. Then you can add the icon to a JLabel (or any other Swing component that uses icons) and then add the JLabel to the frame.
Wow, here's a quick and dirty code. Hope this helps your learning process.
/* save and compile as MManDriver.java */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MManPanel extends JPanel{
String mode = "smile";
public void paintComponent(Graphics g){
super.paintComponent(g);
if (mode.equals("smile")){
g.setColor(Color.red);
int xOffSet = 200; // not used ?
g.drawOval(50,50,50,50);
g.setColor(Color.blue);
int yOffSet = 100; // not used ?
g.drawArc(65,80,20,10,0,-180);
g.drawOval(60,65,7,7);
g.drawOval(80,65,7,7);
}
else{
g.setColor(Color.red);
g.drawOval(90,50,50,50);
g.setColor(Color.blue);
g.drawArc(105,80,20,10,0,180);
g.drawOval(100,65,7,7);
g.drawOval(120,65,7,7);
}
}
}
class MMan extends JFrame{
JButton smile, sad;
MManPanel mmp;
public MMan(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Faces");
mmp = new MManPanel();
smile = new JButton("SMILE");
sad = new JButton("SAD");
getContentPane().add(mmp, BorderLayout.CENTER);
JPanel jp = new JPanel();
jp.add(smile);
jp.add(sad);
ButtonListener btn = new ButtonListener();
smile.addActionListener(btn);
sad.addActionListener(btn);
getContentPane().add(jp, BorderLayout.SOUTH);
setSize(400, 400);
setVisible(true);
}
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == smile){
mmp.mode = "smile";
}
else{
mmp.mode = "sad";
}
mmp.repaint();
}
}
}
public class MManDriver{
public static void main(String args[]){
MMan mm = new MMan();
}
}