K
KnowIt
Gast
Hi,
also ich hab das Problem das ich eine Zeichnung auf einen JPanel machen möchte. ( Class : MyJPanelSpielbrett)
Nur komme ich nicht dazu den JPanel in die Form einzufügen bzw die Zeichnung auf den JPanel zu machen.
please Help
also ich hab das Problem das ich eine Zeichnung auf einen JPanel machen möchte. ( Class : MyJPanelSpielbrett)
Nur komme ich nicht dazu den JPanel in die Form einzufügen bzw die Zeichnung auf den JPanel zu machen.
please Help
Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class Oberflaeche extends JFrame {
Dimension dim = null;
JButton butSave = new JButton();
JButton butLoad = new JButton();
MyJPanelSpielbrett jpanelSpielbrett = null;
MyJPanelButtonSide jpanelButtonSide = null;
MyJButtonButSave jbuttonButSave = null;
MyJButtonButLoad jbuttonButLoad = null;
private String[][] spielBrett = {
{ "White", "Black", "White", "Black", "White", "Black", "White","Black" },
{ "Black", "White", "Black", "White", "Black", "White", "Black","White" },
{ "White", "Black", "White", "Black", "White", "Black", "White","Black" },
{ "Black", "White", "Black", "White", "Black", "White", "Black","White" },
{ "White", "Black", "White", "Black", "White", "Black", "White","Black" },
{ "Black", "White", "Black", "White", "Black", "White", "Black","White" },
{ "White", "Black", "White", "Black", "White", "Black", "White","Black" },
{ "Black", "White", "Black", "White", "Black", "White", "Black","White" }
};
public Oberflaeche() {
dim = Toolkit.getDefaultToolkit().getScreenSize();
this.addMouseMotionListener(new MyMouseMotionAdapter());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.GRAY);
this.setSize(800, 640);
this.jpanelButtonSide = new MyJPanelButtonSide();
this.add(this.jpanelButtonSide, BorderLayout.EAST);
this.jpanelSpielbrett = new MyJPanelSpielbrett();
this.add(this.jpanelSpielbrett, BorderLayout.CENTER);
this.jbuttonButSave = new MyJButtonButSave();
this.jbuttonButLoad = new MyJButtonButLoad();
this.setVisible(true);
}
public static void main(String[] args) {
new Oberflaeche();
}
class MyMouseMotionAdapter extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
class MyJPanelSpielbrett extends JPanel {
public MyJPanelSpielbrett() {
this.setBackground(Color.BLUE);
}
@Override
public void paint(Graphics graphics) {
spielBrett(graphics);
}
private void spielBrett(Graphics spielBrettGraph) {
int i = 0, j = 0;
while (j < 8) {
i = 0;
while (i < 8) {
if (spielBrett[i][j].equals("White")) {
spielBrettGraph.setColor(Color.WHITE);
spielBrettGraph.fillRect(60 * (i + 1), 60 * (j + 1),
60, 60);
}
if (spielBrett[i][j].equals("Black")) {
spielBrettGraph.setColor(Color.BLACK);
spielBrettGraph.fillRect(60 * (i + 1), 60 * (j + 1),
60, 60);
}
i++;
}
j++;
}
}
}
class MyJPanelButtonSide extends JPanel {
public MyJPanelButtonSide() {
this.setBackground(Color.RED);
}
}
class MyJButtonButSave extends JButton {
public MyJButtonButSave() {
butSave.setPreferredSize(new Dimension(100, 40));
butSave.setLocation(new Point(this.WIDTH/10,this.HEIGHT/10));
jpanelButtonSide.add(butSave);
}
}
class MyJButtonButLoad extends JButton {
public MyJButtonButLoad() {
butLoad.setPreferredSize(new Dimension(100, 40));
jpanelButtonSide.add(butLoad);
}
}
}