Hallo zusammen,
ich versuche gerade ein Spielfeld zu erstellen. Der erste Teil funktioniert auch prima. Nun wollte ich nachträglich noch Rechtecke zeichnen und hinzufügen (Die Positionierung im beigefügten Code passt noch nicht, aber wie auch kann Sie ja nicht sehen *gg*).
Leider weiss ich nicht, warum diese nicht angezeigt werden. Es handelt sich im folgenden Code um den Vector image, den ich erzeuge und danach gerne anzeigen würde. Was habe ich falsch gemacht?
ich versuche gerade ein Spielfeld zu erstellen. Der erste Teil funktioniert auch prima. Nun wollte ich nachträglich noch Rechtecke zeichnen und hinzufügen (Die Positionierung im beigefügten Code passt noch nicht, aber wie auch kann Sie ja nicht sehen *gg*).
Leider weiss ich nicht, warum diese nicht angezeigt werden. Es handelt sich im folgenden Code um den Vector image, den ich erzeuge und danach gerne anzeigen würde. Was habe ich falsch gemacht?
Code:
package project.gui;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import project.gui.listener.ButtonListener;
/**
* Das Hauptfenster ist das eigentliche Zentrum des Programms. Es sorgt daf�r,
* dass alle graphischen Elemente angezeigt werden, und dass �berall das zu
* bearbeitende Bild bekannt ist.
*/
public class MainFrame extends JFrame {
private static final long serialVersionUID = 3258131345233623349L;
private JPanel panel;
private MainMenu mainMenu;
private JButton startCalc, newCalc;
private Vector image = new Vector();
private MyJTextField[][] problem = new MyJTextField[9][9];
private Color bgcolor = new Color(118, 146, 196);
private Color cellsColor = new Color(222, 222, 222);
private Font fett = new Font("Arial Bold", 1, 16);
public MainFrame() {
setTitle("Spielfeld");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Container contentPane = getContentPane();
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(bgcolor);
mainMenu = new MainMenu(this);
setJMenuBar(mainMenu);
setSize(560, 560);
setLocationRelativeTo(null);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
exit();
}
});
// Rahmen für Spielfeld
for (int j = 0; j < 3; j++ ){
for( int i = 0; i < 3; i++ ){
int x1 = (100 + (j * 120));
int x2 = (100 + (j * 120));
int y1 = 120;
int y2 = 120;
Color color = new Color(0,0,0);
boolean fill = true;
image.add( new Rechteck( x1, x2, y1, y2, color, fill ));
}
}
createSudokuPanel();
createControlPanel();
contentPane.add(panel);
}
private void createSudokuPanel() {
boolean flag = false;
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
problem[y][x] = new MyJTextField();
problem[y][x].setFont(fett);
problem[y][x].setHorizontalAlignment(SwingConstants.CENTER);
// problem[y][x].setBorder(null);
if (flag) {
problem[y][x].setBackground(Color.WHITE);
flag = false;
} else {
problem[y][x].setBackground(cellsColor);
flag = true;
}
panel.add(problem[y][x]);
problem[y][x].setBounds(100 + (y * 40), 60 + (x * 40), 40, 40);
}
}
}
private void createControlPanel() {
startCalc = new JButton("Starte Berechnung");
panel.add(startCalc);
startCalc.setBounds( 100, 450, 150, 20);
startCalc.addActionListener(new ButtonListener(problem, "startCalc", this));
newCalc = new JButton("Neues Spielfeld");
panel.add(newCalc);
newCalc.setBounds( 310, 450, 150, 20);
newCalc.addActionListener(new ButtonListener(problem, "newCalc", this));
}
// Figur hinzufügen
public void add( Paintable p ){
image.add( p );
repaint();
}
// Bild zeichnen
protected void paintComponent( Graphics g ){
for( int i = 0; i < 9; i++ )
((Paintable)image.get(i)).paint( g );
}
public void exit() {
System.exit(0);
}
}
//Oberklasse, für gemeinsahme Grundlage
abstract class Paintable{
private int x, y, w, h;
private Color color;
private boolean fill;
public Paintable( int x, int y, int w, int h, Color color, boolean fill ){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.color = color;
this.fill = fill;
}
public abstract void paint( Graphics g );
public int getX(){ return x; }
public int getY(){ return y; }
public int getWidth(){ return w; }
public int getHeight(){ return h; }
public Color getColor(){ return color; }
public boolean isFill(){ return fill; }
}
class Rechteck extends Paintable {
public Rechteck( int x, int y, int w, int h, Color color, boolean fill){
super( x, y, w, h, color, fill );
}
public void paint( Graphics g ){
g.setColor( getColor() );
if( isFill() )
g.fillRect( getX(), getY(), getWidth(), getHeight() );
else
g.drawRect( getX(), getY(), getWidth(), getHeight() );
}
}