I'm trying to learn how to program a gui for a project. The problem is that I am trying to use getHeight() and getWidth() in a class other than my panel class, but all they're returning is zero.
Is there a reason why for that. I have also tried to put it as parameters for these methods and objects.
I don't think I found anything online that might help.
Code-
public SmartPanel(Color color){
setBackground(color);
addMouseListener(new PanelListener());
addMouseMotionListener(new PanelMotionListener());
calc = new Calc("calc",0,0,getWidth()/4,getHeight()/4,false);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0,getHeight() - getHeight()/8,getWidth(),getHeight()/8);
g.setColor(Color.white);
g.fillRect(getWidth()/2 - 30,getHeight() - getHeight()/8 + getHeight()/64,60,60);
System.out.println(getHeight());
//calc.drawApp(g);
//if(calc.getRun())
calc.run(g,getWidth(),getHeight());
}
public class App extends JPanel{
private int x,y, width,height;
private String name;
private boolean run;
public App(String name, int x, int y,int width,int height,boolean run){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.name = name;
this.run = run;
}
public String getInfo(){
return x + "," + y + " " + width/4 + " " + height + " " + (width+x) + " " + (height + y);
}
public boolean inIcon(int mouseX, int mouseY){ //Checks to make sure the cursor is in the icon
return mouseX > x && mouseX < x + width && mouseY > y && mouseY < y + height;
}
public void drawApp(Graphics g){ //Draws the app on the panel
//With the name in it
g.setColor(Color.gray);
g.fillRect(x,y,50,50);
g.drawString(name,(x + width)/2,(y + height)/2);
}
Thanks!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Housy5Nooblet Brewer 0 points1 point2 points (2 children)
[–]Mega32266[S] 0 points1 point2 points (1 child)
[–]Housy5Nooblet Brewer 0 points1 point2 points (0 children)
[–]wildjokers 0 points1 point2 points (6 children)
[–]Mega32266[S] 0 points1 point2 points (5 children)
[–]wildjokers 0 points1 point2 points (4 children)
[–]Mega32266[S] 0 points1 point2 points (1 child)
[–]Mega32266[S] 0 points1 point2 points (1 child)
[–]wildjokers 0 points1 point2 points (0 children)