Ich muss irgendwie das Oval noch in die Klasse EinfacheGui3C einbinden aber ich weis nicht wie
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EinfacheGui3C implements ActionListener {
JFrame frame;
public static void main (String[] args) {
EinfacheGui3C gui = new EinfacheGui3C();
gui.los();
}
public void los() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Farbe wechseln");
button.addActionListener(this);
MeinZeichenPanel zeichenPanel = new MeinZeichenPanel();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, zeichenPanel);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MeinZeichenPanel {
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
int rot = (int)(Math.random() * 255 );
int grün = (int)(Math.random() * 255 );
int blau = (int)(Math.random() * 255 );
Color startColor = new Color(rot,grün,blau);
rot = (int) (Math.random()*255);
grün = (int) (Math.random()*255);
blau = (int) (Math.random()*255);
Color endColor = new Color(rot,grün,blau);
GradientPaint gradient = new GradientPaint(70,70,startColor,150,150,endColor);
g2d.setPaint(gradient);
g2d.fillOval(70,70,100,100);
}
}