My question is how to display buttons on top of a circle which created with Graphics. My code is :
public class Grafik extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Grafik frame = new Grafik();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Grafik() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 784, 419);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("Click");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fire();
}
});
btnNewButton.setBounds(64, 73, 32, 32);
contentPane.add(btnNewButton);
}
public void fire()
{
JPanel panel = new JPanel(){
#Override
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(20, 20, 400, 400);
g.fillOval(20,20,400,400);
g.setColor(Color.white);
g.drawOval(60, 60, 300, 300);
g.fillOval(60,60,300,300);
}
};
panel.setBounds(10, 27, 383, 233);
setContentPane(panel);
JButton btn = new JButton();
btn.setIcon(new ImageIcon("C:\\Users\\nuria\\Desktop\\icon1.png"));
btn.setBounds(75, 76, 32, 32);
panel.add(btn);
}
}
This code is a little part of my project. I have a lot of panels in my project and my fire method isn't called firstly. I mean the fire method is called by sixth panel. I have to draw two circles with buttons. These buttons should be between two circles. But i cant display buttons directly. When i hover the buttons, they show. I want to show buttons instantly when the sixth panel opens.
I have two buttons above. When the first button is clicked, another panel should open and show circles(which created with Graphics) and another button(with a icon) between them.
JPanel panel = new JPanel(){
#Override
public void paint(Graphics g)
{
Don't override paint(). Custom painting is done by overriding paintComponent() and don't forget to invoke super.paintComponent() as the first statement.
The problem with your current code is that by overriding paint you have changed the default painting logic of the panel and the child components are never painted.
Take a look at the section from the Swing tutorial on Custom Painting for more information and examples, especially the section on A Closer Look at the Paint Mechanism.
Related
I am making a custom title bar in swing, and when I use a letter "x" for the close button, it works fine. But when I replace it with an icon, the whole title bar disappears, until I hover the close button, which makes only the close button appear. Here is a snippet of my code:
// Create title bar
JPanel titleBar = new JPanel();
titleBar.setBackground(new Color(0x343434));
titleBar.setSize(screenSize.width, 36);
JButton closeButton = new JButton();
closeButton.setBackground(new Color(0, 0, 0, 0));
closeButton.setIcon(new ImageIcon(ImageIO.read(new File("src/close.png")).getScaledInstance(16, 16, Image.SCALE_SMOOTH)));
closeButton.setSize(50, 36);
closeButton.setLocation(screenSize.width - 50, 0);
closeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
}
});
closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(MouseEvent e) {
closeButton.setBackground(Color.RED);
}
public void mouseExited(MouseEvent e) {
closeButton.setBackground(new Color(0x343434));
}
});
closeButton.setBorder(null);
closeButton.setFocusPainted(false);
titleBar.add(closeButton);
window.add(titleBar);
Edit: it works sometimes now, but sometimes it doesn't. Here is the new code:
public class Main {
public static void main(String[] args) throws IOException {
// Get screen size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Create window
JFrame window = new JFrame();
//Get taskbar size
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(window.getGraphicsConfiguration());
int taskBarHeight = screenInsets.bottom;
// Configure window
window.setSize(screenSize.width, screenSize.height - taskBarHeight);
window.getContentPane().setBackground(new Color(0x232323));
window.setUndecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
// Create title bar
JPanel titleBar = new JPanel();
titleBar.setBackground(new Color(0x343434));
titleBar.setSize(screenSize.width, 36);
JButton closeButton = new JButton();
closeButton.setBackground(new Color(0x343434));
Image closeImg = ImageIO.read(new File("src/close.png")).getScaledInstance(16, 16, 4);
closeButton.setIcon(new ImageIcon(closeImg));
closeButton.setSize(50, 36);
closeButton.setLocation(screenSize.width - 50, 0);
closeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
}
});
closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(MouseEvent e) {
closeButton.setBackground(Color.RED);
}
public void mouseExited(MouseEvent e) {
closeButton.setBackground(new Color(0x343434));
}
});
closeButton.setBorder(null);
closeButton.setFocusPainted(false);
titleBar.add(closeButton);
window.add(titleBar);
}
}
closeButton.setBackground(new Color(0, 0, 0, 0));
Don't use a transparent background. Swing doesn't paint components properly when you use transparency.
For full transparency you simply use:
closeButton.setOpaque( false );
Swing was designed to be used with layout managers:
closeButton.setSize(50, 36);
closeButton.setLocation(screenSize.width - 50, 0);
The above code will do nothing (so get rid of it). The default layout manager of a JPanel is the FlowLayout. Once the frame is visible the layout manager will be invoked and the size/location will be reset by the layout manager.
So let the layout manager do it's job. In your case you can use:
//JPanel titleBar = new JPanel();
JPanel titleBar = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
Now any components you add to the panel will be aligned to the right edge.
window.add(titleBar);
The default layout manager for a JFrame is the BorderLayout. When you don't specify a constraint, the component is added to the CENTER.
For a title bar you would want that added to the top of the Frame. So you should be using:
//window.add(titleBar);
window.add(titleBar, BorderLayout.PAGE_START);
Read the Swing tutorial on Layout Managers for more information and working examples for both the FlowLayout and the BorderLayout.
Also, Swing components need to be added to the frame BEFORE the frame is made visible. The layout manager is initially invoked when the frame is "packed" or made "visible".
This question is an extension of java- repaint() method is misbehaving?
(Reading it, is optional)
I am working on a Music Player
I am using a JSlider as seek bar and using a JLabel to draw text on screen, such as song name.
I am new to Graphics2D
Here's the minimized code:
public class JSliderDemo extends JFrame
{
JLabel label;
JSlider seek = new JSlider();
int y = 10;
public JSliderDemo()
{
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
createWindow();
setVisible(true);
startThread();
}
public void createWindow()
{
JPanel panel = new JPanel(new BorderLayout());
panel.setOpaque(true);
panel.setBackground(Color.BLUE);
panel.setBorder(new LineBorder(Color.YELLOW));
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
label = new Component();
label.setSize(300, 300);
createSlider();
layeredPane.add(seek, new Integer(50));
layeredPane.add(label, new Integer(100));
panel.add(layeredPane);
add(panel);
}
protected void createSlider()
{
seek.setUI(new SeekBar(seek, 300, 10, new Dimension(20, 20), 5,
Color.DARK_GRAY, Color.RED, Color.RED));
seek.setOrientation(JProgressBar.HORIZONTAL);
seek.setOpaque(false);
seek.setLocation(10, 50);
seek.setSize(300, 20);
seek.setMajorTickSpacing(0);
seek.setMinorTickSpacing(0);
seek.setMinimum(0);
seek.setMaximum(1000);
seek.setBorder(new MatteBorder(5, 5, 5, 5, Color.CYAN));
}
protected void startThread()
{
Thread thread = new Thread(new Runnable(){
#Override
public void run()
{
try
{
while(true)
{
if(y == label.getHeight()){y = 1;}
label.repaint();
y += 1;
Thread.sleep(100);
}
}
catch(Exception ex){}
}
});
thread.start();
}
protected class Component extends JLabel
{
#Override
public void paintComponent(Graphics g)
{
Graphics2D gr = (Graphics2D) g;
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gr.setColor(Color.RED);
gr.setFont(new Font("Calibri", Font.PLAIN, 16));
gr.drawString("Song Name", 50, y);
gr.dispose();
}
}
public static void main(String[] args)
{
new JSliderDemo();
}
}
The problem is, when I call repaint() for JLabel it automatically repaints JSlider with it even though JSlider is not included in JLabel.
Output :
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted.........
Now if I remove label.repaint() from the Thread, then the JSlider is not re-painted.
Output:
Slider re-painted
Slider re-painted
Is the repaint() method supposed to work like this?
In my last question, I was told to use Layout Manager and when I did use GridLayout just for checking if it's the solution, then it worked!
Only JLabel was repainted.
But I want to overlap JLabel on JSlider, so I thought of using JLayeredPane. And now, the problem is back.
How can I solve this?
Bottom Line : How can I overlap JLabel on JSlider without leading to repaint() method misbehave ?
OR
Does the repaint() method work like this?
As was already mentioned in the comments, the reason for your JSlider being repainted is that it has overlapping bounds with the JLabel. Even though your label doesn't paint over the area of the slider swing will still mark the overlapping area as dirty (i.e. the overlapping part of the slider will need to be repainted) because swing doesn't know that you are only painting in one part of the component.
To reduce the amount of repaints you will need to make the size of your JLabel smaller. Preferably only as large as it needs to be by invoking its getPreferredSize() method. You'll then be able to move the text by moving the location of the label.
Also you shouldn't be doing updates to the gui in a plain Thread. Use javax.swing.Timer instead. It ensures that all updates to the gui happen on the swing event thread, which is where they should be made.
After making these adjustments to your code the slider is only repainted while the label is actually visually over the slider.
public class JSliderDemo extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(JSliderDemo::new);
}
private final JLabel label = new CustomLabel();
public JSliderDemo() {
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
createWindow();
setVisible(true);
startTimer();
}
public void createWindow() {
JPanel panel = new JPanel(new BorderLayout());
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
label.setLocation(0, 0);
label.setBorder(new LineBorder(Color.RED));
label.setSize(label.getPreferredSize());
layeredPane.add(createSlider(), Integer.valueOf(50));
layeredPane.add(label, Integer.valueOf(100));
panel.add(layeredPane);
setContentPane(panel);
}
protected JSlider createSlider() {
JSlider seek = new CustomSlider();
seek.setOrientation(JProgressBar.HORIZONTAL);
seek.setOpaque(false);
seek.setLocation(10, 50);
seek.setSize(300, 20);
seek.setMajorTickSpacing(0);
seek.setMinorTickSpacing(0);
seek.setMinimum(0);
seek.setMaximum(1000);
seek.setBorder(new LineBorder(Color.BLUE));
return seek;
}
private void startTimer() {
new Timer(100, e -> {
int y = label.getY();
int maxY = label.getParent().getHeight();
if (y == maxY) {
y = -label.getHeight();
}
label.setLocation(label.getX(), y + 1);
label.repaint();
}).start();
}
private static class CustomLabel extends JLabel {
protected CustomLabel() {
setFont(new Font("Calibri", Font.PLAIN, 16));
setText("Song Name");
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Painting Label");
}
}
protected static class CustomSlider extends JSlider {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Painting Slider");
}
}
}
I am trying to make a JFrame display a different JPanel when a specific tab is selected. I have tried adding code to make it add the new panel based on which tab index is selected.
Where am I going wrong with this? What do I need to add to make it work? Thanks.
EDIT
Here is my solved SSCCE:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
public class MainPanel {
private static JTabbedPane tabbedPane = new JTabbedPane();
private static JFrame frame = new JFrame("Testing");
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGUI);
}
protected static void createAndShowGUI()
{
DrawGraphics drawGraphics = new DrawGraphics();
DrawDifferentGraphics drawDifferentGraphics = new DrawDifferentGraphics();
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tabbedPane, BorderLayout.WEST);
tabbedPane.addTab("CFG", null);
tabbedPane.addTab("CNX", null);
frame.add(drawGraphics);
tabbedPane.addChangeListener(e -> {
if (tabbedPane.getSelectedIndex() == 0) {
frame.remove(drawDifferentGraphics);
frame.add(drawGraphics);
frame.validate();
frame.repaint();
}
if (tabbedPane.getSelectedIndex() == 1) {
frame.remove(drawGraphics);
frame.add(drawDifferentGraphics);
frame.validate();
frame.repaint();
}});
frame.setLocationByPlatform(true);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
class DrawGraphics extends JPanel {
private ArrayList<Shape> shapes = new ArrayList<Shape>();
public DrawGraphics() {
setLayout(new BorderLayout());
shapes.add(new Ellipse2D.Double(10, 10, 20, 20));
shapes.add(new Ellipse2D.Double(10, 30, 20, 20));
shapes.add(new Ellipse2D.Double(10, 50, 20, 20));
shapes.add(new Ellipse2D.Double(10, 70, 20, 20));
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
g2d.setColor(Color.BLUE);
shapes.forEach(g2d::fill);
g2d.dispose();
}
}
class DrawDifferentGraphics extends JPanel {
private ArrayList<Shape> shapes = new ArrayList<Shape>();
public DrawDifferentGraphics() {
setLayout(new BorderLayout());
shapes.add(new Rectangle2D.Double(10, 10, 10, 10));
shapes.add(new Rectangle2D.Double(10, 30, 10, 10));
shapes.add(new Rectangle2D.Double(10, 50, 10, 10));
shapes.add(new Rectangle2D.Double(10, 70, 10, 10));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
g2d.setColor(Color.RED);
shapes.forEach(g2d::fill);
g2d.dispose();
}
}
I want to display the graphics on the panel next to the tabbedPane.
Read the section from the Swing tutorial on How to Write a ChangeListener.
You will be notified when a tab has been clicked. You then get the selected tab and add the panel to the frame.
So basically your if (tabbedPane.getSelectedIndex() == 0) logic would be moved to the ChangeListener.
Or instead of having a bunch of "if statement" you could have a Map of Integer/JPanel values. Then you just get the index and get the panel from the Map.
Once you add the panel to the frame you then need to revalidate() and repaint() the frame.
Edit:
Actually the above suggestion is not complete. You can't just keep adding panels to the frame. The CENTER area of the BorderLayout should only contain a single component, otherwise you can get painting problems.
This can be demonstrated by clicking on the unselected tab, and then resize the frame. The original panel will be displayed.
You need to do one of the following:
Use a CardLayout (read the tutorial if you haven't used layout before) on a penel in the CENTER of the BordreLayout. So in this case the panel using the CardLayout is the only component in the CENTER and then it manages the panel that is displayed in the CardLayout. So your ChangeListener would need to identify the card to be displayed. You could set the card identifier to be the text of the selected tab. So
Remove the current panel BEFORE adding the new panel. In this case there is only a single panel in the CENTER so painting is as expected.
I am working on a little menu program with clickable buttons and an image that changes based on button clicks. If I click a button I get a shadow of the button at the bottom where I change the JLabel text. I cannot figure it out for the life of me. Any advice would be greatly appreciated. Visuals below...thanks
public class SampleGUI
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
System.out.println("Created GUI on EDT? " +
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Robert's VICI Prototype");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}
class MyPanel extends JPanel
{
// Fields
Image imageDisplayed;
JLabel status;
// Methods
public MyPanel()
{
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setLayout(null);
JLabel title = new JLabel("CS380 TEAM 5 VICI Prototype");
title.setFont(new Font("Tahoma", Font.BOLD, 20));
title.setBounds(425, 10, 400, 40);
add(title);
status = new JLabel("Please click an option above.");
status.setFont(new Font("Tahoma", Font.BOLD, 14));
status.setBounds(425, 740, 400, 40);
add(status);
JButton choice1 = new JButton("Search Class");
choice1.setBounds(50, 50, 150, 40);
add(choice1);
JButton choice2 = new JButton("Add Class");
choice2.setBounds(225, 50, 150, 40);
add(choice2);
JButton choice3 = new JButton("Drop Class");
choice3.setBounds(400, 50, 150, 40);
add(choice3);
JButton choice4 = new JButton("Verify Reg Hold");
choice4.setBounds(575, 50, 150, 40);
add(choice4);
JButton choice5 = new JButton("Verify Reg Date");
choice5.setBounds(750, 50, 150, 40);
add(choice5);
JButton choice6 = new JButton("Schedule Advisor");
choice6.setBounds(925, 50, 150, 40);
add(choice6);
choice6.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Schedule Advisor button pressed.");
status.setText("Choose a date.");
imageDisplayed = new ImageIcon("C:\\Temp\\sa01.jpg").getImage();
}
});
JButton exit = new JButton("EXIT");
exit.setBounds(940, 750, 150, 40);
add(exit);
exit.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
imageDisplayed = new ImageIcon("C:\\Temp\\main.jpg").getImage();
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(1100, 800);
}
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
}
You've broken the paint chain...
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The first thing you should be calling is super.paintComponent(g)
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The Graphics context is shared resource, meaning that the Graphics context you get has also been used to paint the other components that have also been painted. One of the jobs of paintComponent is to clear the Graphics context ready for the component to be painted (fill the background)
See Painting in AWT and Swing and Performing Custom Painting for more details
You should, also, avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See Why is it frowned upon to use a null layout in SWING? for more details...
Before you go all defensive over getting your screen laid out just nicely, you shouldn't be doing it this way. You controls should be laid out on separate containers (using appropriate layout managers) and you "drawing surface" should be it's own component. See Laying Out Components Within a Container for more details...
If you're still not convicned, take a look at Why setting setPreferredSize() on JFrame is bad? and java expandable JDialog for examples of differences between Mac and Windows
I got a JPanel where a triangle is painted.
When someone clicks on a button the triangle should be repainted with new parameters. The problem is the old triangle is still there and the new one is messed up with part of the textfield underneath.
public class Vermessung {
private static void eingabe(){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(screen.height/2, screen.height/4*3);
JPanel jp = new JPanel();
jp.setLayout(new BoxLayout(jp, BoxLayout.PAGE_AXIS));
//Eingabebereich
JPanel eingabebereich = new JPanel(new GridLayout(3, 1));
JPanel abc = new JPanel(new GridLayout(4, 2));
abc.add(new JLabel("Strecke"));
abc.add(new JLabel("Gemessener Wert in [m]"));
abc.add(new JLabel("a:"));
abc.add(tfa);
abc.add(new JLabel("b:"));
abc.add(tfb);
abc.add(new JLabel("c:"));
abc.add(tfc);
//AusgabeBereich
JPanel ausgabe = new JPanel(new GridLayout(2, 3));
ausgabe.add(new JLabel("p [m]"));
ausgabe.add(new JLabel("q [m]"));
ausgabe.add(new JLabel("h [m]"));
ausgabe.add(P);
ausgabe.add(Q);
ausgabe.add(H);
P.setEditable(false);
Q.setEditable(false);
H.setEditable(false);
//Buttons mit Listenern
JPanel buttons = new JPanel(new FlowLayout());
JButton ok = new JButton("OK");
JButton cancel = new JButton("beenden");
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//Textfelder auslesen
TextfelderAuslesen();
//bei gueltiger Eingabe Höhe Berechnen
if(berechenbar){
berechnungPQ();
berechnungH();
P.setText(String.valueOf(p));
Q.setText(String.valueOf(q));
H.setText(String.valueOf(h));
sketch.update(vec);
sketch.repaint();
}else{
}
}
};
ok.addActionListener(al);
ActionListener beenden = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
};
cancel.addActionListener(beenden);
buttons.add(ok);
buttons.add(cancel);
//Fensteraufbau
sketch.setPreferredSize(new Dimension(screen.height/2, screen.height/2));
jp.add(sketch);
eingabebereich.add(abc);
eingabebereich.add(ausgabe);
eingabebereich.add(buttons);
eingabebereich.setPreferredSize(new Dimension(screen.height/4, screen.height/4));
jp.add(eingabebereich);
f.add(jp);
f.setVisible(true);
}
}
public class Zeichnung extends JPanel{
public void paint(Graphics g){
zeichneDreieck(g);
}
private void zeichneDreieck(Graphics g){
berechneLaengen();
g.setColor(new Color(255,0,0));
g.drawLine(30, 30, ca, 30);
g.drawString("c", ca/2, 20);
g.drawLine(ca, 30, qa, ha);
g.drawString("a", (ca-pa/2), ha/2);
g.drawLine(qa, ha, 30, 30);
g.drawString("b", (qa/2), ha/2);
g.setColor(new Color(0,0,0));
g.drawLine(qa, ha, qa, 30);
g.drawString("h", qa+5, ha/2);
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
zeichneDreieck(g);
}
For JPanel, override paintComponent() instead. As the first line, call the super method.
Call
yourFrame.invalidate();
yourFrame.validate();
yourFrame.repaint();
See more information about invalidate() and validate() here.
Per the documentation: The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
Create a new method clear(Graphics g) in your Zeichnung class and clear all the lines from it by adding the body as:
super.paintComponent(g);
this.removeAll();
this.updateUI();
Call this method in your zeichneDreieck method at the first line.