Hallo Ich bin ganz neu hier und will auch schon gleich meine Frage loswerden. Und zwar habe ich ein problem. Ich wollte in Java das Brettspiel "GO" implementieren. Nun war ich dabei erstmal grafisch das Spielfeld zu implementieren. Das funktioniert auch alles ganz gut, lediglich habe ich probleme mit meiner repaint Funktion. Und zwar sagt eclipse mir: "drawpanel can not be resolved or is not a field". Vllt. ist es ganz einfach aber ich sehe meinen Fehler einfach nicht.
Den Fehler schmeißt eclipse mir in der Klasse Gamefield in Zeile 93.
Ich poste gleich mal beide Klassen die ich bisher erstellt habe:
Den Fehler schmeißt eclipse mir in der Klasse Gamefield in Zeile 93.
Ich poste gleich mal beide Klassen die ich bisher erstellt habe:
Java:
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.event.*;
public class Gamefield {
int _gameSize; //Size of the Gamefield
int _gameArray[][]; //Array which contains the numbers 0, 1 or 2. 0 = no stone, 1 = white stone, 2 = black stone
int _oldWidth = 500;
int _oldHeight = 500;
JFrame MainFrame;
DrawPanel drawpanel;
Color _backgroundColor = new Color(251, 196, 103);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //Size of the Computer Screen
//The constructor of the class Gamefield
Gamefield(int _gamesize)
{
this._gameSize = _gamesize;
createGameField();
createFrame("Go Brett");
draw();
}
//This method creates a new board with no stones
public void createGameField()
{
_gameArray = new int[_gameSize][_gameSize];
for(int i=0; i<_gameSize; i++)
{
for(int j = 0; j < _gameSize; j++)
{
_gameArray[i][j] = 0;
}
}
}
//This method creates the JFrame
public void createFrame(String title)
{
MainFrame = new JFrame(title);
MainFrame.setSize(500, 500);
MainFrame.setVisible(true);
MainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//centers the frame
MainFrame.setLocation( (d.width - MainFrame.getSize().width) / 2,
(d.height - MainFrame.getSize().height) / 2 );
//adds the componentlistener to the frame
MainFrame.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent arg0) {
resizeContent();
}
public void componentMoved(ComponentEvent arg0) {}
public void componentShown(ComponentEvent arg0) { System.out.println("shown"); }
public void componentHidden(ComponentEvent arg0) {}
});
}
//This method creates and instance off the class DrawPanel
public void draw()
{
drawpanel = new DrawPanel(_backgroundColor, _gameSize, _gameArray, MainFrame.getHeight(), MainFrame.getHeight());
MainFrame.add(drawpanel);
}
public void resizeContent()
{
if(MainFrame.getHeight() != MainFrame.getWidth())
{
if(MainFrame.getHeight() > _oldHeight || MainFrame.getWidth() > _oldWidth )
{
if(MainFrame.getHeight() > MainFrame.getWidth())
{
MainFrame.setSize(MainFrame.getHeight(), MainFrame.getHeight());
_oldHeight = MainFrame.getHeight();
_oldWidth = MainFrame.getHeight();
MainFrame.drawpanel.repaint();
}
else
{
MainFrame.setSize(MainFrame.getWidth(), MainFrame.getWidth());
_oldHeight = MainFrame.getWidth();
_oldWidth = MainFrame.getWidth();
}
}
else
{
if(MainFrame.getHeight() > MainFrame.getWidth())
{
MainFrame.setSize(MainFrame.getWidth(), MainFrame.getWidth());
_oldHeight = MainFrame.getWidth();
_oldWidth = MainFrame.getWidth(); }
else
{
MainFrame.setSize(MainFrame.getHeight(), MainFrame.getHeight());
_oldHeight = MainFrame.getHeight();
_oldWidth = MainFrame.getHeight();
}
}
}
}
public static void main(String[] agrs)
{
Gamefield _gamefield = new Gamefield(19);
}
}
Java:
import java.awt.Graphics;
import javax.swing.*;
import java.awt.Color;
import java.awt.geom.*;
import java.awt.*;
public class DrawPanel extends JPanel
{
int height;
int width;
int array[][];
int size;
DrawPanel(Color _color, int size, int array[][], int height, int width)
{
setBackground(_color);
this.size = size;
this.array = array;
this.height = height;
this.width = width;
}
//The paint-mehotd which draws the board
@Override
protected void paintComponent( Graphics g )
{
super.paintComponent( g );
Graphics2D ga = (Graphics2D) g;
// for(int i=0; i<size; i++)
// {
// for(int j = 0; j<size; j++)
// {
// //17 and 39 are the values of the JFrame border
// int x = (j + 1) * ((width - 17) / (size +1)) ;
// int y = (i + 1) * ((height - 39) / (size +1));
//
// Shape circle = new Ellipse2D.Float(x, y, 3, 3);
// ga.draw(circle);
// ga.setPaint(Color.black);
// ga.fill(circle);
// System.out.println("(" + x + "," + y + ")");
// }
// }
for(int i=0; i<size; i++)
{
int x = ((width - 17) / (size +1)) ;
int y = (i + 1) * ((height - 39) / (size +1));
int x2 = 19 * ((width - 17) / (size + 1));
ga.draw(new Line2D.Double(x, y, x2, y));
}
for(int i = 0; i<size; i++)
{
int x = (i +1) *((width - 17) / (size +1)) ;
int y = ((height - 39) / (size +1));
int y2 = 19 * ((height - 39) / (size + 1));
ga.draw(new Line2D.Double(x, y, x, y2 ));
}
}
}