- 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
|
#
¿
Sep 5, 2014 06:45
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 28, 2024 07:19
|
|
- Yhag
- Dec 23, 2006
-
I'm dying you idiot!
|
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);.
|
#
¿
Sep 5, 2014 07:55
|
|
- 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.
|
#
¿
Sep 5, 2014 08:33
|
|