Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Yhag
Dec 23, 2006

I'm dying you idiot!
Hopefully a simple question:

Using Netbeans I created most of a small triangulation program for an assignment:



The grid is a seperate JPanel, relevant code:

code:
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.white);
        g.fillRect(35, 0, 600, 375);
        g.setColor(Color.black);
        g.drawRect(35, 0, 600, 375);
        g.setColor(Color.LIGHT_GRAY);

        for (int k = 0; k < 14; k = k + 1) {
            g.drawLine(36, 25 + k * 25, 634, 25 + k * 25);
        }

        for (int l = 0; l < 23; l = l + 1) {
            g.drawLine(60 + l * 25, 1, 60 + l * 25, 374);
            
        }
    }
On button click it calculates 2 coordinates xC and yC. I want to draw a line from (xA,yA) to (xC,yC) and from (xB,yB) to (xC,yC), relevant code:

code:
                    class jPanel2 extends Plot {

                        @Override
                        public void paintComponent(Graphics g) {
                            super.paintComponent(g);
                            g.setColor(Color.red);
                            g.drawline(xA, yA, xC, yC);
                            g.drawline(xB, yB, xC, yC);
                            Plot.repaint();

                        }
                    }


I run into two problems:

- Using Plot.repaint(); I get the following error:
non-static method repaint() cannot be referenced from a static context.
Googling it gets me solutions that are more complicated (to me) than this problem seems.

- Using doubles xA, yA, etc in the g.drawline() tells me:
incompatible types: posible lossy conversion from double to int
local variable xA is accessed from within inner class; needs to be declared final
.

My Java experience is pretty much zero so googling these errors doesn't really help much.

Thanks

Adbot
ADBOT LOVES YOU

Yhag
Dec 23, 2006

I'm dying you idiot!

TheresaJayne posted:

Well you are trying to access a non static method in a static way,
Plot.repaint() is static

why not try super.repaint();

you have already done that with the paintComponent.

Thanks, that got rid of that error, it won't draw the line though, even if I just add something like g.drawline(0, 0, 100, 100);.

Yhag
Dec 23, 2006

I'm dying you idiot!
Thanks for the quick replies, made me realise I was calling on the wrong thing, it works now!

I was using doubles because they were being used in some example I had, using ints now and actually calling the proper redraw now.

Using this now for the grid panel:
code:
g.setColor(Color.red);
        g.drawLine(35 + p1xA, 375 - p1yA, 35 + p1xC, 375 - p1yC);
        g.setColor(Color.green);
        g.drawLine(35 + p1xB, 375 - p1yB, 35 + p1xC, 375 - p1yC);

    }
    public void setxA(int pxA) {
        this.p1xA = pxA;
    }
    public void setyA(int pyA) {
        this.p1yA = pyA;
    }
    public void setxB(int pxB) {
        this.p1xB = pxB;
    }
    public void setyB(int pyB) {
        this.p1yB = pyB;
    }
    public void setxC(int pxC) {
        this.p1xC = pxC;
    }
    public void setyC(int pyC) {
        this.p1yC = pyC;
    }
And this for the other one:
code:
                    int pxA = (int) (xA);
                    int pyA = (int) (yA);
                    int pxB = (int) (xB);
                    int pyB = (int) (yB);
                    int pxC = (int) (xC);
                    int pyC = (int) (yC);

                    pnlPlot1.setxA(pxA);
                    pnlPlot1.setyA(pyA);
                    pnlPlot1.setxB(pxB);
                    pnlPlot1.setyB(pyB);
                    pnlPlot1.setxC(pxC);
                    pnlPlot1.setyC(pyC);
                    
                    pnlPlot1.repaint();
I don't know if that's the fastests/easiest way, but it works.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply