I using Slick2D and LWJGL and i want to ask one question how to change game icon in the taskbar when i run it.
Actually depending on what you precisely want to do it can be quite hard.
The easyest way is to use Slick2d method: In GameContainer you can use the setIcon(String) method.
But on one of my personal project I had some difficulties loading images with Slick at start. So I looked for another solution with LWJGL :
public static void main(String[] args) {
AppGameContainer app;
try {
app = new AppGameContainer(new Main("Anode"));
org.lwjgl.opengl.Display.setIcon(loadIcon("resources/images/logo.png", app));
}
public static ByteBuffer[] loadIcon(String filepath,AppGameContainer app)
{
BufferedImage image = null;
try
{
image = ImageIO.read(app.getClass().getClassLoader().getResource(filepath));
}
catch (IOException e)
{
e.printStackTrace();
}
ByteBuffer[] buffers = new ByteBuffer[3];
buffers[0] = loadIconInstance(image, 128);
buffers[1] = loadIconInstance(image, 32);
buffers[2] = loadIconInstance(image, 16);
return buffers;
}
private static ByteBuffer loadIconInstance(BufferedImage image, int dimension)
{
BufferedImage scaledIcon = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = scaledIcon.createGraphics();
double ratio = 1;
if(image.getWidth() > scaledIcon.getWidth())
{
ratio = (double) (scaledIcon.getWidth()) / image.getWidth();
}
else
{
ratio = (int) (scaledIcon.getWidth() / image.getWidth());
}
if(image.getHeight() > scaledIcon.getHeight())
{
double r2 = (double) (scaledIcon.getHeight()) / image.getHeight();
if(r2 < ratio)
{
ratio = r2;
}
}
else
{
double r2 = (int) (scaledIcon.getHeight() / image.getHeight());
if(r2 < ratio)
{
ratio = r2;
}
}
double width = image.getWidth() * ratio;
double height = image.getHeight() * ratio;
g.drawImage(image, (int) ((scaledIcon.getWidth() - width) / 2), (int) ((scaledIcon.getHeight() - height) / 2),
(int) (width), (int) (height), null);
g.dispose();
byte[] imageBuffer = new byte[dimension*dimension*4];
int counter = 0;
for(int i = 0; i < dimension; i++)
{
for(int j = 0; j < dimension; j++)
{
int colorSpace = scaledIcon.getRGB(j, i);
imageBuffer[counter + 0] =(byte)((colorSpace << 8) >> 24 );
imageBuffer[counter + 1] =(byte)((colorSpace << 16) >> 24 );
imageBuffer[counter + 2] =(byte)((colorSpace << 24) >> 24 );
imageBuffer[counter + 3] =(byte)(colorSpace >> 24 );
counter += 4;
}
}
return ByteBuffer.wrap(imageBuffer);
}
I really advice you to use the Slick2d solution and is you don't reach your goal then you can switch to the more complicated one
Related
This is the new Composite class for a Multiply(Overlay) effect made by Kristopher Ives(Howto perform a MULTIPLY composite effect using Graphics2D). As far as I can tell he's been inactive for quite some time. Every time I run the Main class all I get is "Expected integer sample type" exception which is thrown when:
'(r.getSampleModel().getDataType() != DataBuffer.TYPE_INT)'
import java.awt.*;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
public class MultiplyComposite implements Composite, CompositeContext {
protected void checkRaster(Raster r) {
if (r.getSampleModel().getDataType() != DataBuffer.TYPE_INT) {
throw new IllegalStateException("Expected integer sample type");
}
}
#Override
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
checkRaster(src);
checkRaster(dstIn);
checkRaster(dstOut);
int width = Math.min(src.getWidth(), dstIn.getWidth());
int height = Math.min(src.getHeight(), dstIn.getHeight());
int x, y;
int[] srcPixels = new int[width];
int[] dstPixels = new int[width];
for (y=0; y < height; y++) {
src.getDataElements(0, y, width, 1, srcPixels);
dstIn.getDataElements(0, y, width, 1, dstPixels);
for (x=0; x < width; x++) {
dstPixels[x] = mixPixel(srcPixels[x], dstPixels[x]);
}
dstOut.setDataElements(0, y, width, 1, dstPixels);
}
}
private static int mixPixel(int x, int y) {
int xb = (x) & 0xFF;
int yb = (y) & 0xFF;
int b = (xb * yb) / 255;
int xg = (x >> 8) & 0xFF;
int yg = (y >> 8) & 0xFF;
int g = (xg * yg) / 255;
int xr = (x >> 16) & 0xFF;
int yr = (y >> 16) & 0xFF;
int r = (xr * yr) / 255;
int xa = (x >> 24) & 0xFF;
int ya = (y >> 24) & 0xFF;
int a = Math.min(255, xa + ya);
return (b) | (g << 8) | (r << 16) | (a << 24);
}
#Override
public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) {
return this;
}
#Override
public void dispose() {
}
public static final MultiplyComposite Multiply = new MultiplyComposite();
}
This is my code for blending the two photos together which are BufferedImage types. The Overlay is provided from a folder and converted to a buffered image in a separate method which you can find at the bottom, and the SS is provided by a screenshot that is run through another class to make it greyscale and is then converted to a BufferedImage and is returned(You can find that at the bottom too).
// BLENDING THE PHOTOS TOGETHER
public static BufferedImage photosBlender(BufferedImage SS, BufferedImage Overlay) {
try {
BufferedImage base = SS;
BufferedImage overlay = Overlay;
Graphics2D g2d = base.createGraphics();
g2d.setComposite(MultiplyComposite.Multiply);
int x = (base.getWidth() - overlay.getWidth()) / 2;
int y = (base.getHeight() - overlay.getHeight()) / 2;
g2d.drawImage(overlay, x, y, null);
g2d.dispose();
File f = new File("resources/OutputImages/OutputBlended.png");
ImageIO.write((RenderedImage) base, "png", f);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
GETTING THE OVERLAY: the args is provided by a random selection method for selecting random overlay files provided in resources
public static BufferedImage OverlayProcess(String args) {
BufferedImage OverlayInput = null;
JFrame OverlayFrame = null;
try {
OverlayInput = ImageIO.read(new File(args));
ImageIcon screenShotIcon = new ImageIcon(OverlayInput);
//To Display selected Overlay for Testing purposes
/*OverlayFrame = new JFrame();
OverlayFrame.setLayout(new FlowLayout());
OverlayFrame.setSize(1500, 800);
JLabel lbl = new JLabel();
lbl.setIcon(screenShotIcon);
OverlayFrame.add(lbl);
OverlayFrame.setVisible(true);
OverlayFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
} catch (IOException e) {
}
return OverlayInput;
}
GREYSCALING THE SS
public class GreyscaleTheImage {
public static BufferedImage main(String args) throws IOException {
BufferedImage img = null;
File f = null;
//read image
try {
f = new File(args);
img = ImageIO.read(f);
} catch (IOException e) {
System.out.println(e);
}
//get image width and height
int width = img.getWidth();
int height = img.getHeight();
//convert to grayscale
int x = 0;
int y;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
int p = img.getRGB(x, y);
int a = (p >> 24) & 0xff;
int r = (p >> 16) & 0xff;
int g = (p >> 8) & 0xff;
int b = p & 0xff;
//calculate average
int avg = (r + g + b) / 3;
//replace RGB value with avg
p = (a << 24) | (avg << 16) | (avg << 8) | avg;
img.setRGB(x, y, p);
}
}
BufferedImage finalImage = null;
try {
finalImage = img;
} catch (Exception e) {
System.out.println(e);
}
//write image
try {
f = new File("resources/OutputImages/Output.png");
ImageIO.write(img, "png", f);
} catch (IOException e) {
System.out.println(e);
}
return finalImage;
}//main() ends here
}//class ends here
I've already tried changing the Raster of the image but it's just too much for my current skill level. I simply can't understand what sampleModels are or what it means to have a DataType of 3. I have a suspicion that it might have something to do with the image possibly having something other than a 32 bit "integer" buffer (whatever that means), because of Kristopher's resources saying that's all it will work with.
I made a new method that converts the image to ARGB, thank you to Harald K for the help he left in the comment section. The ARGB image works with the method I was having issues with.
I want to put a tensorflow model on Anroid.
I recently noticed that the results of running the same data in Python and Android, respectively, are inconsistent.
After several trial and error, I found that the input data I entered when I ran the model on Android was wrong.
It was just a java.lang.IllegalArgumentException error, and I think I put the data correctly, but I have no idea what went wrong.
I used images that were transformed into image resizing and gray scale as learning data. in Python
I did the same preprocessing on Android.
My image type is .jpg
I attached my source.
Source related to image preprocessing
btntrans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
image_bitmap = resizeBitmapImage(image_bitmap, 28);
image_bitmap = RGB2GRAY(image_bitmap);
image.setImageBitmap(image_bitmap);
byte[] byteArrayRes = bitmapToByteArray(image_bitmap);
float[] inputArray = bytetofloat(byteArrayRes);
activityPrediction(inputArray);
}
catch(Exception e){
}
}
});
Everything happens when I click the button
resizeBitmapImage method
public Bitmap resizeBitmapImage(Bitmap source, int maxResolution)
{
int width = source.getWidth();
int height = source.getHeight();
int newWidth = width;
int newHeight = height;
float rate = 0.0f;
if(width > height)
{
if(maxResolution < width)
{
rate = maxResolution / (float) width;
newHeight = (int) (height * rate);
newWidth = maxResolution;
}
}
else
{
if(maxResolution < height)
{
rate = maxResolution / (float) height;
newWidth = (int) (width * rate);
newHeight = maxResolution;
}
}
return Bitmap.createScaledBitmap(source, newWidth, newHeight, true);
}
RGB2GRAY method
public Bitmap RGB2GRAY(Bitmap image){
int width = image.getWidth();
int height = image.getHeight();
Bitmap bmOut;
bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
for(int x = 0; x < width; x++){
for(int y = 0 ; y < height; y++){
int pixel = image.getPixel(x, y);
int A = Color.alpha(pixel);
int R = Color.red(pixel);
int G = Color.green(pixel);
int B = Color.blue(pixel);
R = G = B = (int)(0.299 * R + 0.587 * G + 0.114 * B);
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
bitmap to byte array method
private byte[] bitmapToByteArray(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
bytetofloat method
public float[] bytetofloat(byte[] array){
int[] returnArr = new int[array.length/4];
float[] returnArr1 = new float[array.length/4];
for(int i = 0 ; i < returnArr.length; i++){
//array[i] = 0;
returnArr[i] = array[i*4] & 0xFF;
if(returnArr[i] < 0 || returnArr[i]>255)
Log.d("ARRAY", returnArr[i]+" ");
returnArr1[i] = (float)returnArr[i];
}
return returnArr1;
}
When I run it with the above source, I get this error exactly.
java.lang.IllegalArgumentException: buffer with 308 elements is not
compatible with a Tensor with shape [1, 28, 28]
28 * 28 is Input image size
Before image resizing, it had an average width of 20 and a height of 36.
The strange thing is that the number 308 is changed to 306, 307 and fixed.
What can i do?
Here is my method for converting Java BufferedImage to Tensor object:
private static Tensor<?> convertImageToArray(BufferedImage bf) {
int width = bf.getWidth();
int height = bf.getHeight();
float[][][][] rgbArray = new float[1][height][width][3];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
bf.getRaster().getPixel(i, j, rgbArray[0][i][j]);
}
}
return Tensor.create(rgbArray);
}
Your problem is probably in missed channels of your image. Float array length must be equal to
height * width * channels
of the image.
I have blured an image. But it is not smooth.
I heard that if I use the gaussian blur technique then I can remove the box effect.
But I don't know how to implement it with my code (I did some random technique but it messes with the color). Can you suggest me how to do gaussian blur with my code?
public class BlurImageDemo {
Color c[];
BlurImageDemo() throws IOException, InterruptedException {
File f = new File("D:\\x.jpg");
BufferedImage im = ImageIO.read(f);
BufferedImage bi = new BufferedImage(im.getWidth(), im.getHeight(), BufferedImage.TYPE_INT_RGB);
int i = 0;
int max = 400, radius = 10;
int a1 = 0, r1 = 0, g1 = 0, b1 = 0;
c = new Color[max];
int x = 1, y = 1, x1, y1, ex = 5, d = 0;
for (x = radius; x < im.getHeight() - radius; x++) {
for (y = radius; y < im.getWidth() - radius; y++) {
//20x20 matrix
for (x1 = x - radius; x1 < x + radius; x1++) {
for (y1 = y - radius; y1 < y + radius; y1++) {
c[i++] = new Color(im.getRGB(y1, x1));
//System.out.println(i);
}
}
i = 0;
for (d = 0; d < max; d++) {
a1 = a1 + c[d].getAlpha();
}
a1 = a1 / (max);
for (d = 0; d < max; d++) {
r1 = r1 + c[d].getRed();
}
r1 = r1 / (max);
for (d = 0; d < max; d++) {
g1 = g1 + c[d].getGreen();
}
g1 = g1 / (max);
for (d = 0; d < max; d++) {
b1 = b1 + c[d].getBlue();
}
b1 = b1 / (max);
int sum1 = (a1 << 24) + (r1 << 16) + (g1 << 8) + b1;
bi.setRGB(y, x, (int) (sum1));
}
}
ImageIO.write(bi, "jpg", new File("D:\\x1.jpg"));
}
public static void main(String[] args) throws IOException, InterruptedException {
new BlurImageDemo();
}
}
One really nice property of Gaussian blur is that it is separable, meaning that it can be expressed as the composition of a purely horizontal and a purely vertical blur. The advantage of doing that is that, per pixel, it them takes 2N multiplications (N is the size of the kernel), whereas the 2D non-separated version takes N2 multiplications. For N=7 (as you have), that's already a decent difference. It also has a small downside, the intermediate result is either rounded (losing some precision) or big (3 floats per pixel instead of 1 int), usually a little rounding is not a problem though.
An other thing, more of an implementation detail, is that the division by the total weight of the kernel can be put into the kernel itself, saving a whole bunch of (pretty slow) divisions.
Also your kernel doesn't actually look like a Gaussian, it's too "pointy". That's up to you, but the Gaussian kernel is the only circularly symmetric one that is also separable (if you only look at real-valued kernels) and generally has nice properties, so I would recommend only deviating from it if there is a good reason for it.
Anyway I'll write some example code now, not tested:
BufferedImage transposedHBlur(BufferedImage im) {
int height = im.getHeight();
int width = im.getWidth();
// result is transposed, so the width/height are swapped
BufferedImage temp = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
float[] k = new float[7] { 0.00598, 0.060626, 0.241843, 0.383103, 0.241843, 0.060626, 0.00598 };
// horizontal blur, transpose result
for (int y = 0; y < height; y++) {
for (int x = 3; x < width - 3; x++) {
float r = 0, g = 0, b = 0;
for (int i = 0; i < 7; i++) {
int pixel = im.getRGB(x + i - 3, y);
b += (pixel & 0xFF) * k[i];
g += ((pixel >> 8) & 0xFF) * k[i];
r += ((pixel >> 16) & 0xFF) * k[i];
}
int p = (int)b + ((int)g << 8) + ((int)r << 16);
// transpose result!
temp.setRGB(y, x, p);
}
}
return temp;
}
Since it also transposes, you can simply call it twice and the second time will effectively be a vertical blur that also restores the orientation:
temp = transposedHBlur(input);
result = transposedHBlur(temp);
This is the best article I ever found:
Java Image Processing
I got the answer. here it is.
public class BlurImageDemo {
Color c[];
BlurImageDemo() throws IOException, InterruptedException {
File f = new File("D:\\p.jpg");
BufferedImage im = ImageIO.read(f);
BufferedImage bi = new BufferedImage(im.getWidth(), im.getHeight(), BufferedImage.TYPE_INT_RGB);
int i = 0;
int max = 49, radius = 3;
int a1 = 0, r1 = 0, g1 = 0, b1 = 0,t=0;
c = new Color[max];
float xx[] = {1,1,1,1,1,1,1,
1,3,3,3,3,3,1,
1,3,4,4,4,3,1,
1,3,4,15,4,3,1,
1,3,4,4,4,3,1,
1,3,3,3,3,3,1,
1,1,1,1,1,1,1,
};
float h=0;
for(t=0;t<xx.length;t++){
h+=xx[t];
}
System.out.println(h);
int x = 1, y = 1, x1, y1, ex = 5, d = 0, ll = 0;
for (x = radius;x < im.getHeight()- radius; x++) {
for (y = radius; y < im.getWidth() - radius; y++) {
for (x1 = x - radius; x1 <= x + radius; x1++) {
for (y1 = y - radius; y1 <= y + radius; y1++) {
c[i] = new Color(im.getRGB(y1, x1));
//System.out.println(i);
//ll+=xx[i];
//System.out.println(ll);
i++;
}
}
i = 0;
ll = 0;
for (d = 0; d < max; d++) {
float o = xx[d] * c[d].getAlpha();
a1 = (int) (a1 + o);
}
a1 = (int) (a1 / h);
for (d = 0; d < max; d++) {
float o = xx[d] * c[d].getRed();
r1 = (int) (r1 + o);
}
r1 = (int) (r1 / h);
for (d = 0; d < max; d++) {
float o = xx[d] * c[d].getGreen();
g1 = (int) (g1 + o);
}
g1 = (int) (g1 / h);
//System.out.println(g1);
for (d = 0; d < max; d++) {
float o = xx[d] * c[d].getBlue();
//System.out.println(o);
b1 = (int) (b1 + o);
}
b1 = (int) (b1 / h);
int sum1 = (r1 << 16) + (g1 << 8) + b1;
bi.setRGB(y, x, sum1);
r1 = g1 = b1 = 0;
//System.out.println(new Color(sum1));
}
}
ImageIO.write(bi,
"jpg", new File("D:\\p2.jpg"));
}
public static void main(String[] args) throws IOException, InterruptedException {
new BlurImageDemo();
}
}
Is there any way to bend a BufferedImage in Java?
I thought that if I crop the image into smaller pieces and rotate them then I would essentially bend the image, but it doesn't seem to work.
Here is the method I created:
/**
* This is a recursive method that will accept an image the point where the bending will start and the point where the bending will end, as well as the angle of bending
*
* #param original:the original image
* #param startingPoint: the point where the bending should start
* #param endingPoint: the point where the bending should end
* #param radiands: the angle
* #return the bent image
*/
public static BufferedImage getBentImage(BufferedImage original, int startingPoint, int endingPoint, double radians) {
if (startingPoint >= endingPoint)
return original;
int type = BufferedImage.TYPE_INT_ARGB;
int width = original.getWidth();
int height = original.getHeight();
BufferedImage crop = original.getSubimage(0, 0, startingPoint, height);
BufferedImage crop0 = original.getSubimage(startingPoint, 0, width - startingPoint, height);
BufferedImage bendCrop = new BufferedImage(width, height, type);
BufferedImage image = new BufferedImage(width, height, type);
AffineTransform rotation = new AffineTransform();
rotation.translate(0, 0);
rotation.rotate(radians);
Graphics2D g = bendCrop.createGraphics();
g.drawImage(crop0, rotation, null);
g.dispose();
g = image.createGraphics();
g.drawImage(crop, 0, 0, null);
g.drawImage(bendCrop, startingPoint, 0, null);
g.dispose();
return getBentImage(image, startingPoint + 1, endingPoint, radians);
}
This is the original Image:
And this is the result of this getBentImage(image, 200, 220, Math.toRadians(1)):
I was expecting something closer to:
Any ideas on how to actually implement a getBentImage() method?
As suggested in the comments, a simple approach is to divide the image into 3 parts:
Identical to the original.
Bent according to the bending transformation.
Constant diagonal continuation.
Here is a quick and a bit messy example that shows the original shape and the resulting shape below it. I just used a label icon for the images instead of doing custom painting. (Also I didn't adhere to the Java naming conventions with final variables because it's math and not typical coding.)
Since there are quite a few variables in the calculation code, I added a sketch at the end that shows what the variables represent.
public class Main extends JFrame {
static BufferedImage image;
public static void main(String[] args) {
try {
image = ImageIO.read(ClassLoader.getSystemResource("img.png"));
} catch (IOException e) {
e.printStackTrace();
}
new Main();
}
public Main() {
getContentPane().setLayout(new BorderLayout(5, 10));
BufferedImage img2 = transform(15, 100, 300);
JLabel label1 = new JLabel(new ImageIcon(image));
label1.setHorizontalAlignment(JLabel.LEFT);
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
add(label1, BorderLayout.NORTH);
JLabel label2 = new JLabel(new ImageIcon(img2));
label2.setHorizontalAlignment(JLabel.LEFT);
label2.setOpaque(true);
label2.setBackground(Color.CYAN);
add(label2);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
static BufferedImage transform(int t, int x1, int x2) {
final double TH = Math.toRadians(t);
final int D = x2 - x1;
final int W = image.getWidth();
final int H = image.getHeight();
final int dD = (int) (D / (2 * TH) * Math.sin(2 * TH));
final int dH = (int) (D / TH * Math.pow(Math.sin(TH), 2));
final int pH = (int) ((W - x2) * Math.tan(2 * TH));
final int width = W - (D - dD);
final int height = (int) (H + dH + pH);
System.out.println(W + " " + H + " -> " + width + " " + height);
BufferedImage img2 = new BufferedImage(width, height, image.getType());
for (int x = 0; x < x1; x++) {
for (int y = 0; y < H; y++) {
int rgb = image.getRGB(x, y);
img2.setRGB(x, y, rgb);
}
}
for (int x = x1; x < x2; x++) {
for (int y = 0; y < H; y++) {
int rgb = image.getRGB(x, y);
int dx = (int) (D / (2 * TH) * Math.sin(2 * (x-x1) * TH / D));
int dy = (int) (D / TH * Math.pow(Math.sin((x-x1) * TH / D), 2));
img2.setRGB(x1 + dx, y + dy, rgb);
}
}
for (int x = x2; x < W; x++) {
for (int y = 0; y < H; y++) {
int rgb = image.getRGB(x, y);
int dp = (int) ((x - x2) * Math.tan(2 * TH));
img2.setRGB(x - (D - dD), y + dH + dp, rgb);
}
}
return img2;
}
}
As for the calculations, I'll leave it for you as homework; it's just geometry/trigonometry which belongs on Math.SE more than on SO. If you can't figure it out I'll give you a direction.
Note that this method might not be fast at all and could certainly be optimized, I'll leave that to you also. Oh, and rounding doubles to ints carelessly, so the result is not pixel-perfect.
I dont know what you mean by bending but essentially you have a rectangle and you break one piece of it and rotate it:
so the algorithm is as follows:
rotate line(x, 0, width-1, 0)
rotate line(x, height-1, width-1, height-1)
connect the pieces
So essentially you are looking for rotate line.
I have struggled with this problem for several days now, and I just can't solve the problem myself.
I have a Minecraft 2D sort of game. The Blocks are divided into Chunks of 64 * 64 Blocks.
I have this draw function:
#Override
protected void gameDraw(Graphics2D g) {
g.drawImage(Texture.getImage("background"), 0, 0, 800, 600, 0, 0, 800, 600, null);
int pixelXMin = (int) (screenX);
int pixelYMin = (int) (screenY);
int pixelXMax = (int) (canvas.getWidth() + screenX);
int pixelYMax = (int) (canvas.getHeight() + screenY);
int blockXMin = pixelXMin / Block.BLOCK_PIXEL_WIDTH;
int blockYMin = pixelYMin / Block.BLOCK_PIXEL_HEIGHT;
int blockXMax = pixelXMax / Block.BLOCK_PIXEL_WIDTH;
int blockYMax = pixelYMax / Block.BLOCK_PIXEL_HEIGHT;
int diffX = pixelXMin % Block.BLOCK_PIXEL_WIDTH;
int diffY = pixelYMin % Block.BLOCK_PIXEL_HEIGHT;
for (int bx = blockXMin; bx < blockXMax; bx++) {
for (int by = blockYMin; by < blockYMax; by++) {
int chunkNum = bx / Chunk.COLUMNS_IN_CHUNK;
int blockX = bx % Chunk.COLUMNS_IN_CHUNK;
int blockY = by % Chunk.COLUMNS_IN_CHUNK;
Chunk chunk = level.loadChunk(chunkNum);
Block block = chunk.getBlock(blockX, blockY);
if (block == null) { continue; }
BlockData blockData = block.getBlockData();
if (blockData.getType().getTextureName().equals("")) { continue; }
int blockPosX = canvas.getWidth() - (bx * Block.BLOCK_PIXEL_WIDTH + diffX);
int blockPosY = canvas.getHeight() - (by * Block.BLOCK_PIXEL_HEIGHT + diffY);
BufferedImage image = Texture.getImage(blockData.getType().getName());
g.drawImage(image, blockPosX, blockPosY, Block.BLOCK_PIXEL_WIDTH, Block.BLOCK_PIXEL_HEIGHT, null);
}
}
}
Here i tried to calculate the maximum and minimum number of pixels on the screen with the variables screenX and screenY who serve as "camera position". Then I loop through every block that is within these pixels.
The rest is sort of self explanatory.
Now for the problem:
As you can see does the game draw nicely, but when you change the values of screenX and screenY, the "box" of blocks move realtive to the screen, but the blocks stay at the same location.
How can I solve this? Thank you!
Link to dropbox download for .jar file and full sourcecode. gameDraw() is in CubeExplorer.java
https://www.dropbox.com/sh/madienlwmhjt2wh/AADA83aKa00j5UFowhotfmmaa?dl=0
SOLUTION
Here is my solution:
#Override
protected void gameDraw(Graphics2D g) {
g.drawImage(Texture.getImage("background"), 0, 0, 800, 600, 0, 0, 800, 600, null);
int x = (int) (screenX / Block.BLOCK_PIXEL_WIDTH);
int y = (int) (screenY / Block.BLOCK_PIXEL_HEIGHT);
int blockXMin = x;
int blockYMin = y;
int blockXMax = x + canvas.getWidth() / Block.BLOCK_PIXEL_WIDTH + 2;
int blockYMax = y + canvas.getWidth() / Block.BLOCK_PIXEL_WIDTH + 2;
System.out.println(x + "," + y + " " + blockXMax + "," + blockYMax);
for(int blockX = blockXMin; blockX < blockXMax; blockX++) {
for(int blockY = blockYMin; blockY < blockYMax; blockY++) {
int chunkX = blockX / Chunk.COLUMNS_IN_CHUNK;
Chunk chunk = level.loadChunk(chunkX);
int blockChunkPosX = blockX % Chunk.COLUMNS_IN_CHUNK;
int blockChunkPosY = blockY % Chunk.ROWS_IN_CHUNK;
Block block = chunk.getBlock(blockChunkPosX, blockChunkPosY);
if (block == null) { continue; }
BlockData blockData = block.getBlockData();
if (blockData.getType().getTextureName().equals("")) { continue; }
int blockScreenLocX = (int) ((int) + blockX * Block.BLOCK_PIXEL_WIDTH - screenX);
int blockScreenLocY = 600 - (int) ((int) + blockY * Block.BLOCK_PIXEL_HEIGHT - screenY);
BufferedImage image = Texture.getImage(blockData.getType().getName());
g.drawImage(image, blockScreenLocX, blockScreenLocY, Block.BLOCK_PIXEL_WIDTH, Block.BLOCK_PIXEL_HEIGHT, null);
}
}
}