So I was giving this code to run on Netbeans but I'm not sure what's wrong I'm doing, when I copy and past it to new project it keep giving me errors, I think I need to do something when I first create the project, naming most likely, but I can't figure out what's wrong.
The code is
Basically, my question is: if I give these two codes what you gonna do, step by step, to run them on NetBeans
//code one
package LineDrawing;
import java.awt.Color;
import java.awt.Graphics;
public class LiningPanel extends javax.swing.JPanel {
public LiningPanel() { }
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
double lines = 15.0;
for(int i = 0; i < lines; i++)
{
int w2 = (int)((i/lines)*w);
int h2 = (int)((i/lines)*h);
g.drawLine(0, h2, w2, h);
}
}
}
// code 2 //////////////////////////////////////////////////////////
package LineDrawing;
import javax.swing.JFrame;
public class LineDrawingTest {
public static void main(String[] args) {
JFrame application = new JFrame();
LiningPanel panel = new LiningPanel();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300, 300);
application.setTitle("Lining Art");
application.setVisible(true);
}
}
It is difficult to figure unless you mention what exactly the error is. But based on the details provided (you have mentioned you are copying this to a new project and error is most likely related to naming), you might be copying this class to default package. You have to create 'LineDrawing' package and then create/copy your java file under this package. Alternatively, change the first line of your code :
package LineDrawing;
to reflect the correct package under which your java file is present.
Related
I'm trying to create a program that allows you to run multiple sketches off of one master sketch through using the PApplet.runSketch function. This has posed the best option for me but I want to be able to access a collection of java files within multiple folders for better organization of the games. The problem is I have no idea how to do so within the constraints of the function and when I search for pre-existing examples, none properly solve it the way I'd like.
Reason for wanting folders instead of multiple tabs:
The folders would be saved in the same folder alongside my main sketch.pde. The problem with using multiple tabs is that I have games that use up to 5-8+ tabs and that would eventually just clutter everything. So I'm looking for a way to store games in individual folders and then access within my main sketch.pde each individual game's main java file
Here's my code
\sketch.pde
void settings() {
size(600, 200);
}
void setup() {
String args[] = {
"--display=1",
"--location=0,0",
"--sketch-path=" + sketchPath("Game1"),
"Game1"
};
Game1 g = new Game1(320, 420);
PApplet.runSketch(args, g);
}
void draw() {
background(0);
ellipse(50, 50, 10, 10);
}
\Game1\Game1.java
import processing.core.*;
public class Game1 extends PApplet {
private final int w, h;
public Game1(int w, int h) {
this.w = w;
this.h = h;
}
public void settings() {
size(this.w, this.h);
}
public void draw() {
background(0);
}
}
The problem is that the Game1 class is not recognized because the Game1.java file is within a folder and not present with the sketch.pde, but that is how I want to be able to run the sketches. Any ideas?
I am having some trouble with my code which is written in java. The first time after I start eclipse it launches and runs perfectly, but if I try to run it again - without making any changes - the only thing I see is an empty JFrame. Why might this happen? I have gotten quite a bit of files so to launch them all up here would be a lot to look through. Maybe you've tried something like this before? Where the program can launch sometimes? If not tell me so I can add the code.
I know that all of my classes are called since I have printed them all in my search to get the game to work.
The entire code worked until I started to put most of it in different objects in order to make it easier to look through.
package Pacman;
import java.util.Scanner;
import javax.swing.JFrame;
public class Frame extends JFrame{
public static int Width, Height;
public static void main(String[] args){
Scanner console = new Scanner(System.in);
Width = console.nextInt();
Height = console.nextInt();
new Frame();
}
public Frame(){
new JFrame();
this.setTitle("PacMan");
this.setLocation(400,300);
this.setResizable(false);
this.setVisible(true);
Screen screen = new Screen(this, Width, Height);
this.add(screen);
}
}
This is the JFrame, but I am pretty sure the problem isn't here.
If you want to see the entire codesystem it's at github: https://github.com/Lampeskaerm/SoftTek_Projekt.git
I hope you can help me
Anne-Katrine
After adding the component you should use this.setVisible(true) in the last.
I am relatively new to Java and have been trying to simulate the motion of an object (say a car) on a straight path.
I want my object to move in steps in the output, instead of appearing just at the last point of the line.
I have used 2 classes :Veh.java - the vehicle object and SimuFrame.java - to create the simulation environment.
I have referred to some online tutorials for ideas: http://www.newthinktank.com/2012/07/java-video-tutorial-52/ (This simulates the asteroids game. Howeer I want my object to move in a straight line instead of in a random direction)
Please help me understand where I am wrong and what to do next..
Thanks a lot.
Here's my code:
import java.awt.*;
import java.awt.event.*;
public class Veh extends Rectangle{
int uLeftX, uLeftY; //upper LH Position for Rectangle
static int height = 20;
static int width = 20;
int[] pathCoords=new int[1000];
int startPosY; // start position of the objet - anywhere on the left bounday of the frame.
int goalPosY; // end position of the objet - anywhere on the right boundary of the frame.
//Constructor to Create a new Veh
public Veh(int startPosY,int goalPosY){
//Create a new rectangle vehicle from super class constructor
super(0, startPosY, height, width);
this.startPosY=startPosY;
this.goalPosY=goalPosY;
this.pathCoords = Pathmove();
}
//Calculating the 1000 points on the line joining (0,startPosY) and (goalPosY,999)
int[] Pathmove(){
//Slope calculation
float s=(float)(this.goalPosY-this.startPosY)/999;
pathCoords[0]=this.startPosY;
System.out.println("First xy pair is: 0," +this.pathCoords[0]);
for(int m=1; m<1000; m++){
pathCoords[m]= (int)(m*s)-(int)((m-1)*s)+ pathCoords[m-1];
}
return pathCoords;
}
//Function to move the Reactangular object along the line using the Y coordinate values from Pathmove()
void move(){
int[] a = (int[])this.pathCoords.clone();
for (int c=0; c<a.length;c++){
this.setLocation(c,a[c]);
}
}
}
This is the code for creating the simulation environment.
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class SimuFrame extends JFrame{
public static int frameWidth=1000;
public static int frameHeight=1000;
public static void main(String[] args){
new SimuFrame();
}
public SimuFrame(){
this.setSize(frameWidth,frameHeight);
this.setTitle("Path Planning Results");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimuObject SO=new SimuObject();
this.add(SO);
// Used to execute code after a given delay
// The attribute is corePoolSize - the number of threads to keep in
// the pool, even if they are idle
ScheduledThreadPoolExecutor executor= new ScheduledThreadPoolExecutor(5);
executor.scheduleAtFixedRate(new RepaintTheFrame(this), 0L, 20L, TimeUnit.MILLISECONDS);
this.setVisible(true);
}
}
// Class implements the runnable interface
// By creating this thread I want to continually redraw the screen
// while other code continues to execute
class RepaintTheFrame implements Runnable{
SimuFrame theFrame;
public RepaintTheFrame(SimuFrame theFrame){
}
#Override
public void run() {
theFrame.repaint();
}
}
class SimuObject extends JComponent{
//Holds every Veh created
public ArrayList<Veh> vehs=new ArrayList<Veh>();
public SimuObject(){
int startPosY = (int)(Math.random()*999);
int goalPosY = (int)(Math.random()*999);
vehs.add(new Veh(startPosY,goalPosY));
}
public void paint(Graphics g){
// Allows me to make many settings changes in regards to graphics
Graphics2D graphicSettings = (Graphics2D)g;
// Draw a background that is as big as the Simu board
graphicSettings.setColor(Color.WHITE);
graphicSettings.fillRect(0, 0, getWidth(), getHeight());
// Set rendering rules
graphicSettings.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Set the drawing color to red
graphicSettings.setPaint( Color.RED);
// Cycle through all of the Rock objects
for(Veh veh : vehs){
// Move the vehicle
veh.move();
graphicSettings.draw(veh);
}
}
}
You have a number of problems in your code:
You have a (swallowed) NullPointerException (NPE) in RepaintTheFrame.run(), which causes ScheduledThreadPoolExecutor.scheduleAtFixedRate() to run only once, per scheduleAtFixedRate()'s javadoc.
You are moving your car in JComponent.paint().
In any graphical framework, the repaint will be called automatically by the framework, usually on an OS event, e.g. moving the window, moving the mouse over the window, etc.
Your paint() method should only draw. It should not modify your domain model.
Your move() method always ends up with the vehicle at the end. That's probably not your intent. You probably want your move() method to merely increment the car's position.
Moving from a start value to an end value in steps is called interpolation. You want linear interpolation specifically here. Its one of the easiest to grasp.
This page will be of great assistance to you.
Without getting fancy with interpolation, you could just change your move routine like so:
int index =0;
void move(){
//int[] a = (int[])this.pathCoords.clone();
if(index<this.pathCoords.length)
this.setLocation(c,pathCoords[index]);
index+=1;
}
Not sure why you were cloning the array there. It probably isn't necessary.
After more than a couple of days I am still unable to populate a text area by going through an array as I can in other languages. I have tried Google, YouTube, stackoverflow, and others and I am still unable to use any examples to help me do this. I have also referenced Java texts. Here is exactly what I am trying to do:
public void getDrinks() {
//System.out.println(theDrinks[arrayCount].toString());
for(int i=0; i<arrayCount; i++) {
area.append(theDrinks[i].toString());
}
}
This code works in other languages but something is wrong with the way I am using the TextArea or the array because I am getting a null pointer. I would love to paste the entire program, but that is not working either. This is the only part that will even remotely paste correctly. Please help me if you can.
You do not, in general, want to use a variable like arrayCount when you can help it. A better version is this:
public void getDrinks() {
for(int i = 0; i < theDrinks.length; i++)
area.append(theDrinks[i].toString());
}
When doing this, it is important to make sure that area has been instantiated already (i.e. it is not null).
If I were implementing this, I would use Java's foreach construct instead, as I find it's a bit more expressive. The following code assumes that theDrinks is an array of Drink objects.
public void getDrinks() {
if(area != null) {
for(Drink drink : theDrinks) {
area.append(drink.toString());
}
}
}
I can't way whay your specific problem can be without more code but here some java code that works.
import java.awt.BorderLayout;
import java.awt.TextArea;
import javax.swing.JFrame;
public class Driver {
public static void main(String[] args) {
// 1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
// 2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 3. Create components and put them in the frame.
// ...create emptyLabel...
TextArea area = new TextArea("Area");
frame.getContentPane().add(area, BorderLayout.CENTER);
// 4. Size the frame.
frame.pack();
// 5. Show it.
frame.setVisible(true);
String[] drinks = {"Drink1","Drink2"};
getDrinks(area, drinks);
}
public static void getDrinks(TextArea area, String[] theDrinks) {
// System.out.println(theDrinks[arrayCount].toString());
for (String drink : theDrinks) {
area.append(drink.toString());
}
}
}
So, for some reason when I try to use a for loop to initialize panels in chess board, it actually loops the loop itself. In other words, it doesn't go on forever, but it starts and completes again and again.
package chessgame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChessGame extends JFrame implements ActionListener{
public static final int WIDTH=800;
public static final int HEIGHT=800;
public static void main(String[] args) {
ChessGame gui = new ChessGame();
gui.setVisible(true);
}
public ChessGame(){
super("Chess Game Demo");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(8,8));
JPanel[] chessSquares = new JPanel[64];
for (int a=0;a<64;a++){
System.out.println(a);
}
}
}
I have included all potentially relevant code because I plan to use indices of chessSquares to color squares black and white. When I do this I also get a NullPointerException. I can understand why I'm getting that given the following error, but I cannot at all understand why a would be printed 0, 1....62, 63 over and over again. I am relatively new to Swing and have absolutely no idea why it does this. If anyone could explain that would be tremendously helpful. Thanks.
Don't put meaningful initialization in ChessGame's constructor, but instead override frameInit. When you do, also be sure to call super.frameInit(). See the javadoc or this tutorial.