-
1 Attachment(s)
Can you tell why the graphics flickers even with BufferedImage implemented in my code
Greetings EveryOne :(grin):
Thanks to Norms help I was able to make the first step to make my board game in java.
This is my code
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
public class ClickToDrawPeiceInsideCell extends Applet
implements MouseListener
{
private int MouseX;
private int MouseY;
private boolean ImageBeenLoaded = false;
Image ImgBuffer;
Image Smily, BoardImg;
Graphics2D Graph;
DrawGrid Grid = new DrawGrid();
BufferedImage BuffImg = new BufferedImage(Grid.GetBoardWidth(),
Grid.GetBoardHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D GridGraph = BuffImg.createGraphics();
@Override
public void init()
{
setSize(Grid.GetWinWidth(), Grid.GetWinHeight());
Smily = getImage(getDocumentBase(), "Smily.jpg");
BoardImg = getImage(getDocumentBase(), "ChessBoard.jpg");
addMouseListener(this);
}
@Override
public void paint(Graphics G)
{
int GridSize = Grid.GetGridSize();
if(!ImageBeenLoaded)
{
if(GridGraph.drawImage(BoardImg, 0, 0, this))
{
GridGraph.drawImage(Smily, -50, -50, this);
ImageBeenLoaded = true;
}
}
G.drawImage(BuffImg, GridSize, GridSize, this);
}
@Override
public void mouseClicked(MouseEvent ME)
{
MouseX = ME.getX();
MouseY = ME.getY();
Grid.ClickToDraw(GridGraph, Smily, BoardImg, MouseY, MouseX);
repaint();
}
@Override
public void mousePressed(MouseEvent ME) {}
@Override
public void mouseReleased(MouseEvent ME) {}
@Override
public void mouseEntered(MouseEvent ME) {}
@Override
public void mouseExited(MouseEvent ME) {}
}
Code:
import java.applet.*;
import java.awt.*;
public class DrawGrid extends Applet
{
private final int GridSize = 58;
private final int GridRows = 8;
private final int GridColumns = 8;
private int BoardWidth = GridColumns * GridSize;
private int BoardHeight = GridRows * GridSize;
private int WinWidth = BoardWidth+(GridSize*2);
private int WinHeight = BoardHeight+(GridSize*2);
private int Coord_Data[] = new int[9];
private int PeiceCoordX[][] = new int[9][9];
private int PeiceCoordY[][] = new int[9][9];
Image ImgBuffer;
Image Smily;
Graphics2D Graph;
public DrawGrid()
{
SetUpGameCoord();
}
private void SetUpGameCoord()
{
int PeiceOffsetCoord = 4;
Coord_Data[1] = PeiceOffsetCoord;
for(int i=2; i<=8; i++)
{
Coord_Data[i] = Coord_Data[i-1]+GridSize;
}
for(int i=1; i<=8; i++)
{
for(int j=1; j<=8; j++)
{
PeiceCoordX[j][i] = Coord_Data[i];
PeiceCoordY[j][i] = Coord_Data[j];
}
}
}
public int GetWinWidth()
{
return WinWidth;
}
public int GetWinHeight()
{
return WinHeight;
}
public int GetGridSize()
{
return GridSize;
}
public int GetBoardWidth()
{
return BoardWidth;
}
public int GetBoardHeight()
{
return BoardHeight;
}
public int GetPeiceX(int y, int x)
{
return PeiceCoordX[y][x];
}
public int GetPeiceY(int y, int x)
{
return PeiceCoordY[y][x];
}
public void ClickToDraw(Graphics2D Graph, Image PeiceImg,
Image BoardImg, int y, int x)
{
y = (int)(y/GridSize);
x = (int)(x/GridSize);
if(y<=8 && x<=8 &&
y>=1 && x>=1 )
{
Graph.drawImage(BoardImg, 0, 0, this);
Graph.drawImage(PeiceImg, GetPeiceX(y, x), GetPeiceY(y, x), this);
}
}
}
The Game Images: Attachment 2060
The problem is the graphics flickers when ever you click to move the Smily face, even with BufferedImage, I might guess BufferedImage is not well implemented.
Can You Point the problem Please. Thank You In Andvance :(grin):
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
You have been asked to follow coding conventions. Why should members here struggle with your variable and method names that start with an uppercase letter? That's not what we're used to seeing.
Why does your DrawGrid class extend Applet? It certainly isn't being used as an Applet. And why does it have an instance field Graph which is never used (nor needed)?
db
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Why does your DrawGrid class extend Applet? It certainly isn't being used as an Applet. And why does it have an instance field Graph which is never used (nor needed)?
They was there before and I forgot to omit them. I can't edit the first post so I can't take them off.
Quote:
You have been asked to follow coding conventions. Why should members here struggle with your variable and method names that start with an uppercase letter? That's not what we're used to seeing.
Sorry if you don't like my coding style, you are right the convention states something else but I like coding this way so I can take a part between native variables and methods and those I coded.
If you like to help I will appreciate it, but if you don't like to I thank you either ways.
Thank You For Your Understanding.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Not following the naming conventions makes your code hard to read.
How can a new reader of your code tell if a symbol/word is a class name or a variable name?
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Sorry if you don't like my coding style, you are right the convention states something else but I like coding this way so I can take a part between native variables and methods and those I coded.
Good to know that you know more about coding standards than all the text books, tutorials and other professional programmer around the world?
Since you know more than we do I guess there is no need to help since you will be able to solve the problem on your own. Good luck.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
About extending the Applet class, it's needed to implement the drawImage() method.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
About extending the Applet, it's needed to implement the drawImage() method.
No it isn't. That is not how you do custom painting.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Good to know that you know more about coding standards than all the text books, tutorials and other professional programmer around the world?
Since you know more than we do I guess there is no need to help since you will be able to solve the problem on your own. Good luck.
Please don't be mean, I am here to learn wither you like it or not, read my signature before accusing me with such a thing.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
I am not asking you to code it for me, what i need is guidance so i can learn
You where given guidance which you promply ignored.
I would suggest you start with the Swing tutorial on: http://docs.oracle.com/javase/tutori...ts/applet.html
The table of contents also has a section on how to do Custom Painting.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
You where given guidance which you promply ignored.
Think of it as you like but I am not, if you don't want or like to help me please just ignore me and ignore my post, Thank You.
Thank You, I will do.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Not following the naming conventions makes your code hard to read.
How can a new reader of your code tell if a symbol/word is a class name or a variable name?
Sir Norm, you are right on that, maybe the IDE can help.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Try converting the code to use Swing classes and methods. The flicker goes away when you do that.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Try converting the code to use Swing classes and methods. The flicker goes away when you do that.
Thank you Sir Norm, I am gonna work on that and report back, You are a real mentor Bless You.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Originally Posted by
Shikatsu
They was there before and I forgot to omit them. I can't edit the first post so I can't take them off.
Yes should be able to edit the first post.
Quote:
Sorry if you don't like my coding style, you are right the convention states something else but I like coding this way so I can take a part between native variables and methods and those I coded.
It has nothing to do with not "liking" and all to do with not understanding what you've written. If you want our help, we would appreciate it if you took the extra effort to make your code understandable by others.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Originally Posted by Fubarable
Yes should be able to edit the first post.
I can't see the edit link next to reply & reply with quote!
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
Quote:
Originally Posted by
Shikatsu
I can't see the edit link next to reply & reply with quote!
Hm, perhaps this isn't available until you've been here longer. Sorry, and I retract that part of my comment.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
After following Mr. Norm's advice, I had managed to convert my code to use Swing classes and methods.
Here is my code:
Code:
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ClickToDrawPeiceInsideCell extends JApplet
{
private Image vBoardImg, vSmily;
public static void main(String args[]) throws IOException
{
JApplet MyJApplet = new ClickToDrawPeiceInsideCell();
JFrame MyJFrame = new JFrame();
MyJFrame.setContentPane(MyJApplet);
MyJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyJFrame.setTitle("ClickToDrawPeiceInsideCell");
MyJFrame.pack();
//MyJFrame.setResizable(false);
MyJFrame.setVisible(true);
}
public ClickToDrawPeiceInsideCell() throws IOException
{
File vChessBoardImgFile = new File("Image Path/ChessBoard.jpg");
File vBlackSmilyImgFile = new File("Image Path/Smily.jpg");
vBoardImg = ImageIO.read(vChessBoardImgFile);
vSmily = ImageIO.read(vBlackSmilyImgFile);
DrawGrid Grid = new DrawGrid(vBoardImg, vSmily);
add(Grid);
}
}
Code:
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class DrawGrid extends JPanel
{
private final int vGridSize = 58;
private final int vGridRows = 8;
private final int vGridColumns = 8;
private int vBoardWidth = vGridColumns * vGridSize;
private int vBoardHeight = vGridRows * vGridSize;
private int vWinWidth = vBoardWidth+(vGridSize*2);
private int vWinHeight = vBoardHeight+(vGridSize*2);
private boolean vImageBeenLoaded = false;
private BufferedImage vBuffImg = null;
private Image vBoardImg, vSmily;
public DrawGrid(Image vBoardImg, Image vSmily)
{
this.vBoardImg = vBoardImg;
this.vSmily = vSmily;
setPreferredSize(new Dimension(vWinWidth, vWinHeight));
setBackground(Color.white);
repaint();
}
@Override
public void paintComponent(Graphics vG)
{
Graphics2D vGraph2D = (Graphics2D)vG;
super.paintComponent(vGraph2D);
if(vBuffImg == null)
{
vBuffImg = new BufferedImage(vBoardWidth, vBoardHeight,
BufferedImage.TYPE_INT_RGB);
int vG_Width = getSize().width;
int vG_Height = getSize().height;
vBuffImg = (BufferedImage)this.createImage(vG_Width, vG_Height);
vGraph2D = vBuffImg.createGraphics();
}
if(!vImageBeenLoaded)
{
if(vGraph2D.drawImage(vBoardImg, 0, 0, this))
{
vGraph2D.drawImage(vSmily, 0, 0, this);
vImageBeenLoaded = true;
}
}
vGraph2D.drawImage(vBuffImg, vGridSize, vGridSize, this);
}
}
The problem is nothing showing up when the JApplet is run, when you minimize the window and get it back it stills the same nothing shows up, only when you maximize the window the images shows up but when they do they show up multiple times one on top of the other from upper left to down right.
Can You Help Me With This, Thank You In Advance :D:
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
There is a contradiction in the ClickToDrawPeiceInsideCell class.
It extends JApplet which implies that it is an applet that runs from an html page in a browser.
And it has a main() method which is used as a starting point for the java command.
Is it an applet or an application?
How is it executed?
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
It's a JApplet but it's run as an application maybe it's a contradiction as you said, I am not sure about that.
I took this approach working on this Example:
Java: Example - PaintDemo
Thank You For You Quick Reply Mr. Norm.
-
Re: Can you tell why the graphics flickers even with BufferedImage implemented in my
I tried to get it work using init() method, but I couldn't find any helpful example.