Hallo zusammen,
ich bastel gerade an einem kleinen Spiel namens Kaskade.
Aktuel teste ich gerade die Spieleoberfläche.
Bei einem rechts-Klick baut sich das Spielfeld auf, bei einem links-Klick werden in einem Feld Steine gezeichnet.
Zwei Probleme habe ich :
- wenn ich das Fenster an den Rändern anklick und die Größe verändere, ist der Inhalt plötzlich weg
- wenn ich die Bildlaufleisten nutze, dann aktualisiert sich der Inhalt nicht korreckt. Einige Felder überlappen sich plötzlich.
Noch zur Info :
Das "Spiel" beinhaltet noch KEINE Spielefunktionalität, es geht mir hier nur darum die Funktionsweise des Spielfeld- und Steinezeichnens zu verstehen.
Hier mal der Quelltext :
Danke schon mal
Gruß
Christoph
ich bastel gerade an einem kleinen Spiel namens Kaskade.
Aktuel teste ich gerade die Spieleoberfläche.
Bei einem rechts-Klick baut sich das Spielfeld auf, bei einem links-Klick werden in einem Feld Steine gezeichnet.
Zwei Probleme habe ich :
- wenn ich das Fenster an den Rändern anklick und die Größe verändere, ist der Inhalt plötzlich weg
- wenn ich die Bildlaufleisten nutze, dann aktualisiert sich der Inhalt nicht korreckt. Einige Felder überlappen sich plötzlich.
Noch zur Info :
Das "Spiel" beinhaltet noch KEINE Spielefunktionalität, es geht mir hier nur darum die Funktionsweise des Spielfeld- und Steinezeichnens zu verstehen.
Hier mal der Quelltext :
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GamePanel extends JPanel {
int fieldLength = 50;
int fieldHeight = 50;
int fieldSpace = 5;
int fieldCountX = 10;
int fieldCountY = 10;
public void paintArray(){
Graphics g = getGraphics();
int posX = fieldSpace;
int posY = fieldSpace;
int counterI = 1;
int counterJ = 1;
while ( counterJ <= fieldCountY ){
while ( counterI <= fieldCountX ){
g.setColor(Color.LIGHT_GRAY);
g.fillRoundRect(posX, posY, fieldLength, fieldHeight, 10, 10);
g.setColor(Color.BLACK);
g.drawRoundRect(posX, posY, fieldLength, fieldHeight, 10, 10);
posX = posX + fieldLength + fieldSpace;
counterI++;
}
posY = posY + fieldHeight + fieldSpace;
posX = fieldSpace;
counterI = 1;
counterJ++;
}
}
public void paintStone ( int inPosX, int inPosY, int inCount, Color inColor) {
Graphics g = getGraphics();
Dimension fieldNum;
Dimension fieldPos;
fieldNum = calcFieldNum(inPosX, inPosY);
if ((fieldNum.height > 0) && (fieldNum.width > 0)){
fieldPos = calcFieldStartPos(fieldNum);
g.setColor(Color.LIGHT_GRAY);
g.fillRoundRect(fieldPos.height, fieldPos.width, fieldLength, fieldHeight, 10, 10);
g.setColor(Color.BLACK);
g.drawRoundRect(fieldPos.height, fieldPos.width, fieldLength, fieldHeight, 10, 10);
g.setColor(inColor);
switch (inCount){
case 1:
g.fillOval(fieldPos.height + 20, fieldPos.width + 20, 10, 10);
break;
case 2:
g.fillOval(fieldPos.height + 5, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 35, 10, 10);
break;
case 3:
g.fillOval(fieldPos.height + 5, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 20, fieldPos.width + 20, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 35, 10, 10);
break;
case 4:
g.fillOval(fieldPos.height + 5, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 5, fieldPos.width + 35, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 35, 10, 10);
break;
case 5:
g.fillOval(fieldPos.height + 5, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 20, fieldPos.width + 20, 10, 10);
g.fillOval(fieldPos.height + 5, fieldPos.width + 35, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 35, 10, 10);
break;
case 6:
g.fillOval(fieldPos.height + 5, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 20, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 5, fieldPos.width + 35, 10, 10);
g.fillOval(fieldPos.height + 20, fieldPos.width + 35, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 35, 10, 10);
break;
case 7:
g.fillOval(fieldPos.height + 5, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 20, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 5, 10, 10);
g.fillOval(fieldPos.height + 20, fieldPos.width + 20, 10, 10);
g.fillOval(fieldPos.height + 5, fieldPos.width + 35, 10, 10);
g.fillOval(fieldPos.height + 20, fieldPos.width + 35, 10, 10);
g.fillOval(fieldPos.height + 35, fieldPos.width + 35, 10, 10);
break;
default:
break;
}
}
}
public Dimension calcSize(){
Dimension dimension;
int posX;
int posY;
posX = ( fieldLength + fieldSpace ) * fieldCountX ;
posY = ( fieldHeight + fieldSpace ) * fieldCountY ;
dimension = new Dimension (posX,posY);
return dimension;
}
public Dimension calcFieldNum ( int inPosX, int inPosY ){
int counter;
int posMin; int posMax;
int posX; int posY;
boolean check;
// X-Position berechnen
counter = 1;
posMin = - fieldLength;
posMax = 0;
check = false;
while ((counter <= fieldCountX) && (check == false)){
posMin = posMin + fieldSpace + fieldLength;
posMax = posMax + fieldSpace + fieldLength;
if ((inPosX > posMin) && (inPosX <= posMax)){
check = true;
}
else {
counter ++;
}
}
if (check == false){
posX = 0; // inPosX liegt nicht in einem Feld
}
else {
posX = counter; // inPosX liegt in einem Feld. counter ist die Feldnummer
}
// Y-Position berechnen
counter = 1;
posMin = - fieldHeight;
posMax = 0;
check = false;
while ((counter <= fieldCountY) && (check == false)){
posMin = posMin + fieldSpace + fieldHeight;
posMax = posMax + fieldSpace + fieldHeight;
if ((inPosY > posMin) && (inPosY <= posMax)){
check = true;
}
else {
counter ++;
}
}
if (check == false){
posY = 0; // inPosY liegt nicht in einem Feld
}
else {
posY = counter; // inPosY liegt in einem Feld. counter ist die Feldnummer
}
return new Dimension ( posX, posY);
}
public Dimension calcFieldStartPos ( Dimension inField ){
int posX = fieldSpace;
int posY = fieldSpace;
int counter = 1;
while ( counter < inField.getHeight()){
posX = posX + fieldLength + fieldSpace;
counter ++;
}
counter = 1;
while ( counter < inField.getWidth()){
posY = posY + fieldHeight + fieldSpace;
counter ++;
}
return new Dimension (posX, posY);
}
}
class GameFrame extends JFrame implements ActionListener, MouseListener {
GamePanel pnlGame;
JScrollPane pnlScroll;
int i = 0;
public GameFrame() {
setTitle("Kaskade");
setSize(1024,768);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Container contentPane = getContentPane();
pnlGame = new GamePanel();
pnlScroll = new JScrollPane(pnlGame);
pnlGame.setPreferredSize(new Dimension (pnlGame.calcSize().width,pnlGame.calcSize().height));
pnlScroll.setPreferredSize(new Dimension(100,100));
pnlGame.addMouseListener(this);
contentPane.add(pnlScroll,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if (SwingUtilities.isRightMouseButton(e)){
pnlGame.paintArray();
}
else {
pnlGame.paintStone(e.getX(),e.getY(),i,Color.BLACK);
i++;
if (i == 8){
i = 0;
}
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
}
public class GameWindow {
public static void main(String[] args){
//Display the window.
GameFrame frmKaskade = new GameFrame();
frmKaskade.setSize(800, 600);
frmKaskade.setVisible(true);
frmKaskade.show();
}
}
Danke schon mal
Gruß
Christoph