A
ash34
Gast
Hallo,
ich würde gern ein Element einer Klasse verschieben, die von Jlabel erbt.
Die Klasse nennt sich Box.
Sobald ich auf einen Button (APPEND) drücke, soll ein MouseMotionListener zu einer ganz bestimmten Box hinzugefügt werden, sodass diese verschiebbar ist.
Mein Code kompilliert ohne Fehler, aber die Box lässt sich leider nicht verschieben.
ich würde gern ein Element einer Klasse verschieben, die von Jlabel erbt.
Die Klasse nennt sich Box.
Sobald ich auf einen Button (APPEND) drücke, soll ein MouseMotionListener zu einer ganz bestimmten Box hinzugefügt werden, sodass diese verschiebbar ist.
Mein Code kompilliert ohne Fehler, aber die Box lässt sich leider nicht verschieben.
Java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/*
* ActionListener belonging to the append Button
*/
public class AppendListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
String text = GUI.textField.getText();
int currentBox = BoxListener.currentBox;
// index of clicked box in the boxpanel
if(GUI.gridp.getGridList().size() == BoxPanel.boxChosen){
GUI.gridp.addGrid(GUI.boxp.getBoxList().get(currentBox));
GUI.boxp.getBoxList().remove(currentBox);
GUI.boxp.paint();
}
int size = GUI.gridp.getGridList().size();
int x = 300;
int y = 20;
int width = GUI.gridp.getByteSize(GUI.sliderValue);
GUI.gridp.getGridList().get(size - 1).setValue(text);
GUI.gridp.getGridList().get(size - 1).setWidth(width);
GUI.gridp.getGridList().get(size - 1).setLocation(x,y);
GUI.gridp.getGridList().get(size - 1).addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent arg0) {}
@Override
public void mouseDragged(MouseEvent arg0) {
GUI.gridp.getGridList().get(BoxPanel.boxChosen).setLocation(arg0.getX(), arg0.getY());
}
});
GUI.gridp.paint();
}
}
Java:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import javax.swing.JLabel;
/**
*
* @author florian
* class for creating the boxes
*
*/
public class Box extends JLabel {
/**
*
*/
private static final long serialVersionUID = -2804169159814223929L;
private int x, y;
private int width, height;
private Stroke stroke = new BasicStroke(1); // setting thickness of the strokes
private Rectangle bounds;
private Color color;
private String boxValue = ""; // modifiable text (like 192.168.2.1...)
/**
* Constructor
* @param width
* @param height
* @param x
* @param y
*/
public Box(int width, int height, int x, int y, Color color) {
this.width = width;
this.height = height;
this.color = color;
this.setLocation(x, y);
this.setFont(new Font("Dialog", 0, 10));
}
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
bounds = new Rectangle(x, y, width, height);
}
public Rectangle getBounds() {
return bounds;
}
public Color getColor(Box box){
return this.color;
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void setWidth(int width){
this.width = width;
}
public void setColor(Color color){
this.color = color;
}
public void setValue(String str){
this.boxValue = str;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setStroke(stroke);
g2.setColor(color);
g2.fillRect(x, y, width, height);
g2.setColor(Color.BLACK);
g2.drawRect(x, y, width, height);
g.setFont(this.getFont());
g.drawString(this.getText(), x+2, y+11);
g.drawString(boxValue, x+20, y+35);
g2.dispose();
}
}
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
/**
*
* BoxPanel contains all boxes
* @author florian
*
*/
public class BoxPanel extends JPanel {
private static final long serialVersionUID = 1L;
private List<Box> boxList;
private List<Box> gridList;
private Color color;
static final int boxChosen = 23;
private final int standardWidth = 90;
private final int standardGridWidth = 45;
private final int standardHeight = 40;
private final int fourBitWidth = standardGridWidth;
private final int eightBitWidth = (int) (fourBitWidth*2);
private final int sixteenBitWidth = (int) (eightBitWidth*2);
private final int twentyfourBitWidth = sixteenBitWidth + eightBitWidth;
private final int thirtytwoBitWidth = (int) (sixteenBitWidth*2);
private final int fourtyeightBitWidth = thirtytwoBitWidth + sixteenBitWidth;
private Box prae;
private Box destEth;
private Box sourAddr;
private Box lengType;
private Box vers;
private Box hdrLen;
private Box tos;
/**
* Constructor
*
*/
public BoxPanel() {
this.setPreferredSize(new Dimension(1300, 100));
boxList = new ArrayList<Box>();
gridList = new ArrayList<Box>();
}
public void fillBoxPanel(){
String[] boxText = {"Checksum","Code","Data","HdrLen","Hdr Checksum",
"FOS","Identifier","Total Length",
"Length/Type","Options","Pad","Packet ID",
"Protocol","Präambel","SeqNumr",
"SourAddr(Eth)","SourAddr(IP)","TarAddr(Eth)",
"TarAddr(IP)","TOS","TTL","Type","Vers"};
int n = 0; // horizontal gap between boxes
int m = 40; // vertical gap between boxes
int rowCount = 12; // amount of boxes in one row
int x = 45; // vertical gap for the first box (related to (0,0))
int y = 1; // horizontal gap for the first box (related to (0,0))
int width = standardWidth; // width of a normal box
int height = standardHeight; // height of a normal box
this.color = Color.WHITE; // color of a normal box
// creating boxes, setting color and text to each of the boxes
for(int i=0; i<boxText.length; i++){
if(i < rowCount){
Box box = new Box(width, height, x+n, y, color);
box.setText(boxText[i]);
box.setColor(color);
this.addBox(box);
n = width*(i+1);
}
if(i == rowCount){
n=0;
Box box = new Box(width, height, x+n, y+m, color);
box.setText(boxText[i]);
box.setColor(color);
this.addBox(box);
n = width*(i-11);
}
if(i > rowCount){
Box box = new Box(width, height, x+n, y+m, color);
box.setText(boxText[i]);
box.setColor(color);
this.addBox(box);
n = width*(i-11);
}
}
}
public void fillGrid(){
Color invisibleColor = GUI.frameColor;
int x = 30;
int y = 80;
prae = new Box(fourBitWidth, standardHeight,
x, y, invisibleColor);
destEth = new Box(fourtyeightBitWidth, standardHeight,
x+prae.getWidth(), y, invisibleColor);
sourAddr = new Box(fourtyeightBitWidth, standardHeight,
destEth.getX()+destEth.getWidth(), y, invisibleColor);
lengType = new Box(eightBitWidth, standardHeight,
sourAddr.getX()+sourAddr.getWidth(), y, invisibleColor);
vers = new Box(fourBitWidth, standardHeight,
x, y+standardHeight+2, invisibleColor);
hdrLen = new Box(fourBitWidth, standardHeight,
x+vers.getWidth(), y+standardHeight+2, invisibleColor);
tos = new Box(eightBitWidth, standardHeight,
hdrLen.getX()+hdrLen.getWidth(), y+standardHeight+2, invisibleColor);
Box totLen = new Box(sixteenBitWidth, standardHeight,
tos.getX()+tos.getWidth(), y+standardHeight+2, invisibleColor);
Box packetID = new Box(sixteenBitWidth, standardHeight,
x, y+standardHeight*2+2, invisibleColor);
Box fos = new Box(sixteenBitWidth, standardHeight,
packetID.getX()+packetID.getWidth(), y+standardHeight*2+2, invisibleColor);
Box ttl = new Box(eightBitWidth, standardHeight,
x, y+standardHeight*3+2, invisibleColor);
Box protocol = new Box(eightBitWidth, standardHeight,
ttl.getX()+ttl.getWidth(), y+standardHeight*3+2, invisibleColor);
Box hdrCheck = new Box(sixteenBitWidth, standardHeight,
protocol.getX()+protocol.getWidth(), y+standardHeight*3+2, invisibleColor);
Box sourceAddr = new Box(thirtytwoBitWidth, standardHeight,
x, y+standardHeight*4+2, invisibleColor);
Box tarAddr = new Box(thirtytwoBitWidth, standardHeight,
x, y+standardHeight*5+2, invisibleColor);
Box options = new Box(twentyfourBitWidth, standardHeight,
x, y+standardHeight*6+2, invisibleColor);
Box padding = new Box(eightBitWidth, standardHeight,
options.getX()+options.getWidth(), y+standardHeight*6+2, invisibleColor);
Box type = new Box(eightBitWidth, standardHeight,
x, y+standardHeight*7+4, invisibleColor);
Box code = new Box(eightBitWidth, standardHeight,
type.getX()+type.getWidth(), y+standardHeight*7+4, invisibleColor);
Box checkSum = new Box(sixteenBitWidth, standardHeight,
code.getX()+code.getWidth(), y+standardHeight*7+4, invisibleColor);
Box identifier = new Box(sixteenBitWidth, standardHeight,
x, y+standardHeight*8+4, invisibleColor);
Box seqNumber = new Box(sixteenBitWidth, standardHeight,
identifier.getX()+identifier.getWidth(), y+standardHeight*8+4, invisibleColor);
Box data = new Box(thirtytwoBitWidth, standardHeight,
x, y+standardHeight*9+4, invisibleColor);
this.gridList.add(prae);
this.gridList.add(destEth);
this.gridList.add(sourAddr);
this.gridList.add(lengType);
this.gridList.add(vers);
this.gridList.add(hdrLen);
this.gridList.add(tos);
this.gridList.add(totLen);
this.gridList.add(packetID);
this.gridList.add(fos);
this.gridList.add(ttl);
this.gridList.add(protocol);
this.gridList.add(hdrCheck);
this.gridList.add(sourceAddr);
this.gridList.add(tarAddr);
this.gridList.add(options);
this.gridList.add(padding);
this.gridList.add(type);
this.gridList.add(code);
this.gridList.add(checkSum);
this.gridList.add(identifier);
this.gridList.add(seqNumber);
this.gridList.add(data);
}
public int getByteSize(int n){
int result = fourBitWidth;
switch(n){
case 4: result = fourBitWidth;
break;
case 8: result = eightBitWidth;
break;
case 16: result = sixteenBitWidth;
break;
case 24: result = twentyfourBitWidth;
break;
case 32: result = thirtytwoBitWidth;
break;
case 48: result = fourtyeightBitWidth;
break;
}
return result;
}
public void paint(){
this.repaint();
}
public void addGrid(Box box){
this.gridList.add(box);
}
public void addBox(Box box) {
this.boxList.add(box);
}
public List<Box> getBoxList(){
return this.boxList;
}
public List<Box> getGridList(){
return this.gridList;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Box box: boxList)
box.paintComponent(g);
for(Box box: gridList)
box.paintComponent(g);
}
}