import java.applet.*;
import java.util.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.Icon.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import com.borland.jbcl.layout.*;
import java.awt.event.MouseEvent;
/**
*
Überschrift: </p>
*
Beschreibung: </p>
*
Copyright: Copyright (c) 2004</p>
*
Organisation: </p>
* @author unbekannt
* @version 1.0
*/
public class DD_test
extends JApplet
implements Runnable {
private boolean isStandalone = false;
//Parameterwert holen
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
Thread treewalkThread;
int pressedX;
int pressedY;
DaDLabel jLabel_Test;
JPanel jPanelMap = new JPanel();
JButton jButton_createObject = new JButton();
JButton jButton_deleteObject = new JButton();
//Das Applet konstruieren
public DD_test() {
treewalkThread = new Thread(this);
setUpDisplay();
}
//Das Applet initialisieren
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Initialisierung der Komponenten
private void jbInit() throws Exception {
jPanelMap.setLayout(null);
jPanelMap.setBackground(Color.white);
jPanelMap.setAlignmentX( (float) 0.5);
jButton_createObject.setBounds(new Rectangle(60, 271, 100, 20));
jButton_createObject.setText("neues Objekt");
jButton_createObject.addActionListener(new DD_test_jButton_createObject_actionAdapter(this));
jButton_deleteObject.setBounds(new Rectangle(232, 271, 98, 19));
jButton_deleteObject.setText("löschen");
jButton_deleteObject.addActionListener(new DD_test_jButton_deleteObject_actionAdapter(this));
this.getContentPane().add(jPanelMap, BorderLayout.CENTER);
jPanelMap.add(jButton_createObject, null);
jPanelMap.add(jButton_deleteObject, null);
}
//Das Applet starten
public void start() {
}
//Das Applet anhalten
public void stop() {
}
//Das Applet löschen
public void destroy() {
}
//Applet-Information holen
public String getAppletInfo() {
return "Applet-Information";
}
//Parameter-Infos holen
public String[][] getParameterInfo() {
return null;
}
private void setUpDisplay() {
this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED));
// set fonts to smaller-than-normal size, for compaction!
UIManager manager = new UIManager();
FontUIResource appFont = new FontUIResource("SansSerif", Font.PLAIN, 10);
UIDefaults defaults = manager.getLookAndFeelDefaults();
Enumeration keys = defaults.keys();
while (keys.hasMoreElements()) {
String nextKey = (String) (keys.nextElement());
if ( (nextKey.indexOf("font") > -1) || (nextKey.indexOf("Font") > -1)) {
manager.put(nextKey, appFont);
}
}
}
public void run() {
}
public void paintComponent(Graphics g) {
super.paint(g);
Component tempCompo1, tempCompo2;
for (int i = 0; i < jPanelMap.countComponents(); i++) {
tempCompo1 = jPanelMap.getComponent(i);
if (tempCompo1 instanceof DaDLabel) {
for (int j = 0; j < jPanelMap.countComponents(); j++) {
tempCompo2 = jPanelMap.getComponent(j);
if (tempCompo2 instanceof DaDLabel) {
if (tempCompo1 != tempCompo2) {
g.drawLine(tempCompo1.getX() + tempCompo1.size().width / 2,
tempCompo1.getY() + tempCompo1.size().height / 2,
tempCompo2.getX() + tempCompo2.size().width / 2,
tempCompo2.getY() + tempCompo2.size().height / 2);
}
}
}
}
}
// g.drawLine(50,50,100,100);
}
void jButton_createObject_actionPerformed(ActionEvent e) {
jLabel_Test = new DaDLabel(50,50);
jPanelMap.add(jLabel_Test, null);
Integer temp = new Integer(jPanelMap.countComponents()-2);
jButton_createObject.setText(temp.toString());
Graphics g;
g = jPanelMap.getGraphics();
jPanelMap.update(g);
Component tempCompo1, tempCompo2;
for (int i = 0; i < jPanelMap.countComponents(); i++) {
tempCompo1 = jPanelMap.getComponent(i);
if (tempCompo1 instanceof DaDLabel) {
for (int j = 0; j < jPanelMap.countComponents(); j++) {
tempCompo2 = jPanelMap.getComponent(j);
if (tempCompo2 instanceof DaDLabel) {
if (tempCompo1 != tempCompo2) {
g.drawLine(tempCompo1.getX() + tempCompo1.size().width / 2,
tempCompo1.getY() + tempCompo1.size().height / 2,
tempCompo2.getX() + tempCompo2.size().width / 2,
tempCompo2.getY() + tempCompo2.size().height / 2);
}
}
}
}
}
// g.drawLine(50,50,100,100);
}
void jButton_deleteObject_actionPerformed(ActionEvent e) {
jLabel_Test = (DaDLabel) jPanelMap.findComponentAt(50,50);
jPanelMap.remove(jLabel_Test);
jPanelMap.repaint();
}
private class DaDLabel
extends JLabel
implements MouseListener, MouseMotionListener {
private int pressedX;
private int pressedY;
private ImageIcon Icon;
public DaDLabel(int X, int Y) {
super();
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.setIcon(null);
Icon = new ImageIcon("u:/projekte/Drag&Drop/classes/DE.gif");
this.setAlignmentX( (float) 0.0);
this.setAlignmentY( (float) 0.5);
this.setHorizontalAlignment(SwingConstants.LEADING);
this.setHorizontalTextPosition(SwingConstants.TRAILING);
this.setIcon(Icon);
this.setBounds(new Rectangle(X, Y, 32, 32));
}
public void mousePressed(MouseEvent e) {
if (!e.isMetaDown() && !e.isAltDown()) {
pressedX = e.getX();
pressedY = e.getY();
}
}
public void mouseDragged(MouseEvent e) {
int newX, newY;
if (!e.isMetaDown() && !e.isAltDown()) {
newX = this.getLocation().x - pressedX + e.getX();
newY = this.getLocation().y - pressedY + e.getY();
if (0 > newX) {
newX = 0;
}
if (jPanelMap.getWidth() - this.getWidth() < newX) {
newX = jPanelMap.getWidth() - this.getWidth();
}
if (0 > newY) {
newY = 0;
}
if (jPanelMap.getHeight() - this.getHeight() < newY) {
newY = jPanelMap.getHeight() - this.getHeight();
}
this.setLocation(newX, newY);
Graphics g;
g = jPanelMap.getGraphics();
jPanelMap.update(g);
Component tempCompo1, tempCompo2;
for (int i = 0; i < jPanelMap.countComponents(); i++) {
tempCompo1 = jPanelMap.getComponent(i);
if (tempCompo1 instanceof DaDLabel) {
for (int j = 0; j < jPanelMap.countComponents(); j++) {
tempCompo2 = jPanelMap.getComponent(j);
if (tempCompo2 instanceof DaDLabel) {
if (tempCompo1 != tempCompo2) {
g.drawLine(tempCompo1.getX() + tempCompo1.size().width / 2,
tempCompo1.getY() + tempCompo1.size().height / 2,
tempCompo2.getX() + tempCompo2.size().width / 2,
tempCompo2.getY() + tempCompo2.size().height / 2);
}
}
}
}
}
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}
}
class DD_test_jButton_createObject_actionAdapter implements java.awt.event.ActionListener {
DD_test adaptee;
DD_test_jButton_createObject_actionAdapter(DD_test adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton_createObject_actionPerformed(e);
}
}
class DD_test_jButton_deleteObject_actionAdapter implements java.awt.event.ActionListener {
DD_test adaptee;
DD_test_jButton_deleteObject_actionAdapter(DD_test adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton_deleteObject_actionPerformed(e);
}
}