display image when button clicked

Reply

Join Date: Oct 2006
Posts: 730
Reputation: hbk619 is an unknown quantity at this point 
Solved Threads: 7
hbk619's Avatar
hbk619 hbk619 is offline Offline
Master Poster

display image when button clicked

 
0
  #1
Mar 22nd, 2007
I've been googling and going round in circles, simply put: when the button is clicked i want an image of a card to appear.

  1. class Actions extends JFrame implements ActionListener
  2. {
  3. JTextArea textarea = new JTextArea(2,25);
  4. JButton twist = new JButton("Twist");
  5. JButton bust = new JButton("Bust");
  6. public Actions()
  7. {
  8. super("Twist or bust!"); //text in title bar
  9. setSize(300,200); //size of window
  10. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //when window closed close app
  11. setVisible(true); //make window appear
  12.  
  13. Container content = getContentPane(); //main content area for window
  14. content.setBackground(Color.BLACK); //colour of area
  15.  
  16. FlowLayout flowManager = new FlowLayout();
  17. content.setLayout(flowManager);
  18.  
  19. /* CODE FOUND ON SUN SITE //Create a button, attach a listener and put the button in a visible spot
  20.   JButton clicker = new JButton("Press me to execute ActionPerformed");
  21.   clicker.addActionListener(new ActionListener() {
  22.   public void actionPerformed(ActionEvent e)
  23.   {
  24.   //change appearance for a thingie that is in a visible spot
  25.   labelInContainerDisplayingImageWhenActionPerformedExecutes.setIcon(new ImageIcon(getClass().getResource("anImage.jpg")));
  26.   }
  27.   });
  28.   getContentPane().add(clicker, BorderLayout.SOUTH);
  29.   }
  30. */
  31.  
  32.  
  33. twist.addActionListener(new ActionListener()
  34. {
  35. public void actionPerformed(ActionEvent e)
  36. {
  37. getImage(getCodeBase(),"nufc.gif");
  38. }
  39. });
  40. getContentPane().add(twist, BorderLayout.SOUTH);
  41.  
  42. bust.addActionListener(this);
  43.  
  44. content.add(twist);
  45. content.add(bust);
  46. content.add(textarea);
  47. setContentPane(content);
  48. }
  49. public void actionPerformed(ActionEvent e) {
  50. {
  51. Actions.setIcon(new ImageIcon(getClass().getResource("nufc.gif")));
  52. }
  53. }
  54. /* public void actionPerformed(ActionEvent e)
  55.   {
  56.   String str="Damn!";
  57.   if(event.getSource()==bust) str+="You lost";
  58.   Image pic = Toolkit.getDefaultToolkit().getImage("nufc.gif");
  59.   if(pic != null) painter.drawImage(pic,15,5,this);
  60.  
  61. // someother coding i found in google
  62.  
  63.  
  64.   }
  65. }*/

I've got a book which tells how to show an image, how to play music when a button is clicked but not how to do an image when a button is clicked. help appriciated

and once thats sorted, then display an image randomly choosen from a deck of cards, which means each card has a value, then the values added and that decides if you get another go or you lose.. what i'm trying in C http://www.daniweb.com/techtalkforums/thread72970.html

but i want to add graphics to it, rather than just the command prompt.

so did that make any sense?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: display image when button clicked

 
0
  #2
Mar 22nd, 2007
Best to take it one step at a time. Create your class for your cards. I.e. consider what methods you are going to need etc.

Then when you've got that locked down move to the GUI.
Last edited by iamthwee; Mar 22nd, 2007 at 4:28 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC