PIbd-23-Radaev-A.V.-Catamar.../HardForm.java

65 lines
2.3 KiB
Java
Raw Permalink Normal View History

2023-12-11 14:47:27 +04:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class HardForm {
private Canvas canv;
private int pictureBoxWidth;
private int pictureBoxHeight;
private EntityCatamaran makeEntity() {
Random rand = new Random();
return new EntityCatamaran(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)),
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)), rand.nextBoolean(),
rand.nextBoolean());
}
void Draw() {
if (canv == null)
return;
canv.repaint();
}
private IDraw makeIDraw(EntityCatamaran entity) {
return new DrawningOars(0, 0, entity.OarColor());
}
public HardForm() {
Random rand = new Random();
int sz = rand.nextInt(1, 10);
JFrame HardFrame = new JFrame();
HardFrame.setSize(700, 400);
HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
canv = new Canvas();
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
JButton makeObject = new JButton("Создать");
makeObject.setBounds(0, 0, 100, 40);
canv.add(makeObject);
HardFrame.setContentPane(canv);
HardFrame.setVisible(true);
pictureBoxHeight = canv.getHeight();
pictureBoxWidth = canv.getWidth();
HardGeneric<EntityCatamaran, IDraw> toDraw = new HardGeneric<>(sz,
sz, pictureBoxWidth, pictureBoxHeight);
for (int i = 0; i < sz; i++) {
EntityCatamaran ent = makeEntity();
toDraw.InsertFirst(ent);
toDraw.InsertSecond(makeIDraw(ent));
}
makeObject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningCatamaran DrawningCatamaran = toDraw.MakeObject();
DrawningCatamaran.SetPosition(pictureBoxWidth / 2 - DrawningCatamaran.GetWidth() / 2,
pictureBoxHeight / 2 - DrawningCatamaran.GetHeight() / 2);
canv.DrawningCatamaran = DrawningCatamaran;
Draw();
}
});
}
}