0

So I was giving this code to run on Netbeans but I'm not sure what's wrong I'm doing, when I copy and past it to new project it keep giving me errors, I think I need to do something when I first create the project, naming most likely, but I can't figure out what's wrong. The code is

Basically, my question is: if I give these two codes what you gonna do, step by step, to run them on NetBeans //code one package LineDrawing;

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

public class LiningPanel extends javax.swing.JPanel {

    public LiningPanel() { }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int w = getWidth();
        int h = getHeight();

        double lines = 15.0;

        for(int i = 0; i < lines; i++)
        {
            int w2 = (int)((i/lines)*w); 
            int h2 = (int)((i/lines)*h); 

            g.drawLine(0,  h2, w2, h);
        }

    }

}

// code 2 //////////////////////////////////////////////////////////

package LineDrawing;

import javax.swing.JFrame;

public class LineDrawingTest {

    public static void main(String[] args) {

        JFrame application = new JFrame();
        LiningPanel panel = new LiningPanel();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
        application.setSize(300, 300);
        application.setTitle("Lining Art");
        application.setVisible(true);
    }
}
André Schild
  • 4,592
  • 5
  • 28
  • 42
  • 3
    What errors...? (I'm guessing it's complaining about not having a `main` method) – MadProgrammer Sep 02 '15 at 06:02
  • This my error when I copy past them, and run: – user3785208 Sep 02 '15 at 07:48
  • Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: LineDrawing.LiningPanel at LineDrawing.LineDrawingTest.main(LineDrawingTest.java:16) Java Result: 1 – user3785208 Sep 02 '15 at 07:49
  • Probably netbeans issue: Refer to this : http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this – justcurious Sep 02 '15 at 07:56

1 Answers1

1

It is difficult to figure unless you mention what exactly the error is. But based on the details provided (you have mentioned you are copying this to a new project and error is most likely related to naming), you might be copying this class to default package. You have to create 'LineDrawing' package and then create/copy your java file under this package. Alternatively, change the first line of your code :

package LineDrawing;

to reflect the correct package under which your java file is present.

justcurious
  • 254
  • 3
  • 15
  • Ok so basiclly if I give you these two codes, what you goona do, step by step to run them. I know they run, I just don't know how to run them – user3785208 Sep 02 '15 at 07:39
  • //code 1 package LineDrawing; import javax.swing.JFrame; public class LineDrawingTest { public static void main(String[] args) { JFrame application = new JFrame(); LiningPanel panel = new LiningPanel(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(panel); application.setSize(300, 300); application.setTitle("Lining Art"); application.setVisible(true); } } – user3785208 Sep 02 '15 at 07:40
  • // code 2 package LineDrawing; import java.awt.Color; import java.awt.Graphics; public class LiningPanel extends javax.swing.JPanel { public LiningPanel() { } public void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); double lines = 15.0; for(int i = 0; i < lines; i++) { int w2 = (int)((i/lines)*w); int h2 = (int)((i/lines)*h); g.drawLine(0, h2, w2, h); } } } – user3785208 Sep 02 '15 at 07:40
  • I just edited my question to make more clear. thanks! – user3785208 Sep 02 '15 at 07:45
  • this is my error when I past and run>>> Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: LineDrawing.LiningPanel at LineDrawing.LineDrawingTest.main(LineDrawingTest.java:16) Java Result: 1 – user3785208 Sep 02 '15 at 07:49