私はかなりの時間この問題に立ち往生してきましたが、私は解決策を理解できないようです。私は現在、駐車場をシミュレートするプロジェクトに取り組んでいます。駐車場自体は問題ではありません。シミュレーションされるべきいくつかのタイプの顧客です。物事を少し簡単にするために、私は1つの解決法を尋ねるでしょう。それで私は自分自身で他の人を解くことができるはずです。 初心者の方には、パーパスパスを持つ顧客のために別のクラスを作成し、パスホルダを保有している車とそうでない車を示す方法で統合する必要があります。Java、複数のクラスとそれらの複数のブール値
import java.util.Random;
/* creates a boolean called isPass that is randomly picked to be true or false. */
public interface ParkPass {
public Random rnd = new Random();
public boolean isPass = rnd.nextBoolean();
}
これは、私がランダムに駐車場を設定できるクラスです。シミュレーションは別のクラスを介して行われるので、私が行うことができるのは、Passをtrueまたはfalseに設定するメソッドを作成することだけです。このクラスでは、パス自体を設定することはできません。
public abstract class Car {
private Location location;
private int minutesLeft;
public boolean isPaying;
public boolean isBlue;
public void setIsPaying(boolean isPaying) {
this.isPaying = isPaying;
}
// added a method to allow us to set the colour of the car to blue for when they have a parking pass.
public void setIsBlue(boolean isBlue) {
this.isBlue = isBlue;
}
これに属し、私はこのシミュレーションで行くことにしようとしているどの方向をお見せ可能性があるブール値を示しCarクラスの小さな断片です。
public class AdHocCar extends Car implements ParkPass{
public AdHocCar() {
setIsBlue(isPass);
setIsPaying(!isPass);
}
}
これは、駐車場に出入りする車をシミュレートするときに呼び出されるクラスです。ここでは、IsPlueクラスとIsPayingブール値をCarクラスに設定するためにParkPassクラスを実装しようとしたのを見ることができます。次のコードで呼び出すことができます。修正する。
import javax.swing.*;
import java.awt.*;
public class SimulatorView extends JFrame {
private CarParkView carParkView;
private int numberOfFloors;
private int numberOfRows;
private int numberOfPlaces;
private Car[][][] cars;
public void updateView() {
/* Create a new car park image if the size has changed.
added 2 colours to show the difference between the three different customer types.*/
if (!size.equals(getSize())) {
size = getSize();
carParkImage = createImage(size.width, size.height);
}
Graphics graphics = carParkImage.getGraphics();
for(int floor = 0; floor < getNumberOfFloors(); floor++) {
for(int row = 0; row < getNumberOfRows(); row++) {
for(int place = 0; place < getNumberOfPlaces(); place++) {
Location location = new Location(floor, row, place);
Car car = getCarAt(location);
Color color = car == null ? Color.white : Car.isBlue ? Color.blue /*: isReservation == true ? Color.green*/ :Color.red ;
drawPlace(graphics, location, color);
}
}
}
repaint();
}
そしてここで、我々は最終的に私が直面している問題に着きます。あなたが今それを見るならば、おそらくかなり間違っていることに気付くでしょう。これは、10時間の研究と色属性の絶え間ない変更の後、私は、以前の2つのタイプの顧客の違いを示すために以前に作成されたブール値を実装しようとしていた正確な方法のトラックを失ったからです。私はあまりプログラミングに慣れていないので、ちょっと待ってからここで尋ねることにしました。
ここでは、これらの別々のクラスすべてで独自のブール値を作成しています。シミュレーションを使用すると、パーキングパスを使用する車は青色になりますが、通常支払う必要がある車は赤色?
'Color color = car == null? Color.white:Car.isBlue? Color.blue/*:isReservation == true? Color.green * /:Color.red; 'この部分は修正されているようです。あなたは 'drawPlace'メソッドの描画が正しいことを確かめていますか? – rdonuk
プライベートvoid drawPlace(グラフィックスグラフィックス、場所の場所、色の色){ graphics.setColor(color); graphics.fillRect( location.getFloor()* 260 +(1 +(int)Math.floor(location.getRow()* 0.5))* 75 +(location.getRow()%2)* 20、 60 + location.getPlace()* 10、 20 - 1、 10 - 1); // TODOは動的なサイズや定数を使用する } ここで何も間違っているとは思わないが、問題は他のクラスのブール値設定と関係があると思う。 –