Witam. Mam problem z przekazaniem informacji getWidth i getHeight z mojej ramki do klasy MyShape. Poniżej umieszczam kod jeżeli ktoś byłby w stanie mi pomóc będę bardzo wdzięczny bo strzelam że jest to pierdoła a siedzę już nad tym sporo:
public class Main {
public static void main(String[] args) {
new Frame();
}
}
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public
class Frame extends JFrame implements Runnable {
public ArrayList<Shape> shapesList = new ArrayList<>();
public Rectangle myRect;
public Oval myOval;
public Line myLine;
public double width = getWidth();
public double height = getHeight();
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);
g.setColor(myRect.color);
g.setColor(myOval.color);
g.setColor(myLine.color);
g.fillOval(((int)(getWidth()*Math.random()*0.8d)),((int)(getHeight()*Math.random()*0.8d)), (int) (myRect.x2),(int) myRect.y2);
g.fillRect(((int)(getWidth()*Math.random()*0.8d)),((int)(getHeight()*Math.random()*0.8d)), (int) (myRect.x2),(int) myRect.y2);
g.drawLine(((int)(getWidth()*Math.random()*0.8d)),((int)(getHeight()*Math.random()*0.8d)), (int) (myRect.x2),(int) myRect.y2);
}
@Override
public void run() {
while (true) {
try {
Rectangle myRect = new Rectangle();
Oval myOval = new Oval();
// here is switch to choose which shape i need
shapesList.add((Shape) myRect);
shapesList.add((Shape) myOval);
shapesList.add((Shape) myLine);
repaint();
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public double getW()
{
return width;
}
public double getH()
{
return height;
}
}
import java.awt.*;
import java.util.Random;
public
class Line extends MyShape {
public Line() {
random = new Random();
x1 = (parent.width * (Math.random() * 0.8d));
y1 = (parent.height * (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 javax.swing.*;
import java.awt.*;
import java.util.Random;
public
class MyShape extends JFrame {
public Frame parent;
public MyShape(Frame parent)
{
this.parent = parent;
}
public double x1;
public double x2;
public double y1;
public double y2;
public Color color;
public Random random;
}
import java.awt.*;
import java.util.Random;
public
class Oval extends MyShape {
public Oval()
{
random = new Random();
x1 = (parent.width * (Math.random() * 0.8d));
y1 = (parent.height * (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 Rectangle()
{
random = new Random();
x1 = (parent.width * (Math.random() * 0.8d));
y1 = (parent.height * (Math.random() * 0.8d));
x2 = Math.random() * 100;
y2 = Math.random() * 100;
color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
}