我的项目包含继承自Car
的课程Truck
和abstract class Vehicle
。然后随机抽取汽车或车辆共10次。此外,还要计算每个JLabel
中显示的数量,但我的计数在JFrame
中显示为0。这是我的代码。
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);
}
}
答案 0 :(得分:1)
您的问题是您在调用paintComponent
例程之前计算标签文本的值。一旦调用,文本将保留旧值。
我建议的编辑方法是将要绘制哪辆车的图形移动到VehicleComponent
的构造函数,然后在绘制它们时,你可以查看预先填充的数组以确定应该是什么被绘制,计数已经更新。
查看VehicleComponent
public class VehicleComponent extends JComponent
{
private int x;
private int y;
private int ccount = 0; // these shouldn't be static
private int tcount = 0;
private boolean[] truckOrCar; // simple boolean array (you can change the type as you add more vehicles...)
public VehicleComponent() {
// figure out your car/truck count here
Random rand = new Random();
truckOrCar = new boolean[10];
for (int i = 0; i<10; i ++)
{
int vehic = rand.nextInt(2);
if (vehic == 0)
{
// false for car
truckOrCar[i] = false;
ccount += 1;
}
else {
// true for truck
truckOrCar[i] = true;
tcount += 1;
}
}
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Random rand = new Random();
// here just look in your list for what to draw
for (int i = 0; i<10; i ++)
{
// false for car
if ( !truckOrCar[i] )
{
x = rand.nextInt(420);
y = rand.nextInt(545);
Vehicle car = new Car(x, y);
car.draw(g2);
ccount += 1;
}
// true for truck
else
{
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;
}
}
查看结果(虽然你的油漆坐标有点偏......):
答案 1 :(得分:1)
我认为你不理解的是绘画在Swing中的实际效果。绘画是破坏性的,也就是说,每次绘制组件时,都需要从头开始完全重绘其整个状态......
请查看Performing Custom Painting和Painting in AWT and Swing了解详情......
所以你循环使用paintComponent
方法,如果你真的很幸运,只会画一辆车。
相反,您需要将车辆保存在某种List
或数组中,只需让paintComponent
绘制列表......
所以,我们可以改变VehicleComponent
因此......
public class VehicleComponent extends JComponent {
private List<Car> cars;
private List<Truck> trucks;
private Random rnd = new Random();
public VehicleComponent() {
cars = new ArrayList<>(25);
trucks = new ArrayList<>(25);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (Car car : cars) {
car.draw(g2);
}
for (Truck truck : trucks) {
truck.draw(g2);
}
}
@Override
public String toString() {
return "Trucks: " + trucks.size() + ", Cars: " + cars.size();
}
}
现在只需绘制组件重新绘制时cars
和trucks
列表中的内容即可...
现在,其余部分假设我正确理解您的要求......
接下来,我们需要一些方法将新车辆添加到列表中......
我不知道你打算如何更新组件,我写了这个简单的方法,在0-4
和1
之间生成一个随机数,它创建了Car
和3
它生成一个Truck
,您可以将其更改为您喜欢的任何内容。在创建新车辆之前,它会检查新车辆列表中是否有可用空间......
public void addNewVehicle() {
int value = (int) Math.round(Math.random() * 5);
if (value == 1) {
if (cars.size() < 10) {
cars.add(new Car(getSize()));
}
} else if (value == 3) {
if (trucks.size() < 10) {
trucks.add(new Truck(getSize()));
}
} else {
System.out.println("No new toys");
}
repaint();
}
要触发更新,我使用Swing Timer
,每秒调用addNewVehicle
方法......
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
component.addNewVehicle();
runningCount.setText(component.toString());
}
});
timer.start();
添加addNewVehicle
方法的随机性,这意味着您不会每秒都看到添加的车辆。这也会同时更新runningCount
标签,使其保持同步......
现在,考虑到绘画过程的本质,我改变了你的Truck
和Car
绘画的方式,而不是每次绘制时随机化颜色,我在每个实例创建时随机化颜色代替
public class Car extends Vehicle {
private int xLeft;
private int yTop;
private Color color;
public Car(Dimension size) {
xLeft = (int) Math.round((Math.random() * size.width - 60));
yTop = (int) Math.round((Math.random() * size.height - 10));
Random rand = new Random();
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
color = new Color(red, green, blue);
}
@Override
public void draw(Graphics g2) {
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(color);
((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);
}
}
public class Truck extends Vehicle {
private int xLeft;
private int yTop;
private Color color;
public Truck(Dimension size) {
xLeft = (int) Math.round((Math.random() * size.width - 60));
yTop = (int) Math.round((Math.random() * size.height - 20));
Random rand = new Random();
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
color = new Color(red, green, blue);
}
@Override
public void draw(Graphics g2) {
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(color);
((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);
}
}
现在,因为这可能是一些努力重新组合在一起,这是我用来测试它的代码......
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final VehicleComponent component = new VehicleComponent();
frame.add(component);
final JLabel runningCount = new JLabel(component.toString());
frame.add(runningCount, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
component.addNewVehicle();
runningCount.setText(component.toString());
}
});
timer.start();
}
});
}
public abstract class Vehicle {
public abstract void draw(Graphics g2);
}
public class Car extends Vehicle {
private int xLeft;
private int yTop;
private Color color;
public Car(Dimension size) {
xLeft = (int) Math.round((Math.random() * size.width - 60));
yTop = (int) Math.round((Math.random() * size.height - 10));
Random rand = new Random();
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
color = new Color(red, green, blue);
}
@Override
public void draw(Graphics g2) {
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(color);
((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);
}
}
public class Truck extends Vehicle {
private int xLeft;
private int yTop;
private Color color;
public Truck(Dimension size) {
xLeft = (int) Math.round((Math.random() * size.width - 60));
yTop = (int) Math.round((Math.random() * size.height - 20));
Random rand = new Random();
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
color = new Color(red, green, blue);
}
@Override
public void draw(Graphics g2) {
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(color);
((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);
}
}
public class VehicleComponent extends JComponent {
private int x;
private int y;
private List<Car> cars;
private List<Truck> trucks;
private Random rnd = new Random();
public VehicleComponent() {
cars = new ArrayList<>(25);
trucks = new ArrayList<>(25);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (Car car : cars) {
car.draw(g2);
}
for (Truck truck : trucks) {
truck.draw(g2);
}
}
public void addNewVehicle() {
int value = (int) Math.round(Math.random() * 5);
if (value == 1) {
if (cars.size() < 10) {
cars.add(new Car(getSize()));
}
} else if (value == 3) {
if (trucks.size() < 10) {
trucks.add(new Truck(getSize()));
}
} else {
System.out.println("No new toys");
}
repaint();
}
@Override
public String toString() {
return "Trucks: " + trucks.size() + ", Cars: " + cars.size();
}
}
}