为什么已经绘制的线条没有消失?

时间:2017-06-03 13:17:16

标签: java swing jframe components draw

我无法弄清楚为什么已经绘制的组件不会消失,因为我更新了Component的位置。 See picture here

只有底部正方形应该是可见的! 这是我的自定义组件类(抱歉是德语)

package de.jakob.java.sitzplan.elements;

import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

import de.jakob.java.sitzplan.utils.DrawUtils;

public class JTisch extends JComponent {

    private static final long serialVersionUID = 1L;


    JFrame parent;

    public int posX;
    public int posY;

    public int rotation;

    public Person personlinks;
    public Person personrechts;

    public int width = 40;
    public int height = 30;

    public JTisch(int posX, int posY, int rotation, Person personlinks, Person personrechts, JFrame parent) {
        super();

        this.posX = posX;
        this.posY = posY;

        this.setLocation(this.posX, this.posY);
        this.setSize(width, height);

        this.rotation = rotation;

        this.personlinks = personlinks;
        this.personrechts = personrechts;

        this.parent = parent;

    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    @Override
    public void paintComponent(Graphics g) {

        try {
            DrawUtils.drawPosLineWithStroke(g, this.posX             , this.posY              , this.posX + this.width, this.posY              );
            DrawUtils.drawPosLineWithStroke(g, this.posX             , this.posY              , this.posX             , this.posY + this.height);
            DrawUtils.drawPosLineWithStroke(g, this.posX + this.width, this.posY              , this.posX + this.width, this.posY + this.height);
            DrawUtils.drawPosLineWithStroke(g, this.posX             , this.posY + this.height, this.posX + this.width, this.posY + this.height);
        } catch (Exception e) {

        }


    }


}

这是我的DrawUtils课程:

package de.jakob.java.sitzplan.utils;

import java.awt.Graphics;

public class DrawUtils {


    public static void drawRelativeLineWithStroke(Graphics g, int x, int y, int width, int height) throws Exception{
        g.drawLine(x, y, x + width, y + height);

    }

    public static void drawPosLineWithStroke(Graphics g, int x, int y, int xend, int yend) throws Exception  {
        g.drawLine(x, y, xend, yend);
    }



}

这里是漫长而复杂的框架......(不要介意已知的对象,只关注使用onUpdate(每50毫米调用一次)鼠标和JTisch的鼠标更新。

package de.jakob.java.sitzplan.frames;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.jakob.java.sitzplan.core.Core;
import de.jakob.java.sitzplan.elements.JDoppeltisch;
import de.jakob.java.sitzplan.elements.JTisch;
import de.jakob.java.sitzplan.tickthread.FrameUpdate;
import de.jakob.java.sitzplan.tickthread.TickHandler;
import java.awt.event.MouseMotionAdapter;

@SuppressWarnings("serial")
public class SitzplanErstellen extends JFrame implements FrameUpdate {

    public JFrame frame;
    public JFrame parent;

    public boolean mousePressed = false;

    public int mouseX = 0;
    public int mouseY = 0;

    public boolean hovered;

    ArrayList<JTisch> tische = new ArrayList<JTisch>();
    ArrayList<JDoppeltisch> doppeltische = new ArrayList<JDoppeltisch>();
    // ArrayList<Tafel> tafeln = new ArrayList<Tafel>();

    @Override
    public void onUpdate() {
        for (JTisch t : tische) {

            if (mouseX >= t.posX && mouseX <= (t.posX + t.width)) {
                System.out.println("hi");
                if (mouseY >= t.posY + t.height && mouseY <= (t.posY + t.height * 2)) {
                    if (mousePressed) {
                        t.posX = mouseX;
                        t.posY = mouseY;
                    }
                }
            }
            t.paintComponent(frame.getContentPane().getGraphics());
        }
        for (JDoppeltisch dt : doppeltische) {
            dt.paintComponent(frame.getContentPane().getGraphics());
        }


    }

    /**
     * Create the application.
     */
    public SitzplanErstellen(boolean b, JFrame parent) {
        this.parent = parent;
        TickHandler.registerClass(this);
        initialize();
        frame.setVisible(b);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();

        frame.getContentPane().setBackground(Color.GRAY);
        frame.setBounds(Core.rect);

        frame.getContentPane().setLayout(null);

        frame.setIconImage(
                new ImageIcon(SitzplanErstellen.class.getResource("/de/jakob/java/sitzplan/core/icon.png")).getImage());

        JLabel lblKomponenten = new JLabel("Komponenten");
        lblKomponenten.setFont(new Font("SansSerif", Font.BOLD, 11));
        lblKomponenten.setBounds(13, 61, 78, 15);
        frame.getContentPane().add(lblKomponenten);

        JPanel panel = new JPanel();
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                hovered = true;
            }
        });
        panel.setBackground(Color.WHITE);
        panel.setBounds(136, 0, 1153, 635);
        frame.getContentPane().add(panel);

        JLabel lblSchler = new JLabel("Sch\u00FCler");
        lblSchler.setFont(new Font("SansSerif", Font.BOLD, 11));
        lblSchler.setBounds(13, 201, 78, 15);
        frame.getContentPane().add(lblSchler);

        JButton btnTafel = new JButton("Tafel");
        btnTafel.setBounds(13, 121, 97, 23);
        frame.getContentPane().add(btnTafel);
        btnTafel.setBackground(Color.LIGHT_GRAY);
        btnTafel.setToolTipText("Einen neue Tafel zum Klassenraum hinzuf\u00FCgen");

        JButton btnDoppeltisch = new JButton("Doppeltisch");
        btnDoppeltisch.setBounds(13, 87, 97, 23);
        frame.getContentPane().add(btnDoppeltisch);
        btnDoppeltisch.setBackground(Color.LIGHT_GRAY);
        btnDoppeltisch.setToolTipText("Einen neuen Doppeltisch zum Klassenraum hinzuf\u00FCgen");

        JButton btnEinzeltisch = new JButton("Einzeltisch");
        btnEinzeltisch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JTisch jt = new JTisch(200, 100, 0, null, null, frame);

                System.out.println("hi2");
                tische.add(jt);
            }
        });
        btnEinzeltisch.setBounds(13, 153, 97, 23);
        frame.getContentPane().add(btnEinzeltisch);
        btnEinzeltisch.setBackground(Color.LIGHT_GRAY);
        btnEinzeltisch.setToolTipText("Einen neuen Einzeltisch zum Klassenraum hinzuf\u00FCgen");

        JButton btnNewButton = new JButton("<html>Schüler<br>eintragen</html>");
        btnNewButton.setBounds(13, 227, 97, 37);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                Core.setSchuelerListeVisible(true, frame);
            }
        });
        btnNewButton.setBackground(Color.LIGHT_GRAY);

        JButton button = new JButton("<html>Sch\u00FCler<br>Wünsche<br>eintragen</html>");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        button.setBackground(Color.LIGHT_GRAY);
        button.setBounds(12, 275, 98, 51);
        frame.getContentPane().add(button);

        JButton button_1 = new JButton("Back");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                parent.setVisible(true);
                frame.dispose();
            }
        });
        button_1.setIcon(new ImageIcon(SitzplanErstellen.class.getResource("/de/jakob/java/sitzplan/core/pfeil.png")));
        button_1.setFont(new Font("Dialog", Font.BOLD, 12));
        button_1.setBackground(Color.LIGHT_GRAY);
        button_1.setBounds(0, 0, 98, 26);
        frame.getContentPane().add(button_1);
        btnDoppeltisch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doppeltische.add(new JDoppeltisch(100, 50, 0, null, null, frame));
            }
        });

        for (Component c : frame.getContentPane().getComponents()) {
            c.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    mousePressed = true;
                    System.out.println("press");
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    mousePressed = false;
                    System.out.println("not press");
                }
            });

            c.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                    System.out.println(mouseX);
                    mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                    System.out.println(mouseY);
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                    System.out.println(mouseX);
                    mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                    System.out.println(mouseY);
                }
            });
        }

        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mousePressed = true;
                System.out.println("press");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                mousePressed = false;
                System.out.println("not press");
            }
        });

        frame.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                System.out.println(mouseX);
                mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                System.out.println(mouseY);
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                System.out.println(mouseX);
                mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                System.out.println(mouseY);
            }
        });
    }
}

0 个答案:

没有答案