Ich habe ein Problem, welches ich selber nicht lösen kann.
Ich bin jetzt soweit, dass ich rechts das Tileset angezeigt bekomme, dazu in der Lage bin ein Feld nach Wunsch auszuwählen und in den linken Teil des JFrames zu zeichnen.
Abgesehen von der Performance, fehlenden Extras und noch sehr unsauberem Code, funktioniert das ja schon super.
Aber ich habe folgendes Problem:
Wenn ich rechts einen Block auswähle, und links in das Fenster rein klicke, zeichnet er mir den Block + zusätzlich das Tileset..
Ich verstehe absolut nicht warum er das macht.
Ich könnte mit dem ausgewählten grünen Block nun das Tileset, was da links hingezaubert wurde übermalen, sobald ich aber einen
anderen Block auswähle, wird das Tileset erneut links hin gemalt.
Hier ist mein Code:
[CODE lang="java" title="Klasse PokeEditor2"]package pokemon;
import javax.swing.*;
public class PokeEditor2 {
private int tilePanelSelectedX = -1;
private int tilePanelSelectedY = -1;
private String filename = "tileset-advance.png";
JScrollPane tileJScrollPane;
TilePanel tilePanel;
MapPanel mapPanel;
public PokeEditor2() {
JFrame mapCreator = new JFrame("Pokemap-Creator");
mapCreator.setLayout(null);
this.tilePanel = new TilePanel(this);
this.tilePanel.addMouseListener(tilePanel);
this.tilePanel.setFocusable(true);
this.tilePanelSelectedX = tilePanel.getSelectedX();
this.tilePanelSelectedY = tilePanel.getSelectedY();
this.mapPanel = new MapPanel(this.tilePanelSelectedX, this.tilePanelSelectedY, this);
this.mapPanel.addMouseListener(mapPanel);
this.mapPanel.setFocusable(true);
this.mapPanel.setBounds(0, 0, 1350, 800);
tileJScrollPane = new JScrollPane(tilePanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tileJScrollPane.setBounds(1387, 0, 150, 800);
tileJScrollPane.getVerticalScrollBar().setUnitIncrement(20);
mapCreator.add(tileJScrollPane);
mapCreator.add(mapPanel);
mapCreator.pack();
mapCreator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mapCreator.setVisible(true);
mapCreator.setSize(1500, 860);
mapCreator.setLocationRelativeTo(null);
mapCreator.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
private void getAndSet(TilePanel tilePanel, MapPanel mapPanel) {
this.tilePanelSelectedX = tilePanel.getSelectedX();
this.tilePanelSelectedY = tilePanel.getSelectedY();
mapPanel.setTilePanelSelectedX(this.tilePanelSelectedX);
mapPanel.setTilePanelSelectedY(this.tilePanelSelectedY);
}
public void runGetandSet() {
getAndSet(tilePanel, mapPanel);
}
}[/CODE]
[CODE lang="java" title="Klasse TilePanel"]package pokemon;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class TilePanel extends JPanel implements MouseListener {
private final PokeEditor2 pokeEditor2;
private int selectedX = -1;
private int selectedY = -1;
private int preSelectedX = -1;
private int preSelectedY = -1;
private int tilesize = 16;
private String filename = "tileset-advance.png";
private BufferedImage tilesetBufferedImage;
public TilePanel(PokeEditor2 pokeEditor2) {
this.pokeEditor2 = pokeEditor2;
try {
loadBufferedImage();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
pokeEditor2.runGetandSet();
g.drawImage(tilesetBufferedImage, 0, 0, 128, 15971, null);
if (selectedX != -1 && selectedY != -1) {
g.setColor(Color.RED);
g.drawRect(selectedX * tilesize, selectedY * tilesize, tilesize, tilesize);
}
}
private void loadBufferedImage() throws IOException {
// tilesetBufferedImage = ImageIO.read(getClass().getResourceAsStream("filename"));
tilesetBufferedImage = ImageIO.read(getClass().getResource(filename));
}
@Override
public void mouseClicked(MouseEvent e) {
if (selectedX != -1 && selectedY != -1) {
preSelectedX = selectedX;
preSelectedY = selectedY;
}
selectedX = e.getPoint().x;
selectedY = e.getPoint().y;
selectedX = selectedX / 16;
selectedY = selectedY / 16;
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public int getSelectedX() {
return selectedX;
}
public int getSelectedY() {
return selectedY;
}
}[/CODE]
[CODE lang="java" title="Klasse MapPanel"]package pokemon;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class MapPanel extends JPanel implements MouseListener {
private final PokeEditor2 pokeEditor2;
private int selectedX = -1;
private int selectedY = -1;
private int preSelectedX = -1;
private int preSelectedY = -1;
private final int tilesize = 16;
private String filename = "tileset-advance.png";
private BufferedImage tilesetBufferedImage;
private int repainted = 0;
private int tilePanelSelectedX = -1;
private int tilePanelSelectedY = -1;
public MapPanel(int tilePanelSelectedX, int tilePanelSelectedY, PokeEditor2 pokeEditor2) {
this.pokeEditor2 = pokeEditor2;
this.tilePanelSelectedX = tilePanelSelectedX;
this.tilePanelSelectedY = tilePanelSelectedY;
try {
loadBufferedImage();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
pokeEditor2.runGetandSet();
if (this.tilePanelSelectedX != -1 && this.tilePanelSelectedY != -1) {
if (selectedX != -1 && selectedY != -1) {
g.drawImage(tilesetBufferedImage, selectedX * tilesize, selectedY * tilesize, (selectedX + 1) * tilesize, (selectedY + 1) * tilesize, this.tilePanelSelectedX * tilesize, this.tilePanelSelectedY * tilesize, (this.tilePanelSelectedX + 1) * tilesize, (this.tilePanelSelectedY + 1) * tilesize, null); // Das muss geändert werden, soll wo anders gezeichnet werden
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (selectedX != -1 && selectedY != -1) {
preSelectedX = selectedX;
preSelectedY = selectedY;
}
selectedX = e.getPoint().x;
selectedY = e.getPoint().y;
selectedX = selectedX / 16;
selectedY = selectedY / 16;
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
private void loadBufferedImage() throws IOException {
//tilesetBufferedImage = ImageIO.read(getClass().getResourceAsStream(filename));
tilesetBufferedImage = ImageIO.read(getClass().getResource(filename));
}
public void setTilePanelSelectedX(int tilePanelSelectedX) {
this.tilePanelSelectedX = tilePanelSelectedX;
}
public void setTilePanelSelectedY(int tilePanelSelectedY) {
this.tilePanelSelectedY = tilePanelSelectedY;
}
}[/CODE]
Vielleicht habt ihr eine Idee, wo hier der Fehler liegt.
Also fakt ist auf jeden Fall, dass die überschriebene Methode paintComponent in TilePanel das Buffered Image nicht nur in das TilePanel zeichnet, sondern auch in das MapPanel.
Ich muss es irgendwie hinbekommen, dass er nur das TilePanel anmalt und nicht alle Komponenten