L
Lixor123
Gast
Hallo,
ich hab ein Problem. Habe seit 2 Tagen mit Swing und AWT angefangen und wollte nun einen Graphen (Node) in ein Panel zeichnen. Aber irgendwie bekomme ich es nicht hin. Bin schon etwas überfragt und mit Galileo Java bekomme ich keine richtigen Infos.
Der ausgeklammerte Button wird gezeichnet, aber nicht mein Graph:
Hier mein Code:
GUI:
AVLNode:
ich hab ein Problem. Habe seit 2 Tagen mit Swing und AWT angefangen und wollte nun einen Graphen (Node) in ein Panel zeichnen. Aber irgendwie bekomme ich es nicht hin. Bin schon etwas überfragt und mit Galileo Java bekomme ich keine richtigen Infos.
Der ausgeklammerte Button wird gezeichnet, aber nicht mein Graph:
Hier mein Code:
GUI:
Java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.*;
public class Gui extends JFrame {
private static JPanel drawPanel;
private JPanel butPanel;
private JTextArea history;
private JTextField textInput;
private JButton butAdd;
private JButton butDel;
private JButton butSearch;
public Gui(int x, int y) {
super("Algorithmik - AVL-Bäume");
setSize(x, y);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setLayout( null );
initWindow();
}
protected void initWindow() {
drawPanel = new JPanel();
butPanel = new JPanel();
history = new JTextArea();
textInput = new JTextField();
butAdd = new JButton("+");
butDel = new JButton("-");
butSearch = new JButton("S");
drawPanel.setBounds(0, 0, 700, 575);
drawPanel.setBackground( Color.WHITE );
drawPanel.setBorder(BorderFactory.createTitledBorder("Graph"));
butPanel.setBounds(700, 0, 200, 300);
butPanel.setBorder(BorderFactory.createTitledBorder("Steuerung"));
Dimension dButSize = new Dimension(28,28);
butAdd.setPreferredSize( dButSize );
butDel.setPreferredSize( dButSize );
butSearch.setPreferredSize( dButSize );
textInput.setColumns(5);
butPanel.add( textInput );
butPanel.add( butAdd );
butPanel.add( butDel );
butPanel.add( butSearch );
history.setBounds(700, 300, 197, 275);
history.setEditable(false);
getContentPane().add( drawPanel );
getContentPane().add( butPanel );
getContentPane().add( history );
}
public static void main(String[] args) {
JFrame win = new Gui(Params.FRAME_WIDTH, Params.FRAME_HEIGHT);
win.add( drawPanel );
win.add( new Button("1") );
AVLNode<Integer> aN = new AVLNode<Integer>(88, new Point(150,150));
// drawPanel.add(new Button("w"));
drawPanel.add( aN );
drawPanel.validate();
win.setVisible( true );
}
}
AVLNode:
Java:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JPanel;
public class AVLNode<T> extends JPanel {
public static final int LEFT_BALANCE_OOB = -2; // out of balance
public static final int LEFT_BALANCE_ONE = -1;
public static final int EVEN = 0;
public static final int RIGHT_BALANCE_ONE = 1;
public static final int RIGHT_BALANCE_OOB = 2;
private T value; // Wert
private AVLNode<T> left; // Linker Teilbaum
private AVLNode<T> right; // Rechter Teilbaum
private int balance; // Balance
private Point current;
public AVLNode(T value, Point current) {
this.value = value;
left = null;
right = null;
balance = 0;
this.current = current;
}
public AVLNode(T value, AVLNode<T> left, AVLNode<T> right, int balance, Point current) {
this.value = value;
this.left = left;
this.right = right;
this.balance = balance;
this.current = current;
}
public T getValue() {
return this.value;
}
public void setValue(T value) {
this.value = value;
}
public AVLNode<T> getLeft() {
return left;
}
public void setLeft(AVLNode<T> node) {
this.left = node;
}
public AVLNode<T> getRight() {
return right;
}
public void setRight(AVLNode<T> node) {
this.right = node;
}
public int getBalance() {
return this.balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public Point getCurrent() {
return current;
}
public void setCurrent( Point current ) {
this.current = current;
}
@Override
protected void paintComponent( Graphics g ) {
String valueStr;
Integer i = (Integer) this.getValue();
valueStr = i.toString();
int x = this.getCurrent().x;
int y = this.getCurrent().y;
g.drawRoundRect( x, y, 52, 35, 10, 10 );
g.drawString(valueStr, x+13, y+13);
}
}