1

I'm new to programming and I can't seem to sort this problem: how can I make the frame of my app change? It's a Minesweeper game. The program logic is good, events are working, but the frame itself isn't repainting itself... I did not have this kind of problem with applets.. how can I make this work?

public void paint(Graphics g) {
    Graphics2D g2=(Graphics2D)g;
    _Main.toPaint(g2);
}
public void update(Graphics g) {
    Graphics offgc;
    Image offScreen=null;
    Dimension d=size();

    offScreen=createImage(d.height, d.width);
    offgc=offScreen.getGraphics();

    offgc.setColor(getBackground());
    offgc.fillRect(0, 0, d.width, d.height);
    offgc.setColor(getForeground());

    paint(offgc);
    g.drawImage(offScreen,0,0,this);
}
alex2410
  • 10,904
  • 3
  • 25
  • 41
  • 1) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 26 '13 at 08:24
  • I want to learn awt and swing too. Will look swing too.. but I put awt first. Thank you btw! – Barnabas Lesti Dec 26 '13 at 19:21
  • *"but I put awt first."* That makes no sense. In fact, when people go from AWT to Swing, they need to 'unlearn' what they knew about AWT components. The *only* reason it actually makes sense to learn about AWT *components* is if you actually expect to be forced to code using AWT components in a job. That is *extremely* unlikely to happen. – Andrew Thompson Dec 27 '13 at 03:30
  • Thanks very much! I wanted to apply for a job and they're using AWT.. but maybe my information was incorrect.. I'll change to swing.. there are a lot more things for swing to learn from.. Thanks for the heads up! – Barnabas Lesti Jan 01 '14 at 12:45
  • *"they're using AWT"* My *deepest* sympathies. – Andrew Thompson Jan 01 '14 at 12:48

2 Answers2

2

Seems you draw your graphics wrong. For custom paintings you need to use paintComponent(...) method of JComponent, for example of JPanel, instead of paint() method and custom update(). For updating your component graphics just call repaint() method.

Here is simple example with drawing for you,try and examine that:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

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


public class Example extends JPanel{

    private Random rand = new Random();

    public static void main(String... s){
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Example example;
        f.add(example = new Example());

        JButton b = new JButton("repaint");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                example.repaint();
            }
        });
        f.add(b, BorderLayout.SOUTH);
        f.setSize(200,200);
        f.setVisible(true);
    }

    public Example(){
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        for(int i =0; i<10;i++){
            int nextInt = rand.nextInt(getHeight()-10);
            int nextInt2 = rand.nextInt(getWidth()-10);
            g.fillRect(nextInt2, nextInt, 10, 10);
        }
    }
}

Also read recommendations of Andrew Thompson.

alex2410
  • 10,904
  • 3
  • 25
  • 41
0

Try using the JFrame class. Let's say your JFrame is called frame. Try frame.pack(), then frame.repaint(). The only problem with this is that I don't know how to make the frame.repaint() method invoke itself mid-Thread; it wants to wait until the Thread is terminated.

JimmyBob23
  • 11
  • 1