0

Since I'm new I can't post more than two links, but this is an x-post from reddit.com/r/learnprogramming, just for full disclosure.

I'll basically just be pasting what I said there to here. Thanks for your help, if you can help.

I'm writing somewhat of a graphing application. I currently only have it able to graph sin(x), but that's not the point of this question. I am not able to draw to my main panel. Here is what it currently looks like.

I had an overridden paint function in my Window.java class, which drew the sin(x) function and the axes, but when I made an inner class which extended JPanel(), it would no longer draw.

I then tried to make a separate file, but that didn't draw anything either.

What could be preventing it from drawing? Here are all my files in question.

edit: code in question:

GraphDraw.java:

//import stuff
Public class GraphDraw extends JPanel {
    SinX sinx = new SinX();
    GraphPanel p = new GraphPanel();

    @Override
    public void paintComponent(Graphics gc) {
        super.paintComponent(gc);

        Graphics2D g = gc;
        p.paintComponent(g);
        sinx.paint(g);
    }
}

And in Window.java, I initialize GraphDraw and add it to my main panel, which is underneath the buttons in the picture and above the x/y min/max labels.

GraphDraw drawer = new GraphDraw();
/*
  GUI code
*/
mainPanel.add(drawer);

SinX.java

//import stuff

public class SinX extends Component {

    public void paint(Graphics g) {

        g.setColor(Color.red);

        for(double x=-400;x<=400;x=x+0.5) {
            double y = 50 * sin(x*((Math.PI)/180));
            int Y = (int)y;
            int X = (int)x;
            g.drawLine(400+X,300-Y,400+X,300-Y);
        }
    }
}
  • What he said... give us succinct code examples in your question, not in some github link. – Jim Oct 23 '15 at 18:06
  • A couple of odd things I see. Firstly, SinX extends (AWT) Component, not (Swing) JComponent, although this probably doesn't matter since you don't seem to be adding it to the component hierarchy. Secondly, I'm not sure why GraphDraw needs to explicitly paint its children. Instead, just add the child components to the JPanel. – Eric Galluzzo Oct 23 '15 at 18:46

1 Answers1

0

First, before anything else, do the following:

  1. Change you object from Component to JComponent
  2. Do not ever, ever call paintComponent() or paint() on a graphics object from swing or awt, use object.repaint(); (For reasons I won't go into here, because it's long and complicated)

From there I would try calling setVisible(true); on all your objects. If you are getting this code from a tutorial, then stop and use a different tutorial. You need to learn how swing and the AWT library work before you can start making user interfaces. Nobody uses AWT anymore because Swing is much better. For reasons why, look at the following page. If you are too lazy to do that, its because it's more optimized and more powerful.

What is the difference between Swing and AWT?

Community
  • 1
  • 1
nemo
  • 153
  • 1
  • 10