Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem with Graphics/Graphics2D  (Read 1592 times)
0 Members and 1 Guest are viewing this topic.
Offline Red Irony

Junior Newbie




$DO || ! $DO


« Posted 2010-03-24 09:26:52 »

Hey, I'm trying to make a simple program that renders a bunch of squares on a JPanel. It isn't a game in of itself, but more of a base for some game stuff I want to do. Unfortunately, I'm having some really weird graphical glitches when I run it, and I was wondering if yalls had any suggestions.

Here is the relevant code I have:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Game
{
   public static GUILayer GUI;
   public static DataLayer data;
   public static LogicLayer logic;
   
   public static void main(String[] args)
   {
      GUI = new GUILayer();
      data = new DataLayer();
      logic = new LogicLayer();
   }
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GUILayer extends JPanel
{
   public static final int TILE_WIDTH = 20;
   public static final int GAP_WIDTH = 2;
   public static final Color GAP_COLOR = new Color(61, 131, 196);
   public GUILayer()
   {
      this(800,600);
   }
   public GUILayer(int width, int height)
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(width, height);
      frame.setVisible(true);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.add(this);
   }
   
   public void draw(Graphics g) //I think this is the function that is acting up.
  {
      //Draw the tiles
     Graphics2D g2d = (Graphics2D)g.create();
      //Calculate the dimensions of the tile
     int rectWidth = TILE_WIDTH + 2 * GAP_WIDTH;
      //Render the grid of tiles
     for(int i = 0; i < Game.data.getTileArray().length; i++)
      {
         for(int q = 0; q < Game.data.getTileArray()[0].length; q++)
         {
            g2d.setColor(GAP_COLOR);
            g2d.drawRect(i * rectWidth, q * rectWidth, rectWidth, rectWidth);
            g2d.setColor(Game.data.getTileArray()[i][q].getColor());
            g2d.drawRect(i * rectWidth + GAP_WIDTH, q * rectWidth + GAP_WIDTH, TILE_WIDTH, TILE_WIDTH);
         }
      }
   }
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class LogicLayer
{  
   public LogicLayer()
   {
      //I think that this bit may be part of the problem.
     
      //Tell the GUI layer to draw the tiles.
     Game.GUI.draw(Game.GUI.getGraphics().create());
   }
}


At the moment, all this program does is create a blank screen. I'm pretty sure that the problem is either in the LogicLayer's constructor, or in the GUILayer.draw() function. At one point I was able to get the entire grid to render, and occasionally I get part of it, so I don't think that it is the code that actually does the rendering or that sets up the JFrame.

I think it could be where I create the graphics object and call draw(), but I'm not sure. Graphics/Graphics2D objects are still a bit of a mystery to me. At any rate, I'd really appreciate any advice you may have.

--Red Irony<br />...sounds like a band name.
Offline broumbroum

Junior Member





« Reply #1 - Posted 2010-03-24 13:12:17 »

1. Graphics2D g2 = (Graphics2D)g; is sufficient, a graphic2d context has not to be instanciated everytime in that way, java will decide.
2. JPanel are JComponent, and therefore will require to be painted alike. draw() must be implemented in the protected void paintComponent(Graphics) function while the LogicLayer will call either repaint() (for passive rendering) or Jpanel.update(Jpanel.getGraphics()) for active rendering.
This is because Swing is required to be involved for repainting for the Java2D overlay gets lost with peer sync and edthread.

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline DzzD
« Reply #2 - Posted 2010-03-24 21:04:03 »

Quote
Graphics/Graphics2D objects are still a bit of a mystery to me.
Graphics is simply an object that give you a cupple of methods to perform drawing/painting witout knowing the exact underlying target format.

Graphics2D extends Graphics

AWT/Swing methode often return Graphics rather than Graphics2D object (for some old internals compatibilities reasons) but it will be often a Graphics2D instance that's why you can safely do things like :
1  
Graphics2d g2=(Graphics2D)panel.getGraphics();


also be care to not repaint everything : every Graphics object have a clip area associated to it g.getClip(), you must get it to know what area you must repaint (this will be more efficient to only repaint the clip area because everything outer will have no effect and will be discard / not draw by the graphic object =>  you may have compute & draw the whole Graphics for nothing

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Red Irony

Junior Newbie




$DO || ! $DO


« Reply #3 - Posted 2010-03-24 21:10:46 »

@broumbroum: Thanks! It works like a charm now.

@DzzD: You had me until the part about clips. I'm not exactly sure what you mean there. I've only really got one Graphics2D object, to the best of my understanding, so why/how would it have multiple clips?

One of the things that has kind of puzzled me is why I am not allowed to declare Graphics objects in the same way I declare normal objects (i.e. Graphics g = new Graphics()Wink.

--Red Irony<br />...sounds like a band name.
Offline DzzD
« Reply #4 - Posted 2010-03-24 21:46:31 »

@broumbroum: Thanks! It works like a charm now.

@DzzD: You had me until the part about clips. I'm not exactly sure what you mean there. I've only really got one Graphics2D object, to the best of my understanding, so why/how would it have multiple clips?

One of the things that has kind of puzzled me is why I am not allowed to declare Graphics objects in the same way I declare normal objects (i.e. Graphics g = new Graphics()Wink.

for example if a window of antoher software go over your frame than a repaint will be launch by the "system" (AWT/Swing) and the frame paint method like paint(Graphics g) (or anyother system called paint method) may be called with a clipped graphics because not the whole graphics have been occluded(in this case the clip area will be a rectangle where the window is over you frame ).

you can cannot create a graphics object cause it is an abstract object : a graphics object have no sense until it is linked to something where it can paint that's why you cannot instanciate it with "new Graphics" :

often you will ask an object to give you a graphics object (to be able to paint to that object) :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
BufferedImage  bi=new BufferedImage(...)
//If I want to paint on this buffered image I can do the following
Graphics gb=bi.getGraphics();

//or another example

Frame f;
//than later in my code when the frame is visible if I want to paint directyly on it
Graphics g=f.getGraphics()

//or you can also create your own graphics to paint you own object in the way you want....
class MyGraphics extends Graphics
{
   //here implements all original Graphics methods
 
   //for example you may do (to paint to the console)
  public void drawString(String str,int x,int y)
   {
      System.out.println(str +" at " + x + "," + y);
   }
   
   //or to paint to a remote computer
  public void drawString(String str,int x,int y)
   {
      network.sendDrawStringOrderToRemoteComputer(str,x,y);
   }
}


EDIT: typos.... you cannot create a graphics object cause it is an abstract .....

Offline DzzD
« Reply #5 - Posted 2010-03-25 00:31:41 »

and in shorter Smiley : you should think of Graphics as a tools/way to draw/paint on an object and not as the recipient to receive draw/paint

Offline broumbroum

Junior Member





« Reply #6 - Posted 2010-03-27 23:10:30 »

for example if a window of antoher software go over your frame than a repaint will be launch by the "system" (AWT/Swing) and the frame paint method like paint(Graphics g) (or anyother system called paint method) may be called with a clipped graphics because not the whole graphics have been occluded(in this case the clip area will be a rectangle where the window is over you frame ).

such clipping area is returned by the getVisibleRect() of JComponent. However, it won't be usable in an active rendering mode.  the clipping area will be useful when using the component in a Swing managed context, e.g. you paint a custom an animated component on a dektop panel.
In that particular case, update(getGrahics()) may be replaced by a call to paintImmediately(getVisiblerect) which will repaint on the visible graphics clipped area. This can save memory when you are displaying several panels.

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (33 views)
2014-07-14 16:23:47

Riven (34 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!