0
public class Main extends JPanel implements Runnable {

    private static final int WIDTH = 1000;
    private static final int HEIGHT = 800;
    TerranGenartor gen = new TerranGenartor();

    public Main() {
        setSize(WIDTH, HEIGHT);
        gen.run();
    }

    public void paintComponents(Graphics g) {
        repaint();

    }

    public void run() {
    }
}

I do get an error at

 TerranGenartor gen = new TerranGenartor(); 

My TerranGenartor class

public class TerranGenartor implements Runnable {

    Main main = new Main();
    Graphics g;
    Random r = new Random();
    int SMIN = 1;
    int SMAX = 3;
    int i = 0;
    int num;
    int x = 0;
    int y = 0;
    public Color grass = new Color(124, 252, 0);
    public Color stone = new Color(190, 190, 190);
    public Color dirt = new Color(139, 69, 19);
    boolean Runn = false;

    public void start() {
        Runn = true;
        System.out.println("Working...");
    }

    public TerranGenartor() {
        Runn = true;
    }

    protected void paintComponent(Graphics g) {
        g = main.getGraphics();
        while (Runn) {
            if (i <= 1000) {
                num = r.nextInt(SMAX - SMIN + 1) + SMIN;
                switch (num) {
                    case 1:
                        g.setColor(grass);
                        break;
                    case 2:
                        g.setColor(stone);
                        break;
                    case 3:
                        g.setColor(dirt);
                        break;
                    default:
                        g.setColor(grass);
                        break;
                }
                g.fillRect(x, y, 50, 50);
                x += 50;
                System.out.println(i);
                System.out.println("X: " + x);
                try {
                    Thread.sleep(15);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void run() {
        while (Runn) {
            paintComponent(g);
        }
    }
}

This class is the class that will not paint in the main class(JPanel extend). I tried a few things but nothing seems to be working.

My The_Runner class

public class The_Runner {

    Main main = new Main();
    JFrame frame = new JFrame();
    private static final int WIDTH = 1000;
    private static final int HEIGHT = 800;
    String TITLE = "Map gen.";

    public The_Runner() {
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setTitle(TITLE);
        frame.add(main);
    }

    public static void main(String[] args) {
        The_Runner runner = new The_Runner();
    }
}    

This class just complies and runs it, I think it is all good.

The problem with this is that when I want to paint from the TerranGenerator class it does not work. Nothing shown on the JPanel.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) It is typically easier to do custom rendering using a `BufferedImage` as the canvas. – Andrew Thompson Jan 29 '13 at 04:39
  • What is with the plethora of blank 'white space' lines in that source? Only 1 white-space line is ever needed, most of those in the source don't improve readability, and *none* of them make the code go faster. – Andrew Thompson Jan 29 '13 at 04:41
  • I'm sorry it was my first question asked, very new to this site. – Brandon Cunningham Jan 29 '13 at 04:45
  • 1
    The important thing is not about getting it right the first time, but learning from experience. Welcome to SO. :) – Andrew Thompson Jan 29 '13 at 04:53

1 Answers1

2

Let's start with...

setSize(WIDTH, HEIGHT); from Main. This is a bad idea. These values could be overridden by a layout manager (assuming you're using one). You should override getPreferredSize and return them there...

public Dimension getPreferredSize() {
    return new Dimension(WIDTH, HEIGHT);
}

Then this (also from Main)

public void paintComponents(Graphics g) {
    repaint();
}

You should NEVER call repaint from within a paint method or call any method that may cause repaint to be called, you will end up in a infinite loop of doom that will cycle your CPU up to 100%

You have a cyclic reference between Main and TerranGenartor. Basically you've declared a reference of each in each, the compiler can't deal with this as it can't build one class without building the other first.

NEVER do this...

g = main.getGraphics();

getGraphics is can return a null value. The resulting value return by this method is nothing more then a snap shot of the current graphics context used by the application. This could be dereferenced or changed on the next paint cycle.

NEVER paint/update/modify/create ANY Swing component from outside the Event Dispatching Thread (EDT), you could end up with weird painting artifacts if you do, as you try to update the graphics and the EDT updates it as well, never pretty.

You might like to take a look at

Updated

I suspect you "primary" problem (apart from the inability for the code to compile) will be that the repaint request is wiping out any changes you painted by using main.getGraphics

You should also be calling super.paintComponent within the Main#paintComponent method.

Updated with Examples

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366