473,795 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please Help! Changing a graphic via a menu with listeners

10 New Member
hey Im trying to make a game of pairs (the memory game) i have set up a grid which displays a graphic for the card back. im trying to link the edit menu to choose the graphic to be displayed. i've set up a string variable called deck which will contain the path to the required image. and actionlisteners DA and DB corresponding to which style is choosen in the menu. I had to declare the deck to some initial image so it can load.
How do i impliment the change once the menuitem has been selected?
below is my code so far.


Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3.  
  4. import java.awt.*;
  5.  
  6. import java.awt.event.*;
  7.  
  8. import javax.swing.event.*;
  9.  
  10. import java.net.*;
  11.  
  12.  
  13.  
  14. class Filenew extends JFrame implements ActionListener
  15.  
  16. {
  17.  
  18.    JMenuItem Exit, DA, DB, About, Instructions;
  19.  
  20.    Filenew gameFrame = null;
  21.  
  22.    String deck = "images/aqua.jpg";
  23.    JPanel[][] q;
  24.  
  25.    public static void main (String[] args)
  26.  
  27.    {
  28.  
  29.       Filenew frame = new Filenew();
  30.  
  31.       frame.display();
  32.  
  33.    }
  34.  
  35.    public Filenew()
  36.    {
  37.       this(2, 2);
  38.    }
  39.  
  40.    public Filenew(int x, int y)
  41.  
  42.    {
  43.       setBackground(Color.red);
  44.  
  45.       JMenuBar menubar = new JMenuBar();
  46.  
  47.       setJMenuBar(menubar);
  48.  
  49.       JMenu filemenu = new JMenu("File");
  50.  
  51.       menubar.add(filemenu);
  52.  
  53.       JMenu New = new JMenu("New");
  54.       JMenuItem New43 = new JMenuItem("4x3");
  55.       New.add(New43);
  56.       JMenuItem New44 = new JMenuItem("4x4");
  57.       New.add(New44);
  58.       JMenuItem New54 = new JMenuItem("5x4");
  59.       New.add(New54);
  60.  
  61.       Exit = new JMenuItem("Exit");
  62.  
  63.       filemenu.add(New);
  64.  
  65.       filemenu.add(Exit);
  66.  
  67.       JMenu editmenu = new JMenu("Edit");
  68.  
  69.       menubar.add(editmenu);
  70.       JMenuItem ChangeD = new JMenu("Change Deck");
  71.       editmenu.add(ChangeD);
  72.       JMenuItem DA = new JMenuItem("Style 1");
  73.       ChangeD.add(DA);
  74.       JMenuItem DB = new JMenuItem("Style 2");
  75.       ChangeD.add(DB);
  76.       JMenuItem ChangeB = new JMenu("Change Background");
  77.       editmenu.add(ChangeB);
  78.       JMenuItem BA = new JMenuItem("Style 1");
  79.       ChangeB.add(BA);
  80.       JMenuItem BB = new JMenuItem("Style 2");
  81.       ChangeB.add(BB);
  82.  
  83.  
  84.  
  85.       New43.setActionCommand("43");
  86.       New44.setActionCommand("44");
  87.       New54.setActionCommand("54");
  88.       Exit.setActionCommand("Exit");
  89.       DA.setActionCommand("DA");
  90.       DB.setActionCommand("DB");
  91.  
  92.       New43.addActionListener(this);
  93.       New44.addActionListener(this);
  94.       New54.addActionListener(this);
  95.  
  96.       Exit.addActionListener(this);
  97.       DA.addActionListener(this);         
  98.  
  99.       DB.addActionListener(this);
  100.  
  101.  
  102.  
  103.  
  104.       getContentPane().setLayout(new GridLayout(x,y));
  105.  
  106.       q = new JPanel[x][y];
  107.  
  108.       for (int i=0; i<x; i++)
  109.  
  110.       {
  111.  
  112.          for (int j=0; j<y; j++)
  113.  
  114.          {
  115.  
  116.             q[i][j] = new JPanel();
  117.  
  118.             getContentPane().add(q[i][j]);
  119.             URL address = myscene.class.getResource(deck);
  120.  
  121.             ImageIcon image = new ImageIcon(address);
  122.  
  123.             JLabel cardback = new JLabel(image);
  124.  
  125.             q[i][j].add(cardback);
  126.  
  127.          }
  128.  
  129.       }
  130.  
  131.    }
  132.  
  133.  
  134.  
  135.    void display()
  136.  
  137.    {
  138.  
  139.       setTitle("The World's Most Amazing Game of Pairs to Ever Have Existed");
  140.  
  141.       setDefaultCloseOperation(EXIT_ON_CLOSE);
  142.  
  143.       setVisible(true);
  144.       setSize(600,400);
  145.  
  146.       setMinimumSize(new Dimension (600,400));
  147.  
  148.  
  149.    }
  150.  
  151.  
  152.  
  153.    protected void Exit()
  154.  
  155.    {
  156.  
  157.       System.exit(0);
  158.  
  159.    }
  160.  
  161.  
  162.  
  163.    public void actionPerformed(ActionEvent event)
  164.    {
  165.       String source = event.getActionCommand(); 
  166.  
  167.       if(source.equals ("43"))
  168.       {
  169.          newGame(3, 4);
  170.       }
  171.       if(source.equals("44"))
  172.       {
  173.          newGame(4, 4);
  174.       }
  175.       if(source.equals("54"))
  176.       {
  177.          newGame(4, 5);
  178.       }
  179.       if(source.equals("Exit"))
  180.  
  181.       {
  182.  
  183.          Exit();
  184.  
  185.       }
  186.       if(source.equals("DA"))
  187.       {
  188.          deck = "images/pairs.jpg";
  189.  
  190.       }
  191.       if(source.equals("DB"))
  192.       {
  193.          deck = "images/carddeck.jpeg";
  194.       }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.    }
  201.    public void newGame(int x, int y)
  202.    {
  203.       closeGame();
  204.       gameFrame = new Filenew(x, y);
  205.       gameFrame.display();
  206.    }
  207.    public void closeGame()
  208.    {
  209.       if (gameFrame != null)
  210.       {
  211.          gameFrame.dispose();
  212.          gameFrame = null;
  213.       }
  214.    }
  215.  
  216. }
  217.  
Any suggestion welcome
Feb 27 '08 #1
1 1343
BigDaddyLH
1,216 Recognized Expert Top Contributor
Call setIcon(yourNew IconImage) on the JLabel(s) whose image you want to change.
Feb 27 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3497
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql, because the dates must have quotes on each side. I just don't know how make the dates right. Well I'll just show you the code and some insert statements it generates. Could anyone please help me?
2
1931
by: m3ckon | last post by:
Hi there, had to rush some sql and am now going back to it due to a slow db performance. I have a db for sales leads and have created 3 views based on the data I need to produce. However one o the views, which has subqueries to the other views is VERY slow and it needs to be speeded up, but am unsure how, can anyone help... below is the sql?
3
2329
by: Lodewijk van Haringhal | last post by:
I'am new with javascritping not with programming. Is there nobody who can help me with ths simple promblem? :) Please, please give me a hint. Please help me with this script. I have two lists in my form. One to choose a directory and one to choose a picture. The script bellow works perfect without the directory choose function. I tried to make the directory choose function myself but I did not succeed. I think I made a mistake here:...
0
1703
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web browser that Hotmail does not support. If you continue to use your current browser software we cannot guarantee that Hotmail will work correctly for you". Please help, this is very annoying. I have been searching for help on
7
2393
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the ADT specification for a string of characters. It represents slightly more that a minimal string...
7
3300
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that I believe if it's solved, the remaining stuff is easy... my full program until now is here: http://www.geocities.com/tom4_h4wk/progmail.zip the problem is the segmentation fault when main trys to run leficheiro.c.... the *.c2 files are the...
23
3290
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
13
2151
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times will fall somewhere between a Start & End time Further more, there will be Break & Lunch times between Start & End. Example... Start 08:00 Break start 10:30
0
3057
by: 2Barter.net | last post by:
newsmail@reuters.uk.ed10.net Fwd: Money for New Orleans, AL & GA Inbox Reply Reply to all Forward Print Add 2Barter.net to Contacts list Delete this message Report phishing Show original
6
3328
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID, EmpName,DeptID,DateOfJoin, Sal, Addr) Finance (EmpID, Sal) Club (Clubname, EmpID, Fee, DateOfJoin) Leave (EmpID, Date) Department (DeptID, DeptName, NoOfEmployees)...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.