472,780 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 1280
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.