My point of origin (0,0) on my GUI is moved up by about 25/26 pixels. So the very top-left corner of my GUI seems to represent 0,25 or 0,26. I am using a double buffer to draw in things. Basically, whenever I try to draw something in my buffer at 0,0, it appears off-screen.
For example, below is a screenshot of a checkerboard pattern I attempted to generate, starting from 0,0. Although it seems fine horizontally, notice the chopped checkerboard at the top. The height is 480, which is divisible by 32 keep in mind, so the checkerboard pattern should fill in perfectly.
And here is my code (Snipped in irrelevant parts):
public class Dekari_gameGUI extends JFrame implements KeyListener {
// Resources
private final String imagePath = "Resources/Images/";
private final String spriteSheetPath = imagePath + "Sprite Sheets/";
private final String soundPath = "Resources/Sounds/";
private final String dataPath = "Resources/Data/";
private BufferedImage[][] sprites;
private Timer playerMovementTimer = new Timer();
//Game variables
private int pX = 0, pY = 0;
private short pDir = -1, pLastDir = 0, pAniStage = 0, counter = 0;
//Graphics and paint variables
private Graphics buffer;
private Image offscreen;
private Dimension dim;
//Internal variables, used for loops and placeholders
private int a, b, c, d;
/*
____ _ _
/ ___|___ _ __ ___| |_ _ __ _ _ ___| |_ ___ _ __
| | / _ | '_ / __| __| '__| | | |/ __| __/ _ | '__|
| |__| (_) | | | \__ |_| | | |_| | (__| || (_) | |
\____\___/|_| |_|___/\__|_| \__,_|\___|\__\___/|_|
*/
public Dekari_gameGUI() {
// Declaring GUI setup
setResizable(false);
setTitle("Dekari RPG Indev v1.0");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setSize(640, 480);
setBackground(Color.GREEN);
setLocationRelativeTo(null); //Centers the window
splitSpriteSheets(); //Sets up resources
dim = getSize();
offscreen = createImage(dim.width, dim.height);
buffer = offscreen.getGraphics();
*snip*
setVisible(true);
}
*snip*
/*
____ _ _
| _ __ _(_)_ __ | |_
| |_) / _` | | '_ | __|
| __/ (_| | | | | | |_
|_| \__,_|_|_| |_|\__|
*/
public void paint(Graphics g) {
//Clears the buffer
buffer.clearRect(0, 0, dim.width, dim.width);
//Drawing images to the buffer
buffer.setColor(Color.BLACK);
boolean paint = true;
//This is my checkerboard drawing function
for (a = 0; a < getWidth() / 32; a++) {
for (b = 0; b < getHeight() / 32; b++) {
if (paint) {
paint = false;
buffer.fillRect(a * 32, b * 32, 32, 32);
} else {
paint = true;
}
}
}
if (pDir == -1) //If the player is not moving
//Draws player in an idle stance facing last moved direction
buffer.drawImage(sprites[0][4 * pLastDir], pX - 24, pY - 32, this);
else //If the player is moving
//Draws player in a movement state facing the current direction
buffer.drawImage(sprites[0][pAniStage + (4 * pLastDir)], pX - 24, pY - 32, this);
//Drawing the buffer to the frame
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…