Jump to content

I'm trying to learn how to make a java swing program, and when I make an object and try to put in the getHeight() and getWidth() in the parameter for the constructor or use these directly in their class, the only value I'm getting is 0. Is there a reason why for that? Thanks!

 

Link to comment
https://linustechtips.com/topic/1435554-learning-how-to-program-using-java-swing/
Share on other sites

Link to post
Share on other sites

Can you provide any code snippets?

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 9600X || GPU: RX 9070 XT|| Memory: 32GB || Cooler: Peerless Assassin || PSU: RM850e|| Case: Lian Li A3

Link to post
Share on other sites

18 minutes ago, Slottr said:

Can you provide any code snippets?

import javax.swing.*;
import java.awt.*;

public class SmartPhone{

   private JFrame frame;
   
   public SmartPhone(){
   
   }
   
   private void createAndShowGui(){
      frame = new JFrame();
      frame.setSize(360,840);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container pane = frame.getContentPane();
      
      SmartPanel panel = new SmartPanel(Color.white);
      pane.add(panel);
      
      JPanel north = new JPanel();
      north.setBackground(Color.black);
      north.setPreferredSize(new Dimension(360,75));
      pane.add(north,BorderLayout.NORTH);
      
      frame.setVisible(true);
   }
   public static void main(String[] args){
       
      final SmartPhone phone = new SmartPhone();
      EventQueue.invokeLater(phone::createAndShowGui);  
   }  
   
   
   
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SmartPanel extends JPanel{
   
   private int mX,mY; //MouseX, MouseY 
   private Calc calc;
   
   public SmartPanel(Color color){
      setBackground(color);
      //setPreferredSize(new Dimension(360,765));
      
      addMouseListener(new PanelListener());
      addMouseMotionListener(new PanelMotionListener());
      
      calc = new Calc("calc",0,0,getWidth()/4,getHeight()/4,false); //Right now, getWidth and getHeight parameter useless for constructor. 
      //Calc extends App
   }
   
   public void paintComponent(Graphics g){
      super.paintComponent(g);
      
      //Bottom chin w/ home button
      g.setColor(Color.black);
      g.fillRect(0,getHeight()*7/8,getWidth(),getHeight()/8);
      g.setColor(Color.white);
      g.fillRect(getWidth()/2 - 30,getHeight() - getHeight()/8 + getHeight()/64,60,60);
      
      
      calc.drawApp(g,getWidth()/4,getHeight()/8);
      
      if(calc.getRun()){
         calc.run(g,getWidth(),getHeight()); //because I can't use it as parameter as constructor, I just put it in as parameter for the methods
      }
      else{
      
      }  
      
   }
import javax.swing.*;
import java.awt.*;

public class App extends JPanel{
   
   private int x,y;
   private int width,height; //These have values of zero even though I use the getHeight() and getWidth() as parameter for constructor
   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 int getInfo(){
      return -1; //Not using right now
   }
   public boolean inIcon(int mouseX, int mouseY,int w,int h){ //Checks to make sure the cursor is in the icon
      return mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h;//Right now, since height and width doesn't work, I am using w and h
      //return true;
   }
   public void drawApp(Graphics g,int w, int h){ //Draws the app on the panel
                                    //With the name in it
      g.setColor(Color.gray);                              
      g.fillRect(x,y,w,h);
      g.setColor(Color.black);
      g.drawString(name,(x + w)/4,(y + h)/2);
            
   }
   public int getWidth(){
      return width;
   }
   public int getHeight(){
      return height;
   }
   
}

 

Link to post
Share on other sites

I haven't looked at it thoroughly, but after skimming it I would likely say its the way you're calling the methods. When you call getHeight() you're not referencing any specific object.

 

Running getHeight() would return the object from this. instance. So the way you have things layered, you're likely not referencing the Smartphone JFrame object.

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 9600X || GPU: RX 9070 XT|| Memory: 32GB || Cooler: Peerless Assassin || PSU: RM850e|| Case: Lian Li A3

Link to post
Share on other sites

2 minutes ago, Slottr said:

I haven't looked at it thoroughly, but after skimming it I would likely say its the way you're calling the methods. When you call getHeight() you're not referencing any specific object.

 

Running getHeight() would return the object from this. instance. So the way you have things layered, you're likely not referencing the Smartphone JFrame object.

I'm sorry, can you explain that a bit more please, and is there a way to make it so it woud work as intended?

Link to post
Share on other sites

30 minutes ago, EricW said:

I'm sorry, can you explain that a bit more please, and is there a way to make it so it woud work as intended?

 

If you create an object:  Foo myFoo = new Foo();

 

and Foo has the property and get method Height

 

You can reference myFoo's height by doing myFoo.getHeight()

 

 

 

But if I have another object which extends myFoo: ExtFoo myExtFoo = new ExtFoo();

 

this will also have a height property, however between myFoo and myExtFoo, these properties are not the same, as they're entirely different objects.

 

 

Like if you had a couple of people, and assigned each a ball. So when you reference Timmy and his ball, you're only referencing Timmy's ball, not referencing Sarah's ball because Timmy does not have Sarah's ball.

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 9600X || GPU: RX 9070 XT|| Memory: 32GB || Cooler: Peerless Assassin || PSU: RM850e|| Case: Lian Li A3

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×