Hej, stanąłem w martwym punkcie a do oddania projektu dzielą mnie 3 godziny. Macie pomysł jak skończyć to co zapoczątkowałem ? Czyli jak rysować 3 losowe kształty w losowych kolorach i położeniach i dodawać je do listy (którą później trzeba odtworzyć w drugim oknie) ?? Jeżeli ktoś ma pomysł byłbym bardzo wdzięczny bo ten projekt spędza mi sen z powiek... Dodaje to co mam:
public class Main {
public static void main(String[] args) {
new Frame();
}
}
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public
class Frame extends JFrame implements Runnable {
public ArrayList<Shape> shapesList = new ArrayList<>(30);
public double width = getWidth();
public double height = getHeight();
public Rectangle myRect;
public Oval myOval;
public Line myLine;
public Frame()
{
super("I do not need title, just 50%");
setSize(800,600);
setDefaultCloseOperation(3);
setLocationRelativeTo(null);
setVisible(true);
Thread t0 = new Thread(this);
t0.start();
}
/*@Override
public void paint(Graphics g) {
super.paint(g);
for (Shape shape : shapesList) {
// Graphics g = (Graphics) g.create();
g.fillRect(((int)(getWidth()*Math.random()*0.8d)),((int)(getHeight()*Math.random()*0.8d)), (int) (myRect.x2),(int) myRect.y2);
g.dispose();
}
}
*/
@Override
public void run() {
while (true) {
try {
Random rand = new Random();
switch (rand.nextInt(3)+1) {
case 1:
shapesList.add((Shape)myRect);
break;
case 2:
shapesList.add((Shape)myOval);
break;
case 3:
shapesList.add((Shape)myLine);
break;
}
repaint();
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public abstract
class MyShape extends JFrame {
public double x1;
public double x2;
public double y1;
public double y2;
public Color color;
public Random random;
public Rectangle myRect;
public Oval myOval;
public Line myLine;
@Override
public void paint(Graphics g) {
}
// tutaj metoda rysuj
}
import java.awt.*;
import java.util.Random;
public
class Line extends MyShape {
public void paint(Graphics g)
{
g.setColor(myLine.color);
g.drawLine(((int)(getWidth()*Math.random()*0.8d)),((int)(getHeight()*Math.random()*0.8d)), (int) (myRect.x2),(int) myRect.y2);
}
public Line()
{
random = new Random();
x1 = (getWidth() * (Math.random() * 0.8d));
y1 = (getHeight()* (Math.random() * 0.8d));
x2 = Math.random() * 100;
y2 = Math.random() * 100;
color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
}
import java.awt.*;
import java.util.Random;
public
class Oval extends MyShape {
public void paint(Graphics g)
{
g.setColor(myOval.color);
g.fillOval(((int)(getWidth()*Math.random()*0.8d)),((int)(getHeight()*Math.random()*0.8d)), (int) (myRect.x2),(int) myRect.y2);
}
public Oval()
{
random = new Random();
x1 = (getWidth() * (Math.random() * 0.8d));
y1 = (getHeight()* (Math.random() * 0.8d));
x2 = Math.random() * 100;
y2 = Math.random() * 100;
color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
}
import java.awt.*;
import java.util.Random;
public
class Rectangle extends MyShape {
public void paint(Graphics g)
{
g.setColor(myRect.color);
g.fillRect(((int)(getWidth()*Math.random()*0.8d)),((int)(getHeight()*Math.random()*0.8d)), (int) (myRect.x2),(int) myRect.y2);
}
public Rectangle()
{
random = new Random();
x1 = (getWidth() * (Math.random() * 0.8d));
y1 = (getHeight()* (Math.random() * 0.8d));
x2 = Math.random() * 100;
y2 = Math.random() * 100;
color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
}