How to write the BouncingBall program in Intellij . . . I'm trying to use multiple sources of information to get a good grasp of Java. To this end, I've taken the Udacity Intro to Java course, downloaded Intellij-IDEA, use the Sololearn App, and have been watching a series of lectures posted to Youtube by Stanford University. I'm trying to copy the program from Mehran Sahami's tenth lecture in the series https://www.youtube.com/watch?v=YpZCKVG4s5k&t=1995s but can't get it to run. I'm assuming that there are elements required for it to run that are not being properly imported or invoked. But, being a novice, at both Java and Intellij, I'm not sure where to start.
Here's how my code starts, anything obvious leap out? Is anything here calling for files or classes I need to install into the source or project folders, JDK, or elsewhere?
<import acm.program.*.r[]
public class BouncingBall extends GraphicsProgram{
private static final int DIAM_BALL=30;
private static final double GRAVITY=3;
private static final int DELAY=50;
private static final double X_START=DIAM_BALL/2;
private static final double Y_START=100;
Certain terms in the IDE are shown in red and the tooltip indicates "Cannot resolve symbol(or method)". This also suggests to me that the tool cannot reach the required classes or files. So . . . how do I fix that?
Thanks in advance!
I think it should be
import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.applet.*;
public class BouncingBall extends GraphicsProgram {
private static final int DIAM_BALL = 30;
...
The imports just be folded in the video.
Also download the acm.jar into your project directory and right click the jar file, select Add as Library will add the jar file to project in intellij.
Related
I am using Visual Studio Code (version 1.32.3) with Java. I have a .class file called 'RandomGenerator.class' which I want to instantiate from the main. The main.java and RandomGenerator.class are in the same directory.
My statement:
RandomGenerator rdgen = new RandomGenerator(1,1,1,1);
will have an error :RandomGenerator could not be resolved to a type java(16777218)
I tried to include import RandomGenerator; (I don't need but try to see if it is required) - I get an error "RandomGenerator cannot be resolved java(268435846)" at the import statement.
The specification of the constructor given is RandomGenerator​(int seed, double lambda, double mu, double rho)
Appreciate help to make it work in Visual Studio Code. Thanks.
Make sure RandomGenerator.class is on your path!
Import the class in the file that is using it!
If that does not work - is RandomGenerator visible or your class, i.e. public or package private and in the same package?
I am trying to add instant app functionality to my project. So I am following the instructions given in this tutorial : https://codelabs.developers.google.com/codelabs/android-instant-apps/#0
The fifth chapter explains how to move the existing code from an application module to a feature module. I'm following this tutorial step-by-step, updating both the playground topeka project given with this tutorial and my project. However, I'm stuck after the first sub-chapter "Convert the app module into a feature module called topeka-base".
After renaming my project folder to project-base, and transforming it from com.android.application to com.android.feature, the Gradle sync runs fine but I can't rebuild my project anymore, since every occurrence of my.project.R get a Cannot resolve symbol 'R' error. I'm not having this problem with the topeka app that comes along with the tutorial, however.
As you can see below, there is no such error in topeka project:
So I tried looking at my project generated files to find a difference, but actually I don't see any difference with topeka:
package my.project;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "my.project";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 38;
public static final String VERSION_NAME = "1.9.0";
}
So what could be the reason? Thanks for your help.
You can first start with trying to clean and then rebuild the project sometimes it fixes the issue.
You can also try invalidating the caches and restarting the android studio.
Also, I guess you should check the manifest file and edit the app's name there.
I'm trying to teach myself java syntax and using minecraft as a platform for diving in. I'm having a problem though because none of my textures are being loaded. For that matter neither are my localizations. Here is the code for my block
package net.richbaird.testtutorial.blocks;
import cpw.mods.fml.common.registry.GameRegistry;
//import cpw.mods.fml.common.registry.LanguageRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.richbaird.testtutorial.lib.constants;
public class OrangeBlock extends Block {
private String blockName = "orangeBlock";
public OrangeBlock() {
super(Material.rock);
this.setBlockName(constants.MODID + "_" + blockName);
this.setCreativeTab(CreativeTabs.tabBlock);
GameRegistry.registerBlock(this,blockName);
this.setBlockTextureName(constants.MODID + ":" + blockName);
//LanguageRegistry.addName(this,"tutorial block");
}
}
here is my constants class
package net.richbaird.testtutorial.lib;
public class constants {
public static final String MODID = "testtutorial";
public static final String MODNAME = "Test Tutorial";
public static final String VERSION = "1.0";
}
I have my texture saved at
~/IdeaProjects/testmod/src/main/resources/assets/testtutorial/textures/blocks/orangeBlock.png
According to the log it is unable to find my texture. Here's the message I'm getting
[08:08:14] [Client thread/ERROR]:
Using missing texture, unable to load
testtutorial:textures/blocks/orangeBlock.png
java.io.FileNotFoundException: testtutorial:textures/blocks/orangeBlock.png
The client loads and my item shows up but with a default black and purple texture. What have I done wrong? I'm thinking it might have to do with my naming conventions, since the .lang file never gets read either, and the only way I can give my block a friendly name is with the now depreciated LanguageRegistry.addName() method
For those who are curious, it's a bug with intellij 14 looks like. Adding this line to the bottom of the build.gradle that comes with forge
sourceSets {
main { output.resourcesDir = output.classesDir }
}
And running gradle setupDecompWorkspace idea --refresh-dependencies
fixed the problem.
I recently ran into this bug after updating IntelliJ, and while richbai90's solution did fix the immediate issue, it also broke compiling the mod into a jar (the assets folder gets included twice). After some digging around, I eventually found the root of the issue: IntelliJ was delegating the build task to Gradle, which put the assets and classes in separate folders, and Forge didn't know they belong to the same mod. The solution that worked for me was to build and run using the IDE, which is in the Settings dialog under Build, Execution, Deployment | Build Tools | Gradle (the help page has more detailed instructions). On older versions of IntelliJ, this was called "Delegate IDE build/run actions to gradle" (see the help page).
Sorry for this noobie question, I'm new to Java, and instead of using IDE, i want to using command line to learn what's running under the hood
I'm following the Getting Started guild on MigLayout
#MigWindow.java
public class MigWindow {
public static void main(){
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());// a simple line to make sure the library jar import correctly
}
}
and compile with these command:
javac -cp ./MigLayout.jar MigWindow.java
and I got a error:
MigWindow.java:3: cannot find symbol
symbol : class MigLayout
location: class MigWindow
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());
^
1 error
It seems the jar library doesn't import correctly, any idea?
~
Make sure you add the import for MigLayout
import net.miginfocom.swing.MigLayout;
It may sound obvious, but make sure MigLayout.jar the current directory when calling javac here and that your JAR file has not been corrupted.
Update:
To check that your JAR file does contain the class you can do:
jar tvf MigLayout.jar
and check for the MigLayout class. Failing to find the class you can download the correct one from here.
You are missing an import statement in your Source File. The compiler does not know where 'MigLayout' is coming from.
Add at the top of your file, but below of your package statement (if any) an import, e.g.
import package.MigLayout;
This tells the compiler what to import from the given class path. You will need to replace package with the correct package.
I want to learn to write my own packages so I'm not also relient on an IDE, which I feel I have became. The problem is I cannot figure out how to run my own package, or what the proper method is to run your own package.
Here's a resource I used to learn some basics: http://javaworkshop.sourceforge.net/chapter3.html
Here's my current file structure:
Main.java
/src
projectaqua/
GameFrame.java
/classes
projectaqua/
GameFrame.class
I ran the command in the root directory of the project:javac -d ./classes/ ./src/projectaqua/*.java
I originally created a Main file in the /src/projectaqua directory and attempted to run the file. I was given this error:
Main.java:1: error: package projectaqua does not exist
import projectaqua.GameFrame;
I tried running the application in the /classes/projectaqua directory when compiling the Main file with the package, which gave me a class not defined error.
This compiled my package, the problem I'm facing is I don't understand how you are supposed to import your own package to run it, and where would the file to run the package be?
From what I've learned in school, when writing a GUI application we create a class that has a main function in it to instantiate the frame, and that's it's only job. Where would this be in this structure?
Intuitively it seems that file would be outside of the src files, but I feel like that removes the purpose of the src files. I haven't found anything useful on stackoverflow to this topic, if you do or have please point me in that direction.
More source code:
GameFrame Class:
package projectaqua;
import javax.swing.JFrame;
public class GameFrame extends JFrame
{
private int WINDOW_HEIGHT = 500;
private int WINDOW_WIDTH = 500;
private String title = "Project Aqua";
private boolean isVisible = true;
public GameFrame()
{
// Basic Window Defaults
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setTitle(this.title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Content Pane junk
// Will be added
setVisible(this.isVisible);
}
}
The Main class
import projectaqua.GameFrame;
public class Main
{
public static void main(String[] args)
{
GameFrame launch = new GameFrame();
}
}
I now see your problem.
In your question you were not clear that you had trouble running v. compiling. Had you posted this error trace it would have been immediately clear to me what your problem is:
unrollme-dev-dan:projectaqua Dan$ java Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: projectaqua/Main)
Also note that had you Googled NoClassDefFoundError would have found this. The moral here is: understand and research your exact error.
Anyway
unrollme-dev-dan:classes java projectaqua/Main
is what you want. Notice the change of directory. I never bothered to understand why, has to do with relationship between package hierarchy and file structure hierarchy.
Java had two choices when designed: Assume the thing you are talking about is in the global package (yuck!) or try to guess what package it is in. It treats any folder below your working directory as packages. So even though it found a Main class in the directory from which you were running it did not find a Main class in the namespace corresponding to the directory . i.e. the global one.
When you run from one directory up and tell it to run something in projectaqua/ it is now looking for classes starting with projectaqua.
Alternately if you run
unrollme-dev-dan:projectaqua java projectaqua.Main
It looks for the right thing.
try this command at the root of your project
javac -cp ./classes -d ./classes ./src/projectaqua/*.java
Also make sure both your Main.java and GameFrame.java has package projectaqua; at the beginning