Jump to content
  • Advertisement
Sign in to follow this  
CppMaster

Problem with GUI in java

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, tho I'm a java newbie, I'm trying to create a game with my friend. The game is a 2D RPG. We are currently working on an extern application that will help us create the maps (the application is called MapMaker). The problem is that for some unknown reason, the GUI does not work properly. When the mouse is moved over the component made to display the map (instance of class MapDisplay, which extends JPanel), a green rectangle should move with the mouse. The screen, apart from the cursor, which is green, should be black for a new map. Instead, the screen flashes black and then becomes gray. When I move the mouse over it, the GUI it-self is displayed inside the component and sometimes, random components appear (ex: JScrollBars). What could be causing that (see code below)? What should I do? mport java.io.*; import java.awt.*; import javax.swing.*; import java.awt.geom.*; import javax.imageio.*; import java.awt.image.*; public class MapDisplay extends JPanel { //... private void completePaint(Graphics g){ Graphics2D gfx = (Graphics2D)g; BufferedImage disp; Map currentMap = MapMaker.getCurrentMap();//returns a map object if(MapMaker.getLayerMode()== 4){ disp = currentMap.render(MapMaker.getInvisibleColor());//returns the final map disp = addEventIcon(disp); } else disp = currentMap.getData(MapMaker.getLayerMode());//returns a "layer" of the map gfx.drawImage(disp, null, MapDisplay.x, MapDisplay.y); //draw cursor Color current = gfx.getColor(); gfx.setColor(new Color(84,255,3)); /*The cursor rectangle is defined inside MapDisplay, it has a width and a height of 16. It's coordinates depend on the mouse position*/ gfx.draw(MapMaker.getCursor()); gfx.setColor(current); } private void drawCursor(Graphics g){ Graphics2D gfx = (Graphics2D)g; int[] pos = MapMaker.getOldMapCursorCoord();//returns the old coordinates of the cursor pos[0] *= 16; pos[1] *= 16; MapMaker.setRegion(new Rectangle(pos[0]-10, pos[1]-10, pos[0]+10, pos[1]+10)); this.paintRegion(g); Color current = gfx.getColor(); gfx.setColor(new Color(84,255,3)); gfx.draw(MapMaker.getCursor());//Rectangle gfx.setColor(current); } private void paintRegion(Graphics g){ BufferedImage pic; Rectangle r = MapMaker.getRegion(); if(MapMaker.getLayerMode() == 4){ pic = addEventIcon(MapMaker.getCurrentMap().render(MapMaker.getInvisibleColor())); } else { pic = MapMaker.getCurrentMap().getData(MapMaker.getLayerMode()); } //Most of the actual code is to make sure that it doesn't draw oustide the Raster int pos[] = new int[4]; pos[0] = r.x-10; pos[1] = r.y-10; pos[2] = r.width + 10; pos[3] = r.height + 10; Rectangle r2 = MapMaker.getView(); int minX = r2.x; int minY = r2.y; int maxX = r2.x + r2.width; int maxY = r2.y + r2.height; if(minX > pos[0]) pos[0] = minX; if(minY > pos[1]) pos[1] = minY; if(maxX < (pos[0] + pos[2])) pos[2] = maxX; if(maxY < (pos[1] + pos[3])) pos[3] = maxY; pic = pic.getSubimage(pos[0], pos[1], pos[2], pos[3]); Graphics2D gfx = (Graphics2D)this.getGraphics(); gfx.drawImage(pic, null, pos[0], pos[1]); } public void paintComponent(Graphics g){ int paintMode = MapMaker.getPaintMode(); if(paintMode == MapMaker.NORMAL){//This is the default draw this.drawCursor(g); } else if(paintMode == MapMaker.COMPLETE){//drawn only a few times (once at the beginning and every time the window is activated) this.completePaint(g); } else if(paintMode == MapMaker.REGION){ this.paintRegion(g); } else if(paintMode == MapMaker.CURSOR){ this.drawCursor(g); } MapMaker.resetPaintMode(); } } Thank you. P.S.: If needed, I will post the whold code.

Share this post


Link to post
Share on other sites
Advertisement
Maybe you should ask a moderator to move this to the Java section. It seems that you are not quite the newbie that you think you are. You should, however, use the source=java tag to retain the indention in your code.

Share this post


Link to post
Share on other sites
I'm doing something similar, and yes swing is a pain in the ass. However, I might have found the problem. It looks like you are overriding the public void paintComponent(Graphics g) method to do your custom painting, right? From what I have learnt you need to override the public void paint(Graphics g) method instead. This is the method which is called whenever swing schedules the component for updating. Another thing which you might find useful, if you perform an action and expect the component to update but it doesn't, you might need to call the repaint() method of that component.

Something else I've noticed, you seem to be using the Graphics which has been passed in for the first two methods, but you are getting the Graphics from the JPanel in the third one. This may or may not be causing you problems

I'm sure that there must be some proper way of coding in swing but everything I do seems to be messing around with different bits of code until it works. You just have to fiddle around with it :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Vyper_uk
I'm doing something similar, and yes swing is a pain in the ass. However, I might have found the problem. It looks like you are overriding the public void paintComponent(Graphics g) method to do your custom painting, right? From what I have learnt you need to override the public void paint(Graphics g) method instead. This is the method which is called whenever swing schedules the component for updating. Another thing which you might find useful, if you perform an action and expect the component to update but it doesn't, you might need to call the repaint() method of that component.

No no, do not override paint(Graphics). It is paintComponent(Graphics) you should override.

Share this post


Link to post
Share on other sites
Quote:
Original post by rohde
No no, do not override paint(Graphics). It is paintComponent(Graphics) you should override.

Really? Its been working ok so far for me. Guess I'll change my code then before it starts giving me trouble later, thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Vyper_uk
Quote:
Original post by rohde
No no, do not override paint(Graphics). It is paintComponent(Graphics) you should override.

Really? Its been working ok so far for me. Guess I'll change my code then before it starts giving me trouble later, thanks


In AWT you would override paint(Graphics). In Swing, just as in AWT, paint(Graphics) gets called when it's time to render. But in Swing paint(Graphics) calls paintComponent(Graphics), paintBorder(Graphics) and paintChildren(Graphics), in that order, to draw the component.

This means that in Swing you should override paintComponent(Graphics) and not paint(Graphics). And remember to call super.paintComponent(Graphics) in you custom paintComponent(Graphics) to ensure that the component is properly drawn.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!