473,804 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

glassPane update problem

3 New Member
Hi folks,
I'm trying to make a small game about being a drug dealer, with a 10x8 grid JPanel. On top of that i have another 10x8 grid JPanel as glassPane for the player icon.

I have all the properties of the player in a player object class with getter and setter methods for fx his X and Y position in the grid.
When i set up his X and Y position myself everything works fine and he is painted the right place.
My problem is when my keyPressed method updates his X,Y position and repaint the glass pane everything goes wrong. At first key press (no matter which) he disappers, and i have no ideer why.

Something about the repainting of the glassPane goes wrong, but what?

Any help or hint would be greatly appreciated.

Here is my display code:
Expand|Select|Wrap|Line Numbers
  1. package org.DealOrDie;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JMenu;
  13. import javax.swing.JMenuBar;
  14. import javax.swing.JMenuItem;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17.  
  18. public class GUI extends Main implements ActionListener, KeyListener {
  19.  
  20.     //import Player object
  21.     Player player = new Player();
  22.  
  23.     //description of start city
  24.     private final String[][] startCity = { 
  25.             {"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
  26.             {"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
  27.             {"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
  28.             {"grs", "vej", "hus", "grs", "hus", "grs", "grs", "grs", "grs", "grs"},
  29.             {"grs", "vej", "vej", "vej", "vej", "vej", "hus", "hus", "grs", "grs"},
  30.             {"grs", "grs", "vej", "grs", "grs", "vej", "vej", "hus", "grs", "grs"},
  31.             {"grs", "grs", "vej", "grs", "grs", "vej", "hus", "hus", "grs", "grs"},
  32.             {"grs", "grs", "hus", "grs", "grs", "vej", "grs", "grs", "grs", "grs"} };
  33.  
  34.     //city now
  35.     private final String[][] city = { 
  36.             {"grs", "vej", "grs", "grs", "grs", "grs", "grs", "grs", "grs", "grs"},
  37.             {"grs", "vej", "grs", "grs", "grs", "grs", "hus", "hus", "hus", "grs"},
  38.             {"grs", "vej", "grs", "grs", "grs", "vej", "vej", "vej", "vej", "grs"},
  39.             {"grs", "vej", "hus", "grs", "hus", "vej", "grs", "grs", "grs", "grs"},
  40.             {"grs", "vej", "vej", "vej", "vej", "vej", "hus", "hus", "grs", "grs"},
  41.             {"grs", "grs", "vej", "grs", "grs", "vej", "vej", "hus", "grs", "grs"},
  42.             {"grs", "grs", "vej", "grs", "grs", "vej", "hus", "hus", "grs", "grs"},
  43.             {"grs", "grs", "hus", "grs", "grs", "vej", "grs", "grs", "grs", "grs"} };
  44.  
  45.     private JFrame frame;
  46.     private JPanel cityPanel;
  47.     private JPanel playerPane;
  48.     private JLabel icon;
  49.  
  50.     public GUI() {
  51.         player.reset();
  52.  
  53.         frame = new JFrame();
  54.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.         frame.setTitle("Deal Or Die");
  56.         frame.setLocation(200, 50);
  57.         frame.setSize(640, 512);
  58.         frame.setResizable(false);
  59.         frame.addKeyListener(this);
  60.  
  61.         setupMenuBar();
  62.         paintCity();
  63.         paintPlayerPane();
  64.         //paintStatusBar();
  65.  
  66.         frame.pack();
  67.         frame.setVisible(true);
  68.     }
  69.  
  70.     private void setupMenuBar() {
  71.         JMenuBar menuBar = new JMenuBar();
  72.         JMenu menu = new JMenu("File");
  73.         JMenuItem menuItem = new JMenuItem("New");
  74.         menuItem.setActionCommand("New");
  75.         menuItem.addActionListener(this);
  76.         menu.add(menuItem);
  77.         menuItem = new JMenuItem("Load");
  78.         menuItem.setActionCommand("Load");
  79.         menuItem.addActionListener(this);
  80.         menu.add(menuItem);
  81.         menuItem = new JMenuItem("Save");
  82.         menuItem.setActionCommand("Save");
  83.         menuItem.addActionListener(this);
  84.         menu.add(menuItem);
  85.         menuItem = new JMenuItem("Exit");
  86.         menuItem.setActionCommand("Exit");
  87.         menuItem.addActionListener(this);
  88.         menu.add(menuItem);
  89.         menuBar.add(menu);
  90.         menu = new JMenu("Help");
  91.         menuItem = new JMenuItem("Help");
  92.         menuItem.setActionCommand("Help");
  93.         menuItem.addActionListener(this);
  94.         menu.add(menuItem);
  95.         menuItem = new JMenuItem("About");
  96.         menuItem.setActionCommand("About");
  97.         menuItem.addActionListener(this);
  98.         menu.add(menuItem);
  99.         menuBar.add(menu);
  100.         frame.setJMenuBar(menuBar);
  101.  
  102.     }
  103.  
  104.     private void paintCity() {
  105.         //sets up a city map of 8,10 squares. Painted according to the city object
  106.         cityPanel = new JPanel();
  107.         cityPanel.setLayout(new GridLayout(8,10));
  108.         frame.add(cityPanel, BorderLayout.CENTER);
  109.         String citymap[][] = getCity();
  110.         for (int x = 0; x < 8; x++) {
  111.             for (int y = 0; y < 10; y++) {
  112.                 ImageIcon iconX = null;
  113.                 if (citymap[x][y].equals("grs")) { iconX = createImageIcon("images/grs.JPG"); } 
  114.                 if (citymap[x][y].equals("vej")) { iconX = createImageIcon("images/vej.JPG"); }
  115.                 if (citymap[x][y].equals("hus")) { iconX = createImageIcon("images/hus.JPG"); }
  116.                 icon = new JLabel();
  117.                 icon.setIcon(iconX);
  118.                 cityPanel.add(icon);
  119.             }
  120.         }        
  121.     }
  122.  
  123.     private void paintPlayerPane() {
  124.         //setup a JPanel and use it as glasspane on top of city map
  125.  
  126.         playerPane = new JPanel();
  127.         playerPane.setLayout(new GridLayout(8,10));
  128.         ImageIcon icon_player = createImageIcon("images/player.gif");
  129.  
  130.         for (int y = 0; y <= 7; y++) { //paint grid with empty labels except at player position 
  131.             for (int x = 0; x <= 9; x++) {
  132.             if ((player.getPositionX() == x) && (player.getPositionY() == y)) {
  133.                     JLabel label_player = new JLabel();
  134.                     label_player.setSize(64, 64);
  135.                     label_player.setIcon(icon_player);
  136.                     playerPane.add(label_player);
  137.                 }else {
  138.                     JLabel label_player = new JLabel();
  139.                     label_player.setSize(64, 64);
  140.                     label_player.setIcon(null);
  141.                     playerPane.add(label_player);
  142.                 }
  143.             }
  144.  
  145.         }
  146.         frame.setGlassPane(playerPane);
  147.         playerPane.setOpaque(false);
  148.         playerPane.setVisible(true);
  149.  
  150.     }
  151.  
  152.     private String[][] getCity() {
  153.         return city;
  154.     }
  155.  
  156.     private void paintStatusBar() {
  157.         // TODO Auto-generated method stub
  158.  
  159.     }
  160.  
  161.     protected ImageIcon createImageIcon(String path) {
  162.         java.net.URL imgURL = getClass().getResource(path);
  163.         if (imgURL != null) {
  164.             return new ImageIcon(imgURL);
  165.         } else {
  166.             System.err.println("Couldn't find file: " + path);
  167.             return null;
  168.         }
  169.     }
  170.  
  171.     public void actionPerformed(ActionEvent e) {
  172.         if (e.getActionCommand().equals("Save")) {
  173.  
  174.         }
  175.         else if (e.getActionCommand().equals("Load")) {
  176.  
  177.         }
  178.         else if (e.getActionCommand().equals("Exit")) {
  179.             System.exit(0);
  180.         }
  181.         else if (e.getActionCommand().equals("Help")) {
  182.             JOptionPane.showMessageDialog(frame, "");
  183.         }
  184.         else if (e.getActionCommand().equals("About")) {
  185.             JOptionPane.showMessageDialog(frame, "Deal or Die beta 0.1 ©2007\n\nMade by Svante Joergensen");
  186.         }
  187.         else if (e.getActionCommand().equals("New")) {
  188.  
  189.         }
  190.     }
  191.  
  192.     public void keyPressed(KeyEvent e) {
  193.         if (e.getKeyCode() == 37) { //if left arrow is being pressed
  194.             if ((player.getPositionX() - 1) >= 0) { //if left of player is within map limits
  195.                 player.setPositionX((player.getPositionX() - 1)); //move player 1 to the left
  196.             }
  197.         }
  198.         if (e.getKeyCode() == 38) { //if up arrow is being pressed
  199.             if ((player.getPositionY() - 1) >= 0) { //if above player is within map limits
  200.                 player.setPositionY((player.getPositionY() - 1)); //move player 1 up
  201.             }
  202.         }
  203.         if (e.getKeyCode() == 39) { //if right arrow is being pressed
  204.             if ((player.getPositionX() + 1) <= 9) { //if right of player is within map limits
  205.                 player.setPositionX((player.getPositionX() + 1)); //move player 1 to the right
  206.                 System.out.println("right!!!");
  207.             }
  208.         }
  209.         if (e.getKeyCode() == 40) { //if down arrow is being pressed
  210.             if ((player.getPositionY() + 1) <= 7) { //if under player is within map limits
  211.                 player.setPositionY((player.getPositionY() + 1)); //move player 1 down
  212.             }
  213.         }
  214.         JPanel empty = new JPanel();
  215.         frame.setGlassPane(empty);
  216.         paintPlayerPane();
  217.         frame.repaint();
  218.     }
  219.  
  220.     @Override
  221.     public void keyReleased(KeyEvent arg0) {
  222.         // TODO Auto-generated method stub
  223.  
  224.     }
  225.  
  226.     @Override
  227.     public void keyTyped(KeyEvent arg0) {
  228.         // TODO Auto-generated method stub
  229.  
  230.     }
  231. }
  232.  
And my player object class:
Expand|Select|Wrap|Line Numbers
  1. package org.DealOrDie;
  2.  
  3. public class Player {
  4.  
  5.     private String name;
  6.     private int positionX;
  7.     private int positionY;
  8.     private float cash;
  9.     private int heroin;
  10.  
  11.     public String getName() {
  12.         return name;
  13.     }
  14.     public void setName(String name) {
  15.         this.name = name;
  16.     }
  17.  
  18.     public int getPositionX() {
  19.         return positionX;
  20.     }
  21.     public void setPositionX(int positionX) {
  22.         this.positionX = positionX;
  23.         System.out.println("player position is now: (" + this.positionX + "," + this.positionY + ")");
  24.     }
  25.     public int getPositionY() {
  26.         return positionY;
  27.     }
  28.     public void setPositionY(int positionY) {
  29.         this.positionY = positionY;
  30.         System.out.println("player position is now: (" + this.positionX + "," + this.positionY + ")");
  31.     }
  32.     public float getCash() {
  33.         return cash;
  34.     }
  35.     public void setCash(float cash) {
  36.         this.cash = cash;
  37.     }
  38.     public int getHeroin() {
  39.         return heroin;
  40.     }
  41.     public void setHeroin(int heroin) {
  42.         this.heroin = heroin;
  43.     }
  44.     public void reset() {
  45.         name = "player";
  46.         positionX = 5;
  47.         positionY = 5;
  48.         cash = 250;
  49.         heroin = 0;
  50.     }
  51.  
  52. }
  53.  
Dec 13 '07 #1
1 2315
BigDaddyLH
1,216 Recognized Expert Top Contributor
Am I missing the method slapThatHo()?
Dec 13 '07 #2

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

Similar topics

6
7642
by: al | last post by:
Greetings, In the customers table in Northwind db, one can update PK (customerid) and all other fields in the same table. My question is how can you do this in the udpate stat. That is, if one wants to write update query to update all fields including PK, how it can be set? Using PK in the SET statement, gives an error, because this field might have changed during the update? MTIA,
1
6176
by: Gent | last post by:
am using FOR UPDATE triggers to audit a table that has 67 fields. My problem is that this slows down the system significantly. I have narrowed down the problem to the size (Lines of code) that need to be compiled after the trigger has been fired. There is about 67 IF Update(fieldName) inside the trigger and a not very complex select statement inside the if followed by an insert to the audit table. When I leave only a few IF-s in the...
2
5268
by: Niyazi | last post by:
Hi, I have not understand the problem. Before all the coding with few application everything worked perfectly. Now I am developing Cheque Writing application and when the cheque is clear the user have to open a form and entera date so we know in report that the desiered check has been cleared. It takes me while to wrtie. But when I try to update the datagrid changes via dataset to MS Access 2003 I get an error that simply says...
8
3728
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: "Date","P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11","P12","P13","P14","P15","P16","P17","P18","P19","P20","P21" 1/1/2005,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 1/2/2005,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
13
7676
by: abdoly | last post by:
i wrote a code to update datagrid with the datagrid updatecommand but i cant get the updated values after being update that is the code private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) TextBox temp temp=(TextBox)e.Item.Cells.Controls String str=temp.Text; str always returnt the old value of the cell (before being updated) would u plz tell me about whats wrong i m doin
8
2700
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. Example: I have a dataadapter that contains one table with one row. I change the value of the 'FisrtName' column in that row from Jack to John. I call ..update on the dataadapter it goes through fine. Now I change that same column in that same row...
1
2538
by: cindy | last post by:
this is the call private void Page_Load(object sender, System.EventArgs e) { OdbcConnection connection = new OdbcConnection ("DSN=PFW52"); CreateDataAdapter(connection); } this is the code, no errors, but NO UPDATE I have to use ODBC I just need to update a field based on a key, EMBARASSED to say days going
5
3063
by: =?Utf-8?B?UlBhcmtlcg==?= | last post by:
I used the wizard to generate a typed dataset for my table and let it create my SPROCs. It created everything, and the GetData() method and the custom GetByUserName query works great, but when I try to call the Update() method of my TableAdapter, I get the following: "Procedure 'stp_SecurityUsers_Update' expects parameter '@userName', which was not supplied."
11
6071
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know if that's what's causing the behavior or not. It seems like the Update button should at least do something. When the Edit button is clicked, the grid goes into Edit mode and the Cancel button takes the grid out of Edit mode. So, I don't get what...
0
9706
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
9577
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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
10315
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
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
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...
1
4295
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
2990
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.