I wrote a random Dungeon generator, that gives me coordinates like this(S= Start):
Now I would like to print my coordinates on a JPane (or whatever is common for that), but I have no clue where to start ;-(
I saw that I can paint something like rectangles or triangles on a JPane, so I tried to overwrite the paintComponent Method for the Graphics Object with my Console Output function (that is exactly the same, except that I draw instead of print())
@Override
public void paintComponent (Graphics g) {
boolean tmpKoordinateExistiert = false;
boolean tmpSpielerKoordiante = false;
for(int y =0;y <=20; y++) {
for(int x = 0; x <=20; x++) {
if(this.sindKoordinatenEinTreffer(x,y,this.spielerKoordinaten.x,this.spielerKoordinaten.y)) {
tmpSpielerKoordiante = true;
}else {
for(Point p: this.koordinatenListe) {
if(this.sindKoordinatenEinTreffer(x,y,p.x,p.y)) {
tmpKoordinateExistiert = true;
}
}
}
if(tmpSpielerKoordiante) {
g.drawOval(x+10, y+10, 1, 1);
tmpSpielerKoordiante = false;
}else if(!tmpKoordinateExistiert) {
this.repaint();
}else {
g.drawRect(x+10, y+10, 1, 1);
tmpKoordinateExistiert = false;
}
}
System.out.println("");
}
}
but I only get a veeery smal version of the original thing:
What can I do to change that? (Sorry, if that is a stupid question, but I sit here for hours and I'm to blind to find the right solution)
If I increase the size of the rectangles / ovals I get: The whole Dungeon generator Class:
package de.projekt.dungeon;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;
import de.projekt.gui.MiniMap;
import de.projekt.map.DungeonMap;
public class DungeonGenerator {
/**
* startposition des Dungeons
* Kann im Norden, Sueden, osten, Westen liegen (Random)
*/
private Point start;
private Point aktuelleKoordinaten;
/**
* Liste aller Dungeonfelder (bzw. die Koordinaten von denen)
*/
ArrayList<Point> koordinatenListe ;
public ArrayList<Point> getKoordinatenListe() {
return koordinatenListe;
}
public Point getStart() {
return start;
}
/**
* 0 = Norden, 1 = Westen, 2 = Sueden, 3 = Osten
*/
private int himmelsrichtung;
private Random rHimmelsrichtung;
private Random neueKoordinaten;
private int xFaktor = 0;
private int yFaktor = 0;
private Random tmpz = new Random();
/**
* Standardkonstruktor, es werden alles Objekte instanziert, die
* fuer den DungeonGenerator noetig sind.
*
* himmelsrichtung ergibt sich aus einer Zahl zwischen 0-4.
* Dadurch wird Random entschieden, an welcher Stelle der Dungeon startet.
* (N,S,O,W)
* Es wird fuer jeden Startpunkt ein Faktor gesetzt, mit dem im Generator die
* x/y Koordinate multipliziert wird. Dadurch wird sichergestellt, das man nicht im
* Osten startet, und der Dungeon dann in eben diese Richtung generiert wird.
*
* Der Startpunkt wird als erste Koordinate der KoordinatenListe hinzugefuegt.
* Anschliessend beginnt das generieren.
*
* Spaeter koennte man die Mapgroeße ueber das ueberladen des Konstruktors Variabel machen,
* Falls das dann Sinn ergibt.
*
* Jeder Dungeon (Stockwerk?) hat genau eine Map!
*/
public DungeonGenerator(int anzahlMaximalKoordinaten){
this.koordinatenListe = new ArrayList<Point>();
this.rHimmelsrichtung = new Random();
this.neueKoordinaten = new Random();
this.start = new Point();
this.himmelsrichtung = this.rHimmelsrichtung.nextInt(3);
switch(this.himmelsrichtung){
// Norden
case(0): this.start.x = 10;
this.start.y = 0;
this.yFaktor = 1;
break;
// Westen
case(1): this.start.x = 0;
this.start.y = 10;
this.xFaktor = 1;
break;
// Sueden
case(2): this.start.x = 10;
this.start.y = 20;
this.yFaktor = -1;
break;
// Osten
case(3): this.start.x = 20;
this.start.y = 10;
this.xFaktor = -1;
break;
}
koordinatenListe.add(this.start);
this.aktuelleKoordinaten = new Point(this.start);
this.startGenerate(anzahlMaximalKoordinaten);
}
/**
* Generiere den Dungeon und uebergebe den Dungeon(Koordinaten) an die Map.
* (Auch SpielerAnfangskoordinaten)
*/
private void startGenerate(int anzahlMaximalKoordinaten) {
this.generateDungeon(this.start.x, this.start.y, 0, anzahlMaximalKoordinaten);
}
/**
* Gebe das zu dem Dungeon gehoerige Mapobjekt zurueck.
* @return Rueckgabe der Map
*/
public DungeonMap getMap() {
return null;
//return map;
}
/**
* Hier werden die Koordinaten generiert.
*
* Derzeit werden y Koordinaten bevorzugt. (da X nur incr/decr wird, wenn Y == 0 --> Nur eine 50% Chance)
*
* Die Methode wird abgebrochen, sollte eine Wand erreicht werden.
* Die Methode ruft sich ansonsten sooft auf, bis die aktuelle Koordinatenmenge der Maximalen Koordinatenanzahl entspricht
*
* @param x aktuelle x Koordinate
* @param y aktuelle y Koordinate
* @param aktuelleKoordinatenMenge Wieviele Koordinaten umfasst der Dungeon derzeit
* @param anzahlMaximalKoordinaten Wieviele "Spielfelder(Koordinaten)" hat der Dungeon maximal
*/
private void generateDungeon(int x, int y,int aktuelleKoordinatenMenge, int anzahlMaximalKoordinaten) {
int tempX = 0;
int tempY = 0;
while(tempX == 0 && tempY== 0) {
tempX = this.neueKoordinaten.nextInt(2);
tempY = this.neueKoordinaten.nextInt(2);
}
// gehen wir evtl links oder rechts usw., mal sehen
int tmp = this.tmpz.nextInt(3)-1;
// Erlaube es, auf der X / Y Achse zurueckzugehen, fuer verzweigungen
if(this.yFaktor == -1){
this.xFaktor = tmp;
}
if(this.yFaktor == 1){
this.xFaktor = tmp;
}
if(this.xFaktor == -1){
this.yFaktor = tmp;
}
if(this.xFaktor == 1){
this.yFaktor = tmp;
}
// Wenn ich zuerst Nach Y abfrage, wird Y bevorzugt, wenn ich zuerst nach X abfrage halt X
// wenn ich einfach ein Random 0/1 vorklatsche, und nach 0 / 1 Abfrage, dann ist das Ergebniss
// nicht von zuerst X / Y abhaengig.
int randomRichtung = this.neueKoordinaten.nextInt(2);
if(randomRichtung == 0) {
if(tempY==1) {
y = y + (1 * this.yFaktor);
}else if(tempX == 1) {
x = x + (1 * this.xFaktor);
}
}else {
if(tempX==1) {
x = x + (1 * this.xFaktor);
}else if(tempY == 1) {
y = y + (1 * this.yFaktor);
}
}
// Anomalie, bin betrunken, forsche spaeter
if(x == -1)
x=0;
// lul?
if(y == -1)
y=0;
this.aktuelleKoordinaten = new Point(x,y);
//System.out.println("aktuelleKoordinaten: " +this.aktuelleKoordinaten);
this.koordinatenListe.add(this.aktuelleKoordinaten);
if((this.aktuelleKoordinaten.x == 20 && this.start.x !=20)
|| this.aktuelleKoordinaten.y == 20 && this.start.y !=20
|| this.aktuelleKoordinaten.x == 0 && this.start.x !=0
|| this.aktuelleKoordinaten.y == 0 && this.start.y !=0) {
return;
}
aktuelleKoordinatenMenge++;
if(aktuelleKoordinatenMenge <= anzahlMaximalKoordinaten) {
this.generateDungeon(x, y,aktuelleKoordinatenMenge, anzahlMaximalKoordinaten);
} else
return;
}
}
My Gui Class:
package de.projekt.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Gui extends JFrame{
MiniMap miau = new MiniMap();
private static final int N = 256;
public Gui() {
this.setSize(400,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.add(this.miau);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(2 * N, 2 * N);
}
}
And my output Class:
package de.projekt.gui;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JPanel;
import de.projekt.dungeon.DungeonGenerator;
import de.projekt.map.DungeonMap;
public class MiniMap extends JPanel{
private ArrayList<Point> koordinatenListe ;
private Point spielerKoordinaten;
private DungeonGenerator map;
MiniMap(){
this.map = new DungeonGenerator(400);
this.spielerKoordinaten = this.map.getStart();
this.koordinatenListe = this.map.getKoordinatenListe();
}
@Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
boolean tmpKoordinateExistiert = false;
boolean tmpSpielerKoordiante = false;
for(int y =0;y <=20; y++)
{
for(int x = 0; x <=20; x++) {
if(this.sindKoordinatenEinTreffer(x,y,this.spielerKoordinaten.x,this.spielerKoordinaten.y)) {
tmpSpielerKoordiante = true;
}else {
for(Point p: this.koordinatenListe) {
if(this.sindKoordinatenEinTreffer(x,y,p.x,p.y)) {
tmpKoordinateExistiert = true;
}
}
}
if(tmpSpielerKoordiante) {
g.drawOval(x, y, 10, 10);
tmpSpielerKoordiante = false;
}else if(!tmpKoordinateExistiert) {
this.repaint();
}else {
g.drawRect(x+10, y+10, 10, 10);
tmpKoordinateExistiert = false;
}
}
System.out.println("test");
}
System.out.println("enhde");
}
public void setStartPunkt(Point startPunkt) {
this.spielerKoordinaten = startPunkt;
}
public ArrayList<Point> getKoordinatenListe() {
return koordinatenListe;
}
public void setKoordinatenListe(ArrayList<Point> koordinatenListe) {
this.koordinatenListe = koordinatenListe;
}
/**
* Da ein Koordinatenvergleich mindestens 2 mal gebraucht wird, in einer Methode
* @param x aktueller x Wert der Schleife
* @param y aktueller y Wert der Schleife
* @param koordx koordinaten vom Punkt
* @param koordy koordinaten vom Punkt
* @return
*/
private boolean sindKoordinatenEinTreffer(int x, int y, int koordx, int koordy) {
if(x == koordx && y == koordy) {
return true;
}else {
return false;
}
}
/**
* Erstmal nur eine Testausgabe usw. in der Konsole, halt bis wir GUI's haben
*/
public void printTest() {
}
}