I created this class that paints a Jframe and some methods to add Ovals.
public class GUI extends JFrame
{
ImageIcon icon;
JFrame f = new JFrame();
JPanel p = new JPanel();
JLabel j;
BufferedImage img;
public GUI() {
frameErzeugen();
StartPosition();
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public void frameErzeugen()
{
File file = new File("SpielfeldwoR.jpg");
try {
this.img = ImageIO.read(file);
} catch (IOException ex) {
ex.printStackTrace();
}
icon = new ImageIcon(img);
JLabel j = new JLabel(icon);
Container cp = getContentPane();
cp.setLayout (new BorderLayout());
p.setBounds(0, 0, 1920, 1080);
p.setLayout(new BorderLayout());
p.setVisible(true);
cp.add(j);
cp.setVisible(true);
f.pack();
f.setVisible(true);
f.setContentPane(cp);
}
public void ZeichneFigur(String position) {
Graphics g = getGraphics();
String[] parts = position.split("\\.");
String part0 = parts[0];
String part1 = parts[1];
int k = Integer.valueOf(part1);
if (part0.equals("gr")) {
g.setColor(Color.GREEN);
gruen[k] = true;
switch (k) {
case 1:
g.fillOval(803,489,58,58);
break;
case 2:
g.fillOval(724,489,58,58);
break; // more cases following
.... }
Then i have another class which handles another jframe and inputs in it:
public class WFenster extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
GUI fe = new GUI();
public void setWFrame() {
// left out: buttons ad layout etc.
wButton.addActionListener(this);
bButton.addActionListener(this);
}
public WFenster() {
setWFrame();
wFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == wButton) {
int p = wuerfel1.random();
Erg.setText(" "+String.valueOf(p));
while (p == 6) {
bButton.setEnabled(true);
break;
} }
if(e.getSource() == bButton) {
bButton.setEnabled(false);
if(rgruen.isSelected()) {
System.out.println("blub");
if(GUI.isBesetzt("gr.110") || GUI.isBesetzt("gr.1") == false)
{
Graphics g = fe.img.getGraphics();
fe.EntferneFigur("gr.110");
fe.ZeichneFigur("gr.1");
}
}
When i call the Entferne/zeichneFigur (paint/removeFigure) methods i want the jframe to update, but i cant find how to. I tried: upating inside the methods with p.update p.revalidate p.repaint this.update/revalidate/repaint repaint/revalidate/repaint and i tried adding those to the action listener, both resulting in nothing. The Frame only updates when i resize or close it. Sorry for the long question i hope somebody can help me.