help needed on jbuttons

i have made 2 smilies using ovals and arcs I want to control each of them by a Jbutton smile and sad. If any one has a similar program or anything related please let me know. any help is appreciated
[205 byte] By [crystalaruna] at [2007-11-26 22:49:11]
# 1

> 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();

}

hiwaa at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 2
sorry... this is not what I want .... i have made two classes .... sad and smile in which i have generated 2 faces... i have to put 2 jbuttons amile and sad now. so when i click on smile smile face appears and when i click on sad the sad face appearsthanks
crystalaruna at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 3
Swing related questions should be posted in the Swing forum.> when i click on smile smile face appears and when i click on sad the sad face appearsThe sad face appears where?You questions is so vague we can't give an answer.
camickra at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 4
> You questions is so vague we can't give an answer.Except to do this:8-(Which may be what you were looking for?!?
DrLaszloJamfa at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 5

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

crystalaruna at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 6
It's not clear where you are stuck. Your MMPanel class needs a booleanfield for state -- when happy only draw the smile, when not happy, only draw the frown.
DrLaszloJamfa at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

camickra at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 8

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();

}

}

hiwaa at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...
# 9
Thanks man... u r genius... :-)
crystalaruna at 2007-7-10 12:09:29 > top of Java-index,Java Essentials,Java Programming...