Ciao ragazzi, devo disegnare un rettangolo che cambi colore e posizione quando il frame viene ridimensionato.
Solo che non so come mai il rettangolo non viene disegnato(oppure viene disegnato dello stesso colore dello sfondo). Come mai? Ecco il codice.
codice:
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.*;
public class MioPanel extends JPanel {
private int x=100;
private int y=300;
private Random rr=new Random();
public void paintComponent(Graphics2D g1){
super.paintComponent(g1);
Color colore=new Color(rr.nextInt(255),rr.nextInt(255),rr.nextInt(255));
g1.setColor(colore);
Rectangle2D rettangolo=new Rectangle2D.Double(rr.nextInt(255),rr.nextInt(255),x,y);
g1.fill(rettangolo);
}
}
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class MioFrame extends JFrame{
Random rr=new Random();
public MioFrame() {
super("RETTANGOLO COLORATO");
setSize(400,400);
setLocation(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MioPanel pannello=new MioPanel();
Container contenitore=getContentPane();
Color colore1=new Color(rr.nextInt(255),rr.nextInt(255),rr.nextInt(255));
pannello.setBackground(colore1);
contenitore.add(pannello);
}
}
public class Test {
public static void main(String[] args) {
MioFrame finestra=new MioFrame();
}
}