Esercizio Grafica Java

Classe Disegno1

import javax.swing.*;
import java.awt.*;
public class Disegno1 extends JPanel{
    Color colore1=Color.YELLOW;
    Color colore2=Color.GREEN;
    public void setColore1(Color c1){
        colore1=c1;
        }
    public void setColore2(Color c2){
        colore2=c2;
    }
   public void paintComponent(Graphics g){
        g.setColor(colore1);
        g.fillRect(100, 50, 200, 100);
        g.setColor(colore2);
        g.fillOval(100, 200, 100, 100);
    }
}

---------------------------------------------------------------------

Classe TestaDisegno

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestaDisegno {
    JFrame f1=new JFrame("Disegno1");
    Disegno1 d1=new Disegno1();
    JButton b1=new JButton("Colore Rosso e Rosa");
    JButton b2=new JButton("Colore Verde e Arancio");
    public TestaDisegno(){
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.add(BorderLayout.CENTER,d1);
        f1.add(BorderLayout.NORTH,b1);
        f1.add(BorderLayout.SOUTH,b2);
        f1.setSize(400,600);  //adatta la finestra al contenuto
        f1.setVisible(true);
        b1.addActionListener(new Bottone1());
        b2.addActionListener(new Bottone2());
    }
    class Bottone1 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            d1.setColore1(Color.RED);
            d1.setColore2(Color.PINK);
            f1.repaint();
        }
    }
    class Bottone2 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            d1.setColore1(Color.GREEN);
            d1.setColore2(Color.ORANGE);
            f1.repaint();
        }
    }
    public static void main(String args []){
        TestaDisegno d=new TestaDisegno();
    }
}

 

Cattura

Commenti