내 프로젝트에는 abstract class Vehicle
을 상속하는 클래스 Car
과 Truck
이 있습니다. 그런 다음 무작위로 자동차 또는 차량을 총 10 회 추첨해야합니다. 또한 각각의 숫자가 JLabel
에 표시 될 것이지만 내 카운트는 JFrame
에 0으로 계속 표시됩니다. 여기 내 코드가있다.JLabel을 사용하여 카운트 표시?
package vehiclePackage;
import java.awt.Graphics;
import java.util.Random;
public abstract class Vehicle
{
public Vehicle() {
}
public Vehicle(int xLeft, int yTop) {
}
public abstract void draw(Graphics g2);
}
Car
등급 :
package vehiclePackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Random;
public class Car extends Vehicle
{
private int xLeft;
private int yTop;
private int ccount;
public Car() { }
public Car (int x, int y)
{
super(x,y);
this.xLeft = x;
this.yTop = y;
}
//@override
public void draw(Graphics g2)
{
Random rand = new Random();
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
Color randomColor = new Color(red, green, blue);
Rectangle body = new Rectangle (xLeft, yTop +10, 60, 10);
Ellipse2D.Double frontTire = new Ellipse2D.Double (xLeft+10, yTop+20, 10, 10);
Ellipse2D.Double rearTire = new Ellipse2D.Double (xLeft+40, yTop+20, 10, 10);
Point2D.Double r1 = new Point2D.Double(xLeft+10, yTop+10);
Point2D.Double r2 = new Point2D.Double(xLeft+20, yTop);
Point2D.Double r3 = new Point2D.Double(xLeft+40, yTop);
Point2D.Double r4 = new Point2D.Double(xLeft+50, yTop+10);
Line2D.Double frontWindshield = new Line2D.Double(r1,r2);
Line2D.Double roofTop = new Line2D.Double(r2,r3);
Line2D.Double rearWindshield = new Line2D.Double(r3,r4);
((Graphics2D) g2).setColor(randomColor);
((Graphics2D) g2).fill(body);
((Graphics2D) g2).fill(frontTire);
((Graphics2D) g2).fill(rearTire);
((Graphics2D) g2).draw(body);
((Graphics2D) g2).draw(frontTire);
((Graphics2D) g2).draw(rearTire);
((Graphics2D) g2).draw(frontWindshield);
((Graphics2D) g2).draw(rearWindshield);
((Graphics2D) g2).draw(roofTop);
}
}
Truck
등급 :
package vehiclePackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.util.Random;
public class Truck extends Vehicle
{
private int xLeft;
private int yTop;
private int tcount;
public Truck()
{
}
public Truck (int x, int y)
{
super(x, y);
this.xLeft = x;
this.yTop = y;
}
public void draw(Graphics g2)
{
Random rand = new Random();
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
Color randomColor = new Color(red, green, blue);
Rectangle body = new Rectangle (xLeft + 22, yTop +10, 60, 20);
Rectangle topCab = new Rectangle(xLeft, yTop + 10, 20, 10);
Rectangle bottomCab = new Rectangle(xLeft, yTop + 20, 20, 10);
Ellipse2D.Double frontTire = new Ellipse2D.Double (xLeft+6, yTop+30, 10, 10);
Ellipse2D.Double midTire = new Ellipse2D.Double (xLeft+25, yTop+30, 10, 10);
Ellipse2D.Double mid2Tire = new Ellipse2D.Double (xLeft+35, yTop+30, 10, 10);
Ellipse2D.Double backTire = new Ellipse2D.Double (xLeft+55, yTop+30, 10, 10);
Ellipse2D.Double back2Tire = new Ellipse2D.Double (xLeft+65, yTop+30, 10, 10);
((Graphics2D) g2).setColor(randomColor);
((Graphics2D) g2).fill(body);
((Graphics2D) g2).fill(bottomCab);
((Graphics2D) g2).fill(frontTire);
((Graphics2D) g2).fill(midTire);
((Graphics2D) g2).fill(mid2Tire);
((Graphics2D) g2).fill(backTire);
((Graphics2D) g2).fill(back2Tire);
((Graphics2D) g2).draw(body);
((Graphics2D) g2).draw(topCab);
((Graphics2D) g2).draw(bottomCab);
((Graphics2D) g2).draw(frontTire);
((Graphics2D) g2).draw(midTire);
((Graphics2D) g2).draw(mid2Tire);
((Graphics2D) g2).draw(backTire);
((Graphics2D) g2).draw(back2Tire);
}
}
VehicleComponent
등급 :
package vehiclePackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JComponent;
public class VehicleComponent extends JComponent
{
private int x;
private int y;
private static int ccount = 0;
private static int tcount = 0;
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Random rand = new Random();
for (int i = 1; i<=10; i ++)
{
int vehic = rand.nextInt(2);
if (vehic == 0)
{
x = rand.nextInt(420);
y = rand.nextInt(545);
Vehicle car = new Car(x, y);
car.draw(g2);
ccount += 1;
}
if (vehic ==1)
{
x = rand.nextInt(420);
y = rand.nextInt(545);
Vehicle truck = new Truck(x, y);
truck.draw(g2);
tcount += 1;
}
}
}
public String toString()
{
return "Trucks: " + tcount + ", Cars: " + ccount;
}
}
VehicleTester
:
package vehiclePackage;
import java.awt.BorderLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class VehicleViewer {
public static void main(String[] args)
{
// TODO Auto-generated method stub
JFrame f = new JFrame();
f.setSize(500,600);
f.setTitle("Cars and Trucks");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
VehicleComponent component = new VehicleComponent();
f.add(component);
JLabel runningCount = new JLabel(component.toString());
f.add(runningCount, BorderLayout.SOUTH);
f.setVisible(true);
}
}
'VehicleCompon ent'의'ccount'와'tcount'는'static'이 아니어야합니다. 이것은 VehicleComponent의 모든 인스턴스가 같은 값을 가질 것이라는 것을 의미합니다 ... – MadProgrammer
단순히 숫자를 제한하려고하는 것처럼 보입니다. 화면에 요소가 있습니까? 왜리스트에 추가하지 않겠습니까? – MadProgrammer
좋아, 정적이 없으면 시도했지만 여전히 같은 결과를 얻고 있습니다. – son2323