Я в настоящее время изучаю класс CS C и борюсь с лабораторным заданием.
Мы используем BlueJ для изучения базовых c Java функций, а наш профессор не имеет научил нас тому, что на самом деле что-то значит или как на самом деле это понять. Нам дают пакет с инструкциями о том, что добавить к уже существующей сцене. В настоящее время я должен добавить мерцающие звезды и падающие звезды к горизонту. Мне удалось добавить одну падающую звезду, и код выглядит следующим образом:
import cha.*;
import cha.action.*;
import java.awt.*;
import java.util.*;
import static java.lang.Math.*;
import static java.awt.Color.*;
public class NightScene
extends CHApplet {
Random gen;
int horizon;
Sky sky;
CHLabel name;
CHOval [] stars;
Color offwhite;
Twinkle tw;
CHOval shooter;
Shoot st;
CHClip shootingstar;
private class Twinkle
extends CHAction {
public void step() {
Random gen3;
gen3 = new Random();
int v;
v = gen3.nextInt(stars.length);
stars[v].setVisible(false);
sleep(500);
// 1000 didn't show any twinkle. Lower number to show.
stars[v].setVisible(true);
}
}
private class Shoot
extends CHAction {
public void step() {
shooter.moveOneStep();
}
public void before() {//THIS IS A METHOD
Random gen4;
gen4 = new Random();
int xl = gen.nextInt(getWidth());
int yl = gen.nextInt(horizon);
shooter.setBounds(xl,yl,5,5);
shooter.setVisible(true);
int xv = gen.nextInt(-10 + 20);
int yv = gen.nextInt(-10 + 20);
shooter.setVelocity(xv,yv);
shootingstar.play();
}
public void after() {
shooter.setVisible(false);
}
}
public void init() {
setBackground(black);
gen = new Random();
horizon = getHeight() * 4 / 5;
sky = new Sky();
add(sky);
sky.setBounds(0, 0, getWidth(), horizon);
Color darkGreen;
darkGreen = new Color(0, 102, 0);
CHRectangle lawn;
lawn = new CHRectangle();
add(lawn, 0);
int lh;
lh = getHeight() - horizon;
lawn.setBounds(0, horizon, getWidth(), lh);
lawn.setBackground(darkGreen);
lawn.setForeground(darkGreen);
offwhite = new Color(255, 255, 204);
Random gen2;
gen2 = new Random();
stars = new CHOval[500];
for(int i = 0; i <stars.length; i++) {
stars[i] = new CHOval();
//CHOval oval; FA incorrect
add (stars[i],0);
int x;
x = gen2.nextInt(800);
// Prof used x = gen2.nextInt(getWidth());
int y;
y = gen2.nextInt(horizon);
// Prof used y = gen2.nextInt(horizon);
stars[i].setBounds(x,y,1,1);
stars[i].setBackground(offwhite);
stars[i].setForeground(offwhite);
}
tw = new Twinkle();
add(tw);
tw.setLimit(1000);
shooter = new CHOval();
add(shooter,0);
shooter.setBackground(offwhite);
shooter.setForeground(offwhite);
shooter.setVisible(false);
st = new Shoot();
add(st);
Random gen5;
gen5 = new Random();
int sl = gen.nextInt(10 + 20);
st.setLimit(sl);
shootingstar = new CHClip();
add(shootingstar);
shootingstar.setFile("shootingstar.wav");
House house;
house = new House();
add(house, 0);
int hy = horizon - house.getHeight() * 3 / 4;
house.setLocation(100, hy);
} // end of init - DO NOT REMOVE
public void start( ) {
tw.start();
st.start();
}
/***********************************************************
* DO NOT Change the code below this line.
***********************************************************/
public static void run() {
int width = 800;
int height = 600;
CHApplet applet = new NightScene();
applet.run(width, height);
}
private NightScene(){}
}
Когда получено указание изменить этот код для создания «бесконечного l oop» падающих звезд,
Создайте еще один закрытый класс ShootMany, который расширяет CHAction. Единственным методом в этом классе будет метод step (). В этом методе спите случайное количество времени - создайте случайное число от 100 до 5000, затем используйте случайное число в качестве параметра в режиме ожидания. Затем вырежьте операторы для создания, имени, добавления и запуска объекта типа Shoot из методов init () и start () и поместите их в новый шаг ().
В методе init () , объявить, создать, добавить и запустить объект типа ShootMany. Используйте .setLimit (5000), чтобы повторять действие съемки более или менее бесконечно.
Из созданного частного класса мой код теперь выглядит следующим образом:
private class Shoot
extends CHAction {
public void step() {
shooter.moveOneStep();
}
public void before() {//THIS IS A METHOD
Random gen4;
gen4 = new Random();
int xl = gen.nextInt(getWidth());
int yl = gen.nextInt(horizon);
shooter.setBounds(xl,yl,5,5);
shooter.setVisible(true);
int xv = gen.nextInt(-10 + 20);
int yv = gen.nextInt(-10 + 20);
shooter.setVelocity(xv,yv);
shootingstar.play();
}
public void after() {
shooter.setVisible(false);
}
}
private class ShootMany
extends CHAction {
public void step () {
Random gen5;
gen5 = new Random ();
int rs = gen.nextInt(100 + 4900);
sleep(rs);
st = new Shoot();
add(st);
int rl = gen.nextInt(10 + 20);
st.setLimit(rl);
st.start();
}
}
public void init() {
setBackground(black);
gen = new Random();
horizon = getHeight() * 4 / 5;
sky = new Sky();
add(sky);
sky.setBounds(0, 0, getWidth(), horizon);
Color darkGreen;
darkGreen = new Color(0, 102, 0);
CHRectangle lawn;
lawn = new CHRectangle();
add(lawn, 0);
int lh;
lh = getHeight() - horizon;
lawn.setBounds(0, horizon, getWidth(), lh);
lawn.setBackground(darkGreen);
lawn.setForeground(darkGreen);
offwhite = new Color(255, 255, 204);
Random gen2;
gen2 = new Random();
stars = new CHOval[500];
for(int i = 0; i <stars.length; i++) {
stars[i] = new CHOval();
//CHOval oval; FA incorrect
add (stars[i],0);
int x;
x = gen2.nextInt(800);
// Prof used x = gen2.nextInt(getWidth());
int y;
y = gen2.nextInt(horizon);
// Prof used y = gen2.nextInt(horizon);
stars[i].setBounds(x,y,1,1);
stars[i].setBackground(offwhite);
stars[i].setForeground(offwhite);
}
tw = new Twinkle();
add(tw);
tw.setLimit(1000);
shooter = new CHOval();
add(shooter,0);
shooter.setBackground(offwhite);
shooter.setForeground(offwhite);
shooter.setVisible(false);
ShootMany stm;
stm = new ShootMany();
add(stm);
stm.setLimit(5000);
shootingstar = new CHClip();
add(shootingstar);
shootingstar.setFile("shootingstar.wav");
House house;
house = new House();
add(house, 0);
int hy = horizon - house.getHeight() * 3 / 4;
house.setLocation(100, hy);
} // end of init - DO NOT REMOVE
public void start( ) {
tw.start();
}
/***********************************************************
* DO NOT Change the code below this line.
***********************************************************/
public static void run() {
int width = 800;
int height = 600;
CHApplet applet = new NightScene();
applet.run(width, height);
}
private NightScene(){}
}
Падающие звезды не отображаются. Я пробовал несколько разных вещей, которые, как я думал, будут работать, но я большой нуб и ничего не знаю о самом кодировании. Может ли кто-нибудь помочь мне?