Entity not spawning after being summoned Java - java

So I am trying to call an entity called Mothership with a texture. What should be happening is that the image appears where the player is (temporary place till it appears). Instead, no matter where I move it, it does not appear.
This dropbox link is to the whole game because it all links together.
https://www.dropbox.com/sh/kuvoxzjf00wa8jf/AAD-MaXXcnHMn4PtW-X_vHcUa?dl=0
I thought maybe it's because of Controller.java, not initialising it but I was getting errors. In the end, I want the mothership to be stationary and appear on the side that the player spawns on. The mothership will lose health from enemy bullets which I am working on at the moment.
But here are the bits that I have been focussing my attention to:
public void render(Graphics g) {
g.drawImage(tex.nmothership, (int) x, (int) y, null);
}
^This is in Mothership.java
public void init(){
requestFocus();
BufferedImageLoader loader = new BufferedImageLoader();
try {
spriteSheet = loader.loadImage("/spriteSheet.png");
background = loader.loadImage("/background.png");
}catch(IOException e) {
e.printStackTrace();
}
tex = new Textures(this);
c = new Controller(tex, this);
p = new Player(200, 200, tex, this, c);
menu = new Menu();
mothership = new Mothership(200, 200, tex);
ea = c.getEntityA();
eb = c.getEntityB();
this.addKeyListener(new KeyInput(this));
this.addMouseListener(new MouseInput());
c.createEnemy(enemy_count);
//e = error
}
^In Game.java (the main engine of the game)
private void getTextures() {
player = ss.grabImage(1, 1, 32, 32);
missile = ss.grabImage(2, 1, 32, 32);
enemy = ss.grabImage(3, 1, 32, 32);
emissile = ss.grabImage(4, 1, 32, 32);
nmothership = ss.grabImage(1, 1, 128, 128);
^In Textures.java - the grabImage is from Spritesheet.java

After some playing around I managed to get the "mothership" to spawn however it has come at the price of commenting out the mothership's physics mechanic. I will make another post about it if I am still having trouble.

Related

how do I detect key presses with just one method in java?

I'm trying to paint 6 colors, 10 shapes, a jpeg image, and 3 things that change when a key is pressed in java.
However I'm trying to program all of this in one method. I've done everything else except detecting a key press. I've looked at a lot of examples, but all of them use different methods or classes that work together to detect a key press. But how to do I detect key presses and at the same time change pictures/images in JPanel all in just one method.
I tried to write some code but it doesn't work (nothing shows up on the JPanel/screen) Here's my code so far:
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, 1000, 1000);
BufferedImage photo = null;
try
{
File file = new File("holiday.jfif");
photo = ImageIO.read(file);
}
catch (IOException e)
{
g.drawString("Problem reading the file", 100, 100);
}
g.drawImage(photo,0,0,800,800,null);
g.setColor(Color.BLACK);
g.fillRect(120,300,100,100 );
g.setColor(Color.GREEN);
g.fillOval(270,130,50,100 );
g.setColor(Color.BLUE );
g.fillRect(350,200,100,150 );
g.setColor(Color.PINK);
g.fillOval(460, 270, 50, 50);
g.setColor(Color.GRAY);
g.fillArc(500, 300, 110, 100, 5, 150);
g.setColor(Color.YELLOW);
g.fillRect(500,450,100,100);
g.setColor(Color.YELLOW);
g.fillOval(550,500,100,100);
int[] xpoints = {50,150,20,180};
int[] ypoints = {500,500,550,550};
g.setColor(Color.BLACK);
g.drawPolygon(xpoints,ypoints,4);
int[] xpoint = {250,220,380,350};
int[] ypoint = {500,550,550,500};
g.drawPolygon(xpoint,ypoint,4);
int[] x = {600,500,700};
int[] y = {20,100,100};
g.drawPolygon(x,y,3);
g.setColor(Color.ORANGE);
g.fillRect(10,50,220,30);
g.fillRect(115,80,20,100);
Scanner input = new Scanner(System.in);
String press;
boolean stop = false;
while(!stop)
{
//Scanner input = new Scanner(System.in);
press = input.next();
if (press != null)
{
g.clearRect(115,80,20,100);
g.clearRect(500,450,100,100);
stop = true;
}
}
}

Processing - Make an object disappear and appear in certain frames of time

This is my current code:
int doorCounter = 0;
void setup()
{
size(512, 348); //width and height of screen
doorCounter = (int)random(180,300);
}
void draw()
{
display();
doorCounter = doorCounter - 1; // Decrease count by 1
if (doorCounter <= 0)
{
fill(255);
rect(420, 190, 55, 100); //house door outline
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
doorCounter = (int)random(180,480);
}
}
void display()
{
fill(255);
rect(420, 190, 55, 100); //house door outline
fill(0,0,0); // fill the following polygons in black
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
}
However what this code does is just makes the object disappear for a fraction of a second and just makes it reappear instantly. How do I make it so that the object stays disappeared for 3-8 seconds on a random interval just like how the object disappears every 3-8 seconds given that its still on the screen?
P.s I don't know if what I'm trying to achieve makes sense so please feel free to question.
An idea is to use a timestamp and check the time elapsed from it, something like this:
int min_time = 3000; // in ms
int max_time = 8000; // in ms
int time_frame = (int)random(min_time, max_time);
int time_stamp = 0;
boolean show_door = true;
void setup()
{
size(512, 348); //width and height of screen
}
void draw()
{
background(200);
int time_passed = millis() - time_stamp;
if (time_passed < time_frame && show_door) {
display();
} else if (time_passed >= time_frame) {
time_stamp = millis();
time_frame = (int)random(min_time, max_time);
show_door = !show_door;
}
}
void display()
{
fill(255);
rect(420, 190, 55, 100); //house door outline
fill(0, 0, 0); // fill the following polygons in black
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
}

Adding music to my sound board project

So I'm creating a soundboard for my CS project using java. The soundboard consists of an 8 piece drum set that is supposed to play its set sound when clicked. I've set the area to be clicked but don't know how to implement sound that'll start when the set area is clicked.
// FinalProjectst.java
// AP Computer ScienceStudent Version
import java.awt.*;
import java.applet.*;
import java.awt.geom.Ellipse2D;
public class FinalProjectst extends Applet
{
Image picture;
Ellipse2D base, bT, snare, lT, rT, hh, lC, rC;
int numColor;
public void init()
{
picture = getImage(getDocumentBase(),"drumSet.jpg");
base = new Ellipse2D.Double (355, 415, 305, 240); //Bass
bT = new Ellipse2D.Double (715, 360, 325, 245); //Bottom Tom
snare = new Ellipse2D.Double ( 35, 410, 290, 200); //Snare
lT = new Ellipse2D.Double (283, 130, 185, 165); //Left Tom
rT = new Ellipse2D.Double (543, 120, 200, 175); //Right Tom
hh = new Ellipse2D.Double ( 0, 225, 250, 150); //High Hat
lC = new Ellipse2D.Double ( 10, 0, 305, 195); //Left Cymbal
rC = new Ellipse2D.Double (765, 0, 505, 275); //Right Cymbal
}
public boolean contains(Event e, int x, int y)
{
if(base.contains(x,y))
numColor = 1;
else if(bT.contains(x,y))
numColor = 2;
else if(snare.contains(x,y))
numColor = 3;
else if(lT.contains(x,y))
numColor = 4;
else if(rT.contains(x,y))
numColor = 5;
else if(hh.contains(x,y))
numColor = 6;
else if(lC.contains(x,y))
numColor = 7;
else if(rC.contains(x,y))
numColor = 8;
else
numColor = 9;
repaint();
return true;
}
public void paint(Graphics g)
{
g.drawImage(picture, 0, 0, this);
}
}
There are a lot of different ways to play a sound but for example you could do something like this:
public static void playSound(File soundfile) throws LineUnavailableException, UnsupportedAudioFileException, IOException{
AudioInputStream audioInputStream = null;
audioInputStream = AudioSystem.getAudioInputStream(soundfile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}
This code will play wav files without a problem and I think that it will also play other types of sound files, but I am not sure what sound types it will play and what sound types it will not.
I hope this help :)
EDIT:
As you can see there are a lot of exceptions that can be thrown from this code, so you probably want to handle them in an appropriate way.

Refresh text in Applet

I am doing this small race between two cars, in a java applet.
Just two pictures moving at random speed. I am calculating the distance between current position and the finish line, and you are suppose to be able to see the distance in the upper corner.
The thing is I am not able to refresh the text field, instead it just applies a new layer on top of the old number so it is almost impossible to read.
Here are pictures to demonstrate my problem.
I thought I would be able to solve it by creating the blue rectangle at the start of each loop but that does not seem to solve it.
public void action(){
Random rand = new Random();
boolean race = true;
int x1 =500, y1 = 233;
int x2 = 500, y2 = 333;
int speed1 = rand.nextInt(15) + -16;
int speed2 = rand.nextInt(15) + -16;
int finishline = 30;
Text winnerBlue = new Text("Winner: BLUE",new Font("SansSerif",Font.BOLD,20), Color.blue,Color.white);
Text winnerRed = new Text("Winner: RED",new Font("SansSerif",Font.BOLD,20), Color.red,Color.white);
//background
Text text =null;
Text text2 = null;
window.fillRect(0, 0, 600, 400, Color.GREEN);
//track 1
window.fillRect(20, 330, 550, 39, Color.gray);
//track2
window.fillRect(20, 230, 550, 39, Color.gray);
//Finish line
window.fillRect(40, 210, 10, 180, Color.BLACK);
while(race){
text = new Text(Integer.toString(x1),new Font("Courier",Font.BOLD,20), Color.WHITE);
text2 = new Text(Integer.toString(x2),new Font("SansSerif",Font.BOLD,20), Color.WHITE);
window.fillRect(0, 0, 70, 50, Color.blue);
window.fillRect(70, 0, 70, 50, Color.red);
window.showImage(text, 0, 0);
window.showImage(text2, 70, 0);
window.showImage(car1.getImage(), x1, y1);
window.showImage(car2.getImage(), x2, y2);
car1.moveTo(x1 += speed1, y1);
car2.moveTo(x2 += speed2, y2);
window.pause(50);
if(x1 <= (finishline ) ){
speed1 = 0;
speed2 = 0;
window.showImage(winnerBlue, 200, 200);
race = false;
}
if(x2 <= (finishline)){
speed2 = 0;
speed1 = 0;
window.showImage(winnerRed, 200, 200);
race = false;
}
}
}
}
For the two screen shots and supplied code snippt, it's clear that you don't understand how painting works in Swing/AWT.
Do NOT ever, maintain any kind of refernce to the Graphics context out side of the paintXxx methods.
The paint methods perform a number of very important steps to prepare the Graphics context for painting
Start by taking a look through Performing Custom Painting

LibGDX touch position coords error

So, I'm using LibGdx and trying to use their Rectangle class as a bounds for button pressing onn the touch screen. It works perfectly on a 1:1 scale, but when I put the game on my Phone (With a smaller screen) than the images get scaled and drawn properly, but the Rectangles don't. So I tried keeping my Rectangles at their normal scale, and "upscaling" the touch screen's XY Coords, but I guess I'm not doing that right, cause it doesn't work.
optionsMenu = new Vector<Rectangle>();
optionsMenu.add(new Rectangle(100 + (120 * 0), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 1), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 2), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 3), 100, 100, 100));
That's how i'm initalizing my bounding Rectangles.
This is how I'm initalizing my camera:
camera = new OrthographicCamera();
camera.setToOrtho(true, 800, 480);
This is how I'm drawing my buttons:
spriteBatch.draw(buttonImage, optionsMenu.get(2).bounds.getX(),
optionsMenu.get(2).bounds.getY(), optionsMenu.get(2).bounds.getWidth(),
optionsMenu.get(2).bounds.getHeight(), 0, 0, buttonImage.getWidth(),
buttonImage.getHeight(), false, true);
And this is how I do my touch screen logic:
public boolean tap(float x, float y, int count, int button) {
Vector3 temp = new Vector3(x, y, 0);
camera.unproject(temp);
float scalePos = (int) ((float) Gdx.graphics.getWidth()/800.0f);
temp.x = temp.x * scalePos;
temp.y = temp.y * scalePos;
if(optionsMenu.get(0).bounds.contains(temp.x, temp.y)){
//do sutff
}
else if(optionsMenu.get(1).bounds.contains(temp.x, temp.y)){
//do other stuff
}
return false;
}
A few days ago I ran into the same problem. This is what I did to solve it:
If you are using a viewport, then you should add that data to the camera.unproject call, to make sure that the viewport is being taken into account.
For example:
camera.unproject(lastTouch,viewport.x,viewport.y,viewport.width,viewport.height);
To debug the rectangle bounds and the touch position, I used this method to draw them into the screen:
private static ShapeRenderer debugShapeRenderer = new ShapeRenderer();
public static void showDebugBoundingBoxes(List<Rectangle> boundingBoxes) {
debugShapeRenderer.begin(ShapeType.Line); // make sure to end the spritebatch before you call this line
debugShapeRenderer.setColor(Color.BLUE);
for (Rectangle rect : boundingBoxes) {
debugShapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
}
debugShapeRenderer.end();
}

Categories

Resources