0

My problem is, I have an array for a log object, the parameters are made in the same class. But the problem is I don't know how to get the parameters called logX logY etc into an actual object.

This is to make the object appear on the screen, but if I can even get it to become an object fist, that would solve so many problems!

This is the Log class:

package Frogger;

import javax.swing.JFrame;

public class Logs extends JFrame
{
    private int X;
    private int Y;
    private int width;
    private int height;


Logs[] LogArray = new Logs[10];
{

LogArray[0].X = 0;
LogArray[0].Y = 0;  
LogArray[0].width = 50; 
LogArray[0].height = 50;
}
}

This is the main:

   package Frogger;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;


public class Frogger_Main extends JFrame implements ActionListener
{
    private Container cPane;
    private JMenuBar mb;
    private JMenu mSystem;
    private JMenuItem mIRules, mIExit;
    private JButton btnExit, btnStart,btnReset;
    private JLabel lblTitle;
    private JPanel pNorth, pSouth, pCentre, pEast;
    private JPanel imagePanel;

    public void actionPerformed(ActionEvent e) 
    {/*For the main public class*/}



    public Frogger_Main()//Constructor
    {
        cPane = getContentPane();

        imagePanel = new JPanel()
        {
            public void paint(Graphics g)//The background thing
            {
                try
                {
                    BufferedImage image  = ImageIO.read(new File("Background.jpg"));
                    g.drawImage(image, 1, 1, null);
                } 
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        };
        imagePanel.setPreferredSize(new Dimension(500,500));

        cPane.add(imagePanel); // End of the background thing 
    }


}

And this is the class that will run it:

package Frogger;

import javax.swing.JFrame;



public class Test_Frogger
{

    public static void main(String[] args) 
    {

        Frogger_Main f = new Frogger_Main();
        f.setTitle("Frogger");
        f.setSize(1280,980);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}
9
  • and where do you need the Logs class? Commented Apr 20, 2018 at 11:16
  • like the object for the logs? Commented Apr 20, 2018 at 11:18
  • i need to make the object, i have the parameters just dont know how to make that into an object Commented Apr 20, 2018 at 11:19
  • Where do you want to make that object? Commented Apr 20, 2018 at 11:20
  • 1
    Write a constructor Commented Apr 20, 2018 at 11:33

2 Answers 2

1

As I mentioned in the comments, your Logs class is weird...

public class Logs extends JFrame {
 private int X;
 private int Y;
 private int width;
 private int height;

//strange and should not be there, but ok
 Logs[] LogArray = new Logs[10]; 

 //totally nonsense piece of code
  {
  LogArray[0].X = 0;
  LogArray[0].Y = 0;
  LogArray[0].width = 50;
  LogArray[0].height = 50;
 }
}

So, you can do something like

package Frogger;

import javax.swing.JFrame;

public class Logs extends JFrame {
 private int x; //convention camelCase, start with small
 private int y;
 private int width;
 private int height;

  Logs(){
  //some kind of default constructor
    this.x = 0;   //int default value is zero, so you dont need to do this in fact
    this.y = 0;
    this.width = 50;
    this.height = 50;
  }

  //parametrized constructor
  Logs(int x, int y, int width, int height){
  //save values to class fields/ object properties
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }


  //because class fields are private, this is called setter
  public void setX(int x){
    this.x =x;
  }

  //because fields are private, this is called getter  
  public int getX(){
    return this.x;
  }

}

Then you can create somewhere Log array

//create array of 10 logs, but each will be null
Logs[] logArray = new Logs[10];

//for each will be used non parametrized constructor- that "some kind of default"" 
for(int i = 0,i < logArray.length; i++){
  logArray[i] = new Logs(); 
}

For example method to obtain this array with given length

public Logs[] getLogs(int count){
  Logs[] logArray = new Logs[count];

  for(int i = 0,i < logArray.length; i++){
    logArray[i] = new Logs(); 
  }  
}

And notice, because there is a setter, then you can make eg.

logArray[0].setX(4); //for setting
logArray[0].getX(); //for read value, getting
Sign up to request clarification or add additional context in comments.

Comments

0

Use contructor in Logs class

public class Logs extends JFrame {

    private int x,y,width,height;

    public Logs(int x, int y, int width, int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }


}

Then call array like

Logs[] LogsArray = new Logs[10];

LogsArray[0] = new Logs(1,2,3,4);
LogsArray[1] = new Logs(3,2,3,5);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.