Hallöchen,
ich möchte gerne ein bißchen etwas Grafisches machen. Momentan tummeln sich 3 Boxen in meinem jPanel die ich mit der Maus anklicken und je nach belieben rumschieben kann.
Mein Programmbaum:
-> hauptFrame.java (dieses beinhaltet den Frame, Panel und einen Button)
-> BoxPanel.java (Hier der Panel der die 3 Boxen erstellt zudem alle MouseEvents enthält)
-> IconBox.java (Beinhaltet die ganzen grafischen Daten einer Box)
Mein Ziel:
Per klick auf dem jButton soll eine neue Box auf dem Panel entstehen.
Das Problem:
Ich weiß nicht wie ich von dem Clickevent des Buttons auf den jPanel zugreifen kann
Meine Versuche, auf eine eigene Methode aus dem jPanel1 zuzugreifen misslangen.
Würde gerne im jPanel eine Methode machen wie "erstelleBox" (die habe ich ja, aber ist halt nur innerhalb des BoxPanels aufrufbar). Hier etwas Code
ich möchte gerne ein bißchen etwas Grafisches machen. Momentan tummeln sich 3 Boxen in meinem jPanel die ich mit der Maus anklicken und je nach belieben rumschieben kann.
Mein Programmbaum:
-> hauptFrame.java (dieses beinhaltet den Frame, Panel und einen Button)
-> BoxPanel.java (Hier der Panel der die 3 Boxen erstellt zudem alle MouseEvents enthält)
-> IconBox.java (Beinhaltet die ganzen grafischen Daten einer Box)
Mein Ziel:
Per klick auf dem jButton soll eine neue Box auf dem Panel entstehen.
Das Problem:
Ich weiß nicht wie ich von dem Clickevent des Buttons auf den jPanel zugreifen kann
Meine Versuche, auf eine eigene Methode aus dem jPanel1 zuzugreifen misslangen.
Würde gerne im jPanel eine Methode machen wie "erstelleBox" (die habe ich ja, aber ist halt nur innerhalb des BoxPanels aufrufbar). Hier etwas Code
Java:
public class hauptFrame extends javax.swing.JFrame {
/** Creates new form hauptFrame */
public hauptFrame() {
initComponents2();
}
private void initComponents2() {
jPanel1 = new BoxPanel();
jToggleButton1 = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(204, 255, 204));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 313, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 298, Short.MAX_VALUE)
);
jToggleButton1.setText("Add");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jToggleButton1)
.addContainerGap(224, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToggleButton1)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(128, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jToggleButton1 = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(204, 255, 204));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 313, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 298, Short.MAX_VALUE)
);
jToggleButton1.setText("Add");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jToggleButton1)
.addContainerGap(224, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToggleButton1)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(128, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("Druck");
jPanel1.getLayout();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new hauptFrame().setVisible(true);
}
});
}
Java:
public class BoxPanel extends JPanel {
private List<IconBox>[] boxVari = new ArrayList[100];
private int anzahlBoxen;
private int aktbox;
private int currentBox = -1;
private Point clickPoint;
private Point position;
public void boxdefinieren(int nummer, int laenge, int breite, int posx, int posy){
boxVari[nummer] = new ArrayList<IconBox>();
IconBox box = new IconBox(laenge, breite);
box.setLocation(posx, posy);
boxVari[nummer].add(box);
}
public void boxdruck(int nummer, MouseEvent e){
for (int i=boxVari[nummer].size()-1; i>=0; i--) {
clickPoint = e.getPoint();
IconBox box = boxVari[nummer].get(i);
if (box.getBounds().contains(clickPoint)) {
currentBox = i;
aktbox = nummer;
position = new Point(box.getBounds().x, box.getBounds().y);
return;
}
}
}
public void boxziehen(int nummer,MouseEvent e){
if ((currentBox!= -1) && (aktbox == nummer)) {
IconBox box = boxVari[nummer].get(currentBox);
Point p = e.getPoint();
box.setLocation(position.x + (p.x - clickPoint.x), position.y + p.y - clickPoint.y);
repaint();
}
}
public BoxPanel() {
this.setBackground(new java.awt.Color(204, 255, 204));
anzahlBoxen = 3;
boxdefinieren(0,50,50,50,50);
boxdefinieren(1,50,50,70,70);
boxdefinieren(2,50,50,80,80);
this.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {
currentBox = -1;
}
public void mousePressed(MouseEvent e) {
for (int x = 0; x < anzahlBoxen; x++){
boxdruck(x,e);
}
}
public void mouseReleased(MouseEvent e) {
currentBox = -1;
}
});
this.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
// wenn die maus gezogen wird
for (int x = 0; x < anzahlBoxen; x++){
boxziehen(x, e);
}
}
public void mouseMoved(MouseEvent e) {}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int x = 0; x < anzahlBoxen; x++){
for (IconBox box: boxVari[x]) box.paintComponent(g);
}
}
}
Java:
class IconBox {
private int x, y;
private int width, height;
private Stroke stroke = new BasicStroke(3);
private Rectangle bounds;
public IconBox(int width, int height) {
this.width = width;
this.height = height;
this.setLocation(0, 0);
}
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 void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setStroke(stroke);
g2.setColor(Color.WHITE);
g2.fillRoundRect(x, y, width, height, 5, 5);
g2.setColor(Color.DARK_GRAY);
g2.drawRoundRect(x, y, width, height, 5, 5);
g2.dispose();
}
}