mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-14 11:46:06 +00:00
Implement image debug GUI
This commit is contained in:
49
src/main/java/com/dfsek/terra/image/DebugFrame.java
Normal file
49
src/main/java/com/dfsek/terra/image/DebugFrame.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.dfsek.terra.image;
|
||||
|
||||
import com.dfsek.terra.Terra;
|
||||
import com.dfsek.terra.biome.TerraBiomeGrid;
|
||||
import com.dfsek.terra.biome.UserDefinedBiome;
|
||||
import com.dfsek.terra.config.genconfig.BiomeConfig;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class DebugFrame extends JFrame implements ActionListener {
|
||||
private final int x;
|
||||
private final int z;
|
||||
private final BufferedImage img;
|
||||
public DebugFrame(BufferedImage image, String s) {
|
||||
super(s);
|
||||
this.x = image.getWidth();
|
||||
this.z = image.getHeight();
|
||||
this.img = image;
|
||||
new Timer(500, this).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paintComponents(g);
|
||||
for(Player p : Bukkit.getOnlinePlayers()) {
|
||||
int xp = (int) (((double) Math.floorMod(p.getLocation().getBlockX(), x)/x)*getWidth());
|
||||
int zp = (int) (((double) Math.floorMod(p.getLocation().getBlockZ(), z)/z)*getHeight());
|
||||
g.setColor(new Color(255, 255, 255, 64));
|
||||
g.drawRect(xp+10, zp-5, 20, 20);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawString(p.getName(), xp+15, zp);
|
||||
g.drawString(BiomeConfig.fromBiome((UserDefinedBiome) TerraBiomeGrid.fromWorld(p.getWorld()).getBiome(p.getLocation())).getID(), xp+15, zp+15);
|
||||
g.fillOval(xp, zp, 10, 10);
|
||||
g.setColor(Color.RED);
|
||||
g.fillOval(xp+3, zp+3, 5, 5);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
28
src/main/java/com/dfsek/terra/image/DebugGUI.java
Normal file
28
src/main/java/com/dfsek/terra/image/DebugGUI.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.dfsek.terra.image;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class DebugGUI extends Thread {
|
||||
|
||||
private final BufferedImage img;
|
||||
public DebugGUI(BufferedImage img) {
|
||||
this.img = img;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
DebugFrame frame = new DebugFrame(img, "Image2Map Debug GUI");
|
||||
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
frame.setSize(1000, 1000);
|
||||
frame.setResizable(false);
|
||||
ImageIcon imageIcon = new ImageIcon(img.getScaledInstance(1000, 1000, Image.SCALE_SMOOTH));
|
||||
JLabel jLabel = new JLabel();
|
||||
jLabel.setIcon(imageIcon);
|
||||
frame.getContentPane().add(jLabel, BorderLayout.CENTER);
|
||||
frame.pack();
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
package com.dfsek.terra.image;
|
||||
|
||||
import com.dfsek.terra.biome.BiomeZone;
|
||||
import com.dfsek.terra.biome.TerraBiomeGrid;
|
||||
import com.dfsek.terra.config.genconfig.BiomeConfig;
|
||||
import org.bukkit.World;
|
||||
import org.polydev.gaea.biome.NormalizationUtil;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
@@ -35,9 +41,35 @@ public class ImageLoader {
|
||||
}
|
||||
}
|
||||
|
||||
public void debug(boolean genStep, World w) {
|
||||
BufferedImage newImg = copyImage(image);
|
||||
TerraBiomeGrid tb = TerraBiomeGrid.fromWorld(w);
|
||||
BiomeZone z = BiomeZone.fromWorld(w);
|
||||
if(genStep) {
|
||||
for(int x = 0; x < newImg.getWidth(); x++) {
|
||||
for(int y = 0; y < newImg.getHeight(); y++) {
|
||||
float[] noise = tb.getGrid(x, y).getRawNoise(x, y);
|
||||
newImg.setRGB(x, y, new Color((int) (NormalizationUtil.normalize(noise[0], tb.getGrid(x, y).getSizeX()) * ((double) 255/tb.getGrid(x, y).getSizeX())),
|
||||
(int) (NormalizationUtil.normalize(noise[1], tb.getGrid(x, y).getSizeZ()) * ((double) 255/tb.getGrid(x, y).getSizeZ())),
|
||||
(int) (z.getNoise(x, y) * ((double) 255/32)))
|
||||
.getRGB());
|
||||
}
|
||||
}
|
||||
}
|
||||
DebugGUI debugGUI = new DebugGUI(newImg);
|
||||
debugGUI.start();
|
||||
}
|
||||
|
||||
public double getNoiseVal(int x, int y, Channel channel) {
|
||||
return ((double) (getChannel(x, y, channel) - 128)/128)*inverseRoot2;
|
||||
}
|
||||
private static BufferedImage copyImage(BufferedImage source){
|
||||
BufferedImage b = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());
|
||||
Graphics g = b.getGraphics();
|
||||
g.drawImage(source, 0, 0, null);
|
||||
g.dispose();
|
||||
return b;
|
||||
}
|
||||
|
||||
public enum Channel {
|
||||
RED, GREEN, BLUE, ALPHA
|
||||
|
||||
44
src/main/java/com/dfsek/terra/image/WorldImageGenerator.java
Normal file
44
src/main/java/com/dfsek/terra/image/WorldImageGenerator.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.dfsek.terra.image;
|
||||
|
||||
import com.dfsek.terra.biome.BiomeZone;
|
||||
import com.dfsek.terra.biome.TerraBiomeGrid;
|
||||
import com.dfsek.terra.config.WorldConfig;
|
||||
import org.bukkit.World;
|
||||
import org.polydev.gaea.biome.NormalizationUtil;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class WorldImageGenerator {
|
||||
private final World w;
|
||||
private final BufferedImage draw;
|
||||
public WorldImageGenerator(World w, int width, int height) {
|
||||
draw = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
this.w = w;
|
||||
}
|
||||
public void drawWorld(int centerX, int centerZ) {
|
||||
TerraBiomeGrid tb = TerraBiomeGrid.fromWorld(w);
|
||||
int imY = 0;
|
||||
for(int y = centerZ - (draw.getHeight()/2); y < centerZ + (draw.getHeight()/2); y++) {
|
||||
int imX = 0;
|
||||
for(int x = centerX - (draw.getWidth()/2); x < centerX + (draw.getWidth()/2); x++) {
|
||||
int zone = NormalizationUtil.normalize(BiomeZone.fromWorld(w).getRawNoise(x, y), 256);
|
||||
float[] noise = tb.getGrid(x, y).getRawNoise(x, y);
|
||||
Color c = new Color(NormalizationUtil.normalize(noise[0], 256), NormalizationUtil.normalize(noise[1], 256), zone);
|
||||
draw.setRGB(imX, imY, c.getRGB());
|
||||
imX++;
|
||||
}
|
||||
imY++;
|
||||
}
|
||||
}
|
||||
public void save(File file) {
|
||||
try {
|
||||
ImageIO.write(draw, "png", file);
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user