0

I created

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

  public class Painter extends JPanel {

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(new Color(100,100,100));
        g.drawLine(0, 0, 200, 200);
        System.out.println("heir3");
        System.out.println(Main.Array[0]);
        if (Main.Array[0] == 1){
            g.drawOval(100, 100, Main.Radint, Main.Radint);
            System.out.println("heir8");

      } else {
          g.drawString("2", 150, 100);
      }
    }
  } 

This class which should be able to draw something on a Label. At the beginning, it´s just drawing a Line and the 2. But unfortunately I'm not able to call this class a second time to draw the the Oval?

So how am I able to call this class a second time and update or renew the JPanel it's on ?


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Buttons implements ActionListener {

    public JButton Kreis = new JButton();
    public JTextField Text = new JTextField();
    Frame Fr = new Frame();

    public JButton Kreis(){
        Kreis.setText("Kreis");
        Kreis.addActionListener(this);
        return Kreis;
    }
    public JTextField TextField(){
        Text.setPreferredSize(new Dimension(100,20));
        return Text;
    }
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (Kreis == source){
            Main.Radius = Text.getText();
            Main.Radint = Integer.parseInt(Main.Radius);
            Main.Array[0] = 1;
            Main.Array[1] = Main.Radint;
            System.out.println("hier");
            JPanel paint = new Painter();
            Fr.changeFrame(paint);
            System.out.println("hier");

        }
}
}

It´s this one @GojiraDeMonstah

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Why can't you call paintComponent a second time? Also, I don't see where you're changing the value of Main.Array[0], so from the code you posted it looks like it will always call the else statement (g.drawString("2", 150, 150); – Gojira Nov 21 '16 at 15:01
  • I didnt post the other classes because its waaaaaaay to much. But i really change the Main.Array[0] so it should be call the if statement but if i call the paintComponent a second time the program changes nothing. @GojiraDeMonstah – Leleeye Nov 21 '16 at 15:03
  • Are your debug statements printing to the console? – Gojira Nov 21 '16 at 15:15
  • `System.out.println("heir3");` This one yes but when i try to call the Class again they dont print to the console neither @GojiraDeMonstah – Leleeye Nov 21 '16 at 15:17
  • Can you show the code that calls paintComponent(Graphics g) ? – Gojira Nov 21 '16 at 15:23
  • Its a new answer @GojiraDeMonstah – Leleeye Nov 21 '16 at 15:27
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) *"So how am I able to call this class a second time and update or renew the `JPanel` it's on ?"* Call `repaint()` – Andrew Thompson Nov 21 '16 at 21:00

0 Answers0