public class Main extends JApplet {
public static void main(String[] args) {
MyFunction(); //This won't work
}
public void MyFunction() {
System.out.println("Hello World");
}
}
public static void MyFunction(){
}
ToUpperCase myToUpperCaseFunction = new ToUpperCase();
withwindow.setContentPane(new Main());
public class MainClass {
public static void main(String[] args)
{
MyFunction myToUpperCaseFunction = new MyFunction();
MyFunction();
}
public void MyFunction()
{
System.out.println("Hello");
}
}
Of course that won't work. Because MyFunction needs to be a class. What you need is this:
public class MyFunction {
public static void main(String[] args)
{
MyFunction myToUpperCaseFunction = new MyFunction(); // Create a new instance of this class called MyFunction. I.E. calls the constructer.
}
public MyFunction() { // This is what is called when you call the "constructer" with no arguments. (Nothing inside the parenthesis) Notice that there is no return type, as you are creating and returning an "instance" of the object MyFunction.
System.out.println("Object initiated");
}
}
That's actually not entirely correct. You can have many main functions, but only one is defined as THE main function that is called.
ClassName objectName = new ClassName(<param list>);
(yes, i know you can do ClassName objectName = new SubClassName(), i'm keeping it simple.)
public class MainClass {
public static void main(String[] args)
{
MainClass myToUpperCaseFunction = new MainClass();
MyFunction();
}
public void MyFunction()
{
System.out.println("Hello");
}
}
^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();
it doesn't have to have class in it at all. If he wanted the class to be MyFunction, he should call it MyFunction, not MyClass. a method is a function, yes. But in this case it would be a method of the instance variable. e.g.:^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();
i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a function
myToUpperCaseFunction.MyFunction();
it doesn't have to have class in it at all. If he wanted the class to be MyFunction, he should call it MyFunction, not MyClass. a method is a function, yes. But in this case it would be a method of the instance variable. e.g.:^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();
i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a functionCode: [Select]myToUpperCaseFunction.MyFunction();
True, which is why you name it after what it does. not just MyFunction. More like MyUberAwesomeReallyLongThisHasNoSpacesAndIsAPainToInitiateClass. ;-)it doesn't have to have class in it at all. If he wanted the class to be MyFunction, he should call it MyFunction, not MyClass. a method is a function, yes. But in this case it would be a method of the instance variable. e.g.:^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();
i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a functionCode: [Select]myToUpperCaseFunction.MyFunction();
yes, that's what i was aiming for. i just realized MyFunction isn't static so my code won't compile, but regardless if i saw a class called MyFunction i would be confused.
import javax.swing.*;
public class HelloWorldSwing {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
public class MainClass {
public static void main(String[] args)
{
MainClass mainClass = new MainClass();
mainClass.MyFunction();
}
public void MyFunction()
{
System.out.println("Hello");
}
}
I'm making my class an object and accessing one of its properties, which is MyFunction() right?
public class Car
{
public String make;
public String model;
public boolean working; // Set up the variables of the class.
public Car(String arg1, String arg2) // A constructor; it gets called when a new Car object is created to set up some of the new Car's variables.
{
this.make = arg1;
this.model = arg2;
this.working = true;
}
public void dispMakeModel()
{
if (this.working) // First check if it works or not.
{
System.out.println("This car seems to be a " + this.make + " " + this.model + ".");
}
else
{
System.out.println("This car is broken! Fix immediately!");
}
}
public void drive()
{
this.working = false; // Car no longer works.
System.out.println("CRAAASH!!!11");
}
}
public static void main(String[] args)
{
Car myPrecious = new Car("Ford", "Fusion"); // Creates ("buys") a new car with make "Ford" and model "Fusion". Notice that there are two arguments, because the Car constructor above takes two arguments.
myPrecious.dispMakeModel(); // Displays the string "This car seems to be a Ford Fusion."
myPrecious.drive(); // Take it out for a drive. Oh darn, you crashed.
myPrecious.dispMakeModel(); // This time it displays "This car is broken! Fix immediately!"
}
From what I got, is a class like a recipe that defines how to do something or how something is made or what it is made of?
Also, I can only have one main function per program, how do I define which function runs first in a class if that class is not the class with the main function?
Also, why is the relation between file names and class names?
Another thing, a function in a class and both have the same name doesn't need a return type, why??
Another thing, a function in a class and both have the same name doesn't need a return type, why??
That's a constructor, which is a special type of function. It constructs an object when it's created. In other words, whenever you create a new object of that type, it gets run through the constructor which can set up/initiate the object.
public class MainClass {
public static void main(String[] args)
{
MainClass mainClass = new MainClass();
mainClass.MyFunction();
}
public void MyFunction()
{
System.out.println("Hello");
}
public MainClass()
{
System.out.println("How's it going");
}
}
How's it going
Hello
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MainClass extends JApplet{
public static void main(String[] args)
{
MainClass mainClass = new MainClass();
mainClass.theGUI();
System.out.println(Math.cos(100));
}
public void theGUI()
{
JFrame myFrame = new JFrame("Calculator");
myFrame.setSize(400,300);
JButton enterButton = new JButton("Enter");
enterButton.addActionListener(new DisplayMessage());
myFrame.getContentPane().add(enterButton);
myFrame.setVisible(true);
}
private class DisplayMessage implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("You just pressed a button");
}
}
public MainClass()
{
System.out.println("This is the constructor class being ran.");
}
}
Another thing, a function in a class and both have the same name doesn't need a return type, why??
That's a constructor, which is a special type of function. It constructs an object when it's created. In other words, whenever you create a new object of that type, it gets run through the constructor which can set up/initiate the object.
This is the one that I still don't get.
So when I make MainClass mainClass = new MainClass(); will it run through the constructor (if it exists?).
A constructor is ran when its classed is called as an object??
Car myFirstCar = new Car("Ford", "Fusion");
Car mySecondCar = new Car("Mercury", "Milan");
Car myThirdCar = new Car("Lincoln", "Navigator");
myFirstCar.drive();
mySecondCar.drive(); // Not going to crash my brand-new Lincoln :P
Exception in thread "main" java.lang.NullPointerException
at java.awt.Component.setLocation(Component.java:1997)
at MainClass.createGUI(MainClass.java:21)
at MainClass.main(MainClass.java:12)
import javax.swing.*;
import java.awt.*;
public class MainClass extends JApplet {
JFrame mainWindow = new JFrame("Axe Sprite Editor");
public static void main(String[] args)
{
MainClass mainClass = new MainClass();
mainClass.createGUI();
}
public MainClass()
{
}
public void createGUI()
{
mainWindow.setSize(640,640);
mainWindow.setLocation(null);
mainWindow.setResizable(false);
JPanel pixel1 = new JPanel();
mainWindow.add(pixel1);
mainWindow.setVisible(true);
}
}
mainWindow.setLocation(x, y);
or
mainWindow.setLocation(new Point(x, y));
import javax.swing.*;
import java.awt.*;
public class MainClass extends JApplet {
JFrame mainWindow = new JFrame("Axe Sprite Editor");
public static void main(String[] args)
{
MainClass mainClass = new MainClass();
mainClass.createGUI();
}
public MainClass()
{
mainWindow.setSize(640,640);
mainWindow.setResizable(false);
JFrame pixel1 = new JFrame();
mainWindow.add(pixel1);
}
public void createGUI()
{
mainWindow.setVisible(true);
}
}
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:431)
at java.awt.Container.addImpl(Container.java:1039)
at java.awt.Container.add(Container.java:959)
at javax.swing.JApplet.addImpl(JApplet.java:300)
at java.awt.Container.add(Container.java:365)
at MainClass.<init>(MainClass.java:20)
at MainClass.main(MainClass.java:12)
Oh damn, I just noticed, I made it JFrame, I meant JPanel. Sorry and thanks for the setLocationRelativeTo thing :)no problem. I've seen workarounds adding JFrames to other JFrames by doing some weird stuff, but it's usually not necessary.
Now from now on, I'm going to have problems in the layout of the window.If you want it that accurate, just override the paint method and do it yourself. Especially since there will be boundaries for any other object. (e.g. a frame around the box or panel or whatever you use).
I wanna make a 640*640 window with 64 panels of 80*80 as in a grid.
It's a 8*8 grid, each square panel is 80pixels*80pixels, althogether makes a 640*640 pixels window.
Any suggestions/ideas/layouts/code on how to do this?
It just draws a grid on a single JPanel.
In which case, I could give you code, but because of the JPanel having a border, it won't be 640x640. What exactly are you trying to do?It just draws a grid on a single JPanel.
Yes, what I need to do is much harder, I have to draw 64 panels :S
It just draws a grid on a single JPanel.
Yes, what I need to do is much harder, I have to draw 64 panels :S
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class MainClass extends JFrame{
private NPanel[][] panels = new NPanel[8][8];
public MainClass(String s){
super(s);
setSize(8 * 64 + 8, 8 * 64 + 34);
setDefaultCloseOperation(3);
Random rand = new Random();
GridLayout layout = new GridLayout(8, 8);
getContentPane().setLayout(layout);
for(int row = 0; row < 8; row++)
for(int col = 0; col < 8; col++){
panels[row][col] = new NPanel(
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
getContentPane().add(panels[row][col]);
}
}
public static void main(String[] args){
new MainClass("Test");
}
}
class NPanel extends JPanel{
public NPanel(Color c){
setBackground(c);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
}
}
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class MainClass extends JFrame{
private NPanel[][] panels = new NPanel[8][8];
public MainClass(String s){
super(s);
setSize(8 * 64 + 8, 8 * 64 + 34);
setDefaultCloseOperation(3);
Random rand = new Random();
GridLayout layout = new GridLayout(8, 8);
getContentPane().setLayout(layout);
for(int row = 0; row < 8; row++)
for(int col = 0; col < 8; col++){
panels[row][col] = new NPanel(
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
getContentPane().add(panels[row][col]);
}
this.setVisible(true);
}
public static void main(String[] args){
new MainClass("Test");
}
}
class NPanel extends JPanel{
public NPanel(Color c){
setBackground(c);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
}
}