944,159 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8248
  • Java RSS
Mar 22nd, 2007
0

display image when button clicked

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 273
Solved Threads: 8
Master Poster
hbk619 is offline Offline
733 posts
since Oct 2006
Mar 22nd, 2007
0

Re: display image when button clicked

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Applet MouseDragged Event
Next Thread in Java Forum Timeline: Magic Square





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC