473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Please Help! Changing a graphic via a menu with listeners

10
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 1317
BigDaddyLH
1,216 Expert 1GB
Call setIcon(yourNewIconImage) 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
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,...
2
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...
3
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...
0
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...
7
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...
7
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...
23
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...
13
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...
0
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
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.