Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a code, and im supposed to return the rectangles area and perimeter on a JPanel. But when i execute nothing happens at all. Im suspecting an error somewhere in my methods cus i belive the rest is ok. Ill appricate all help.
Im just gonna show you my code in the JPanel.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rektanglar extends JPanel {
Rektanglar r1 = new Rektanglar ();
#Override
public void paintComponent (Graphics g) {
super.paintComponent (g);
g.drawString ("Rektanglar",10,20);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.fillRect(r1.getX(),r1.getY(), r1.getWidth(), r1.getHeight());
}
public int Y;
public int X;
public int width;
public int height;
public int Perimeter;
public int Area;
Rektanglar (){
width = 10;
height = 10;
X = 0;
Y = 0;
}
public void Rectangle(int x, int y, int w, int h) {
X = x;
Y = y;
width = w;
height = h;
}
public void setX(int X ){
this.X = X;
}
public int getX(int X){
return X;
}
public void setY(int Y){
this.Y = Y;
}
public int getY( int Y){
return Y;
}
public int getWidth( int width){
return width;
}
public int getHeight(int height){
return height;
}
public int getPerimeter(){
return (width + width + height + height );
}
public int getArea(){
return (height * width);}
}
}
If you ever actually try to construct an instance of Rektanglar, you'll get a stack overflow due to this:
public class Rektanglar extends JPanel {
Rektanglar r1 = new Rektanglar ();
...
}
That code says that in order to create one instance, you need to create another instance... which will create yet another instance, etc.
It's not at all clear why you've got r1 at all, but I strongly suggest you get rid of it...
I also suspect this:
public void Rectangle(int x, int y, int w, int h) {
X = x;
Y = y;
width = w;
height = h;
}
... is meant to be a constructor, in which case you'd have to write it as:
public Rektanglar(int x, int y, int w, int h) {
X = x;
Y = y;
width = w;
height = h;
}
Note that the name has to match the name of the class and you don't specify a return type.
Additionally, I'd suggest that:
You make all fields private
You compute the perimeter and area from the width and height, rather than keeping them as fields
You follow Java naming conventions for your variables (so x and y instead of X and Y).
Related
I want to change the height of my texture to a random height (with a specified range), and I almost figured out how, but the problem I have is that the texture (tower) is now above the ground. I want to stretch the texture while it remains on the same position, but changes height length.
I have a Tower class and a Scrollable class. In the Tower class I generate a random height in the reset method, but the problem is that I don't know what exactly I have to add or write to put the texture on the correct position (so that it isn't above the ground).
Here is the Tower class:
public class Tower extends Scrollable {
private Random r;
// When Tower's constructor is invoked, invoke the super (Scrollable)
// constructor
public Tower(float x, float y, int width, int height, float scrollSpeed) {
super(x, y, width, height, scrollSpeed);
// Initialize a Random object for Random number generation
r = new Random();
}
#Override
public void reset(float newX) {
// Call the reset method in the superclass (Scrollable)
super.reset(newX); // newX
// Change the height to a random number
Random r = new Random();
int low = 0;
int high = 15;
int result = r.nextInt(high-low) + low;
height = result;
}
}
And here's the Scrollable class:
public class Scrollable {
protected Vector2 position;
protected Vector2 velocity;
protected int width;
protected int height;
protected boolean isScrolledLeft;
public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
position = new Vector2(x, y);
velocity = new Vector2(scrollSpeed, 0);
this.width = width;
this.height = height;
isScrolledLeft = false;
}
public void update(float delta) {
position.add(velocity.cpy().scl(delta));
// If the Scrollable object is no longer visible:
if (position.x + width < 0) {
isScrolledLeft = true;
}
}
// Reset: Should Override in subclass for more specific behavior.
public void reset(float newX) {
position.x = newX;
isScrolledLeft = false;
}
public boolean isScrolledLeft() {
return isScrolledLeft;
}
public float getTailX() {
return position.x + width;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Maybe it's important to know that I have a GameRenderer class which has a drawTowers() method, which is then used in a render() method.
That's my drawTowers() method:
private void drawTowers() {
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight(),
tower1.getWidth(), midPointY - (tower1.getHeight()));
batcher.draw(AssetLoader.texture2, tower2.getX(), tower2.getY() + tower2.getHeight(),
tower2.getWidth(), midPointY - (tower2.getHeight()));
batcher.draw(AssetLoader.texture3, tower3.getX(), tower3.getY() + tower3.getHeight(),
tower3.getWidth(), midPointY - (tower3.getHeight()));
batcher.draw(AssetLoader.texture4, tower4.getX(), tower4.getY() + tower4.getHeight(),
tower4.getWidth(), midPointY - (tower4.getHeight()));
}
You are drawing the tower too high, you need to be adding half the height rather than the whole height.
Here in drawTowers():
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight() / 2, tower1.getWidth(), midPointY - (tower1.getHeight()));
Do the same for the other towers.
This may not be perfectly correct but it shouldn't be far off.
I have created an abstract shape class for 2d game but i am getting an error in both the shape classes. The error is something to do with super(). There maybe other errors as well. i have also shown where i get the error in the code. IS super() suitable to use.
Shape class
public abstract class Shape {
int Y;
int WIDTH;
int HEIGHT;
int DIAMETER;
public Shape(int Y, int WIDTH, int HEIGHT, int DIAMETER) {
this.Y = Y;
this.WIDTH = WIDTH;
this.HEIGHT = HEIGHT;
this.DIAMETER = DIAMETER;
}
public abstract void paint(Graphics g);
}
Racquet class
public class Racquet extends Shape {
int x = 0;
int xa = 0;
private Game game;
public Racquet(int Y, int WIDTH, int HEIGHT) {
super(Y, WIDTH, HEIGHT); // <- **Error Here**
}
public void move() {
if (x + xa > 0 && x + xa < game.getWidth() - this.WIDTH)
x = x + xa;
}
public void paint(Graphics r) {
r.setColor(new java.awt.Color(229, 144, 75));
r.fillRect(x, Y, this.WIDTH, this.HEIGHT);
}
public Rectangle getBounds() {
return new Rectangle(x, this.Y, this.WIDTH, this.HEIGHT);
}
public int getTopY() {
return this.Y - this.HEIGHT;
}
}
Ball Class
import java.awt.*;
public class Ball extends Shape {
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private Game game;
public Ball(Integer DIAMETER) {
super(DIAMETER); // <- **Error Here**
}
void move() {
if (x + xa < 0)
xa = game.speed;
if (x + xa > game.getWidth() - this.DIAMETER)
xa = -game.speed;
if (y + ya < 0)
ya = game.speed;
if (y + ya > game.getHeight() - this.DIAMETER)
game.CheckScore();
if (collision()) {
ya = -game.speed;
y = game.racquet.getTopY() - this.DIAMETER;
game.speed++;
}
x = x + xa;
y = y + ya;
}
private boolean collision() {
return game.racquet.getBounds().intersects(getBounds());
}
public void paint(Graphics b) {
b.setColor(new java.awt.Color(237, 238, 233));
b.fillOval(x, y, this.DIAMETER, this.DIAMETER);
}
public Rectangle getBounds() {
return new Rectangle(x, y, this.DIAMETER, this.DIAMETER);
}
}
Thanks a lot.
By calling super(...), you are actually calling constructor of super class. In the super class you have only one constructor which expects 4 parameters: Shape(int Y, int WIDTH, int HEIGHT, int DIAMETER), so you either have to provide 4 parameters when calling super(...), or provide needed constructors in the super class, with 3 parameters and with 1 parameter
Your Shape class doesn't have constructor with three parameters or one parameters.
You may want to use;
in recquet class
super(Y, WIDTH, HEIGHT, 0);
in Ball class
super(0, 0, 0, DIAMETER);
Shape doesn't have constructors that fit the parameters you are using in Racquet and Ball.
From a "best-practice" perspective, since Ball and Racquet should logically be constructed differently, it may be better to use composition, rather than inheritance.
I'm stuck on this question for a couple of days now and would really like to get some help.
I am given a 2 dimensional point in the range of (0-1 not including 1), such as (0.5,0.2), and N other points (also in the range of 0-1).
The first part of the question is to implement the "dumb" algorithm, which when given a certain point will find the point with the shortest distance from it, which has a complexity of O(N).
The part I'm stuck at, requires to build a Matrix K on K, where each "cell" will contain the points that belong to that cell. Once done, when given the original point I will need to search for the point with the shortest distance to it only in some of the cells and not the entire Matrix, which should result better complexity.
My original thought is to devide the points so that each block will have an arraylist of points that belong to him, and then to somehow go through the main block(the one that the original point belongs to) and continue by going through it's neighbors, however implementing it hasn't been very successful.
I would highly appreciate any help/ advice.
Below is what I currently have:
public class Point {
private double x;
private double y;
private Block b;
public Point(double x, double y)
{
this.x=x;
this.y=y;
}
public Point(double x, double y, Block b) //consrtuctor for complex case
{
this.x=x;
this.y=y;
b.points.add(this);
}
public double getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double distance(Point p)
{
double res=0;
res = Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2));
return res;
}
}
import java.util.ArrayList;
public class Block {
private int x;
private int y;
public ArrayList<Point> points;
public Block(int x, int y) {
points = new ArrayList<Point>();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
import java.util.Random;
public class ComplexCase {
private Block[][] blockMat;
public ComplexCase(int k, int n)
{
Random generator = new Random();
Point p1;
Block b1;
double x,y;
int bx1,by1;
int t;
t = 1/k;
blockMat = new Block[k][k];
for (int i =0;i<n;i++)
{
x = generator.nextDouble();
y = generator.nextDouble();
bx1 = (int) (x/t);
by1 = (int) (y/t);
b1 = new Block(bx1,by1);
p1 = new Point(x,y,b1);
}
}
public Block[][] getBlockMat() {
return blockMat;
}
public void setBlockMat(Block[][] blockMat) {
this.blockMat = blockMat;
}
}
I just want to make sure I'm correct here. I am trying to add methods to
Change height
Change width
Change coordinates
Calculate perimeter
Calculate area
public class MyRectangle {
public int width;
public int height;
public int y;
public int x;
public MyRectangle()
{
width=10;
height=10;
y=10;
x=10;
public int MyRectangle;
public MyRectangle(int width, int height, int y, int x, int MyRectangle) {
this.width = width;
this.height = height;
this.y = y;
this.x = x;
this.MyRectangle = MyRectangle;
}
}
and I'm also getting a illegal start of expression error on my method.
This is your problem, you can't have methods within a method.
But this was due to you not closing your brackets for your methods.
I fixed your code and added the methods you wanted:
public class MyRectangle {
//Best to group your variables up here
public int MyRectangle;
public int width;
public int height;
public int y;
public int x;
public MyRectangle() {
width = 10;
height = 10;
y = 10;
x = 10;
}//Make sure to close this method with the bracket
public MyRectangle(int width, int height, int y, int x, int MyRectangle) {
this.width = width;
this.height = height;
this.y = y;
this.x = x;
this.MyRectangle = MyRectangle;
}
/**
* Changes the current height to the given new height
* #param newHeight
*/
public final void changeHeight(int newHeight) {
height = newHeight;
}
/**
* Changes the current width to the given new width
* #param newWidth
*/
public final void changeWidth (int newWidth) {
width = newWidth;
}
/**
* Calculates the current perimeter based on the width and height
* #return parameter ofd the rectangle
*/
public final int getPerimeter() {
return ((2 * width) + (2 * height));
}
/**
* Calculates the area based on the width and height
* #return area of the rectangle
*/
public final int getArea() {
return (width * height);
}
public final void changesXCoordinate(int newX){
x = newX;
}
public final void changesYCoordinate(int newY){
y = newY;
}
public final void changesCoordinate(int newX, int newY) {
x = newX;
y = newY;
}
}
I will explain more soon, just wanted to post the correct code first :P
As it stands, it's kinda hard to understand what else you are looking for.
If this is what you are looking for, please mark this as the correct answer :D
I'm stuck on something that is usually really simple. I'm getting a NullPointerException when calling this simple class's constructor:
import java.awt.geom.*;
public class Brick extends ColorShape {
private int xPos = 0;
private int yPos = 0;
private int width = 0;
private int height = 0;
private Rectangle2D.Double shape;
// constructor
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
//set brick x, y, width, and height
xPos = x;
yPos = y;
width = w;
height = h;
// update shape
shape.setRect((double)xPos, (double)yPos, (double)width, (double)height);
}
public int getX() {
return xPos;
}
public Rectangle2D.Double getShape() {
return shape;
}
}
It gets called this way:
for (int i = 0; i < numCols; i++) {
for (int j = 0; j < numRows; j++) {
// initialize bricks[i][j]
bricks[i][j].setLocation((double)(i*brickWidth), (double)(j*brickHeight));
bricks[i][j].setSize((double)brickWidth, (double)brickHeight);
//bricks[i][j] = new Brick(i*brickWidth, j*brickHeight, brickWidth, brickHeight);
//bricks[i][j] = new Brick(0, 0, 0, 0);
}
}
No matter what I try, I always get a NullPointerException trying to initialize that class.
EDIT:
Tristan's suggestions along with changing the nested for loops to the code below fixed it
// create new bricks and store them in bricks array
for (int i = 0; i < numCols; i++) {
for (int j = 0; j < numRows; j++) {
// initialize bricks[i][j]
//bricks[i][j].setLocation((double)(i*brickWidth), (double)(j*brickHeight));
//bricks[i][j].setSize((double)brickWidth, (double)brickHeight);
bricks[i][j] = new Brick(i*brickWidth, j*brickHeight, brickWidth, brickHeight);
//bricks[i][j] = new Brick(0, 0, 0, 0);
}
}
I think you are accidentally redeclaring shape as an uninitialized field. The shape you are calling setRect on has not been initialized the way you think it has.
If you have a shape in the parent class that you are trying to access, simply set its modifier to protected and remove the private shape declaration in the class you posted.
/* REMOVE THIS */
private Rectangle2D.Double shape; // uninitialized!
// constructor
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
//set brick x, y, width, and height
xPos = x;
yPos = y;
width = w;
height = h;
// update shape
// This now references a protected instance variable inherited from the parent.
shape.setRect((double)xPos, (double)yPos, (double)width, (double)height);
}
However looking through this constructor, it seems rather off. If it is the case that the parent class has a shape itself, why do you need to set the rectangle any differently than the way you set it in the parent class?
For example, this code appears to be logically equivalent.
// Call the parents constructor to set the shape of this brick..
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
}
As mentioned by Tristan, the problem with the initial brick constructor is that shape has been declared but not instantiated.
That said, it is simple to instantiate shape:
public Brick(int x, int y, int w, int h) {
super(new Rectangle2D.Double(x, y, w, h));
//set brick x, y, width, and height
xPos = x;
yPos = y;
width = w;
height = h;
// update shape
// This now references a protected instance variable inherited from the parent.
shape = (Rectangle2D.Double)super.shape;
shape.setRect(xPos, yPos, width, height);
}