import java.awt.*;
import javax.swing.*;

import java.awt.event.*;  

import java.text.AttributedString;
import java.awt.font.TextAttribute;

public class String03 extends JFrame implements ActionListener {

    CCanvas canvas = new CCanvas();

    JCheckBox cb1 = new JCheckBox("rot",true);
    JCheckBox cb2 = new JCheckBox("2. Font",true);
    JCheckBox cb3 = new JCheckBox("blau/gruen",true);
    JCheckBox cb4 = new JCheckBox("2. Font",true);
    JCheckBox cb5 = new JCheckBox("underline",true);
    JCheckBox cb6 = new JCheckBox("swap Colors",true);

  public String03() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setTitle("2. Beispiel mit drawString");
    this.getContentPane().add( canvas, BorderLayout.CENTER );
    setGUI();
  }

  private void setGUI() {
        JPanel panel = new JPanel();
        this.getContentPane().add(panel, BorderLayout.NORTH);
        panel.setLayout( new FlowLayout() );
        panel.add(cb1);
        panel.add(cb2);
        panel.add(cb3);
        panel.add(cb4);
        panel.add(cb5);
        panel.add(cb6);

      cb1.addActionListener(this);
      cb2.addActionListener(this);
      cb3.addActionListener(this);
      cb4.addActionListener(this);
      cb5.addActionListener(this);
      cb6.addActionListener(this);

       cb1.setActionCommand( "cb1" );
       cb2.setActionCommand( "cb2" );
       cb3.setActionCommand( "cb3" );
       cb4.setActionCommand( "cb4" );
       cb5.setActionCommand( "cb5" );
       cb6.setActionCommand( "cb6" );
       Font f1 = new Font("Arial", Font.PLAIN, 24);
       cb1.setFont(f1);
       cb2.setFont(f1);
       cb3.setFont(f1);
       cb4.setFont(f1);
       cb5.setFont(f1);
       cb6.setFont(f1);
   }

  public void actionPerformed(ActionEvent e) {
       String cmd = e.getActionCommand();

       if (cmd.equals("cb1") )   canvas.cb1 = cb1.isSelected();
       if (cmd.equals("cb2") )   canvas.cb2 = cb2.isSelected();
       if (cmd.equals("cb3") )   canvas.cb3 = cb3.isSelected();
       if (cmd.equals("cb4") )   canvas.cb4 = cb4.isSelected();
       if (cmd.equals("cb5") )   canvas.cb5 = cb5.isSelected();
       if (cmd.equals("cb6") )   canvas.cb6 = cb6.isSelected();
      canvas.update( canvas.getGraphics() ); 
   }

    public static void main(String args[]) {
      String03 f = new String03();
      f.setSize(920,200);
      f.setVisible(true);
    }


}

class CCanvas extends JPanel{

    public boolean cb1 = true;
    public boolean cb2 = true;
    public boolean cb3 = true;
    public boolean cb4 = true;
    public boolean cb5 = true;
    public boolean cb6 = true;


  Font myFont=new Font("Arial", Font.ITALIC | Font.PLAIN, 36);

  public void paint (Graphics g)
  {
  
//Upcast --> mehr Funktionen in Graphics2D
    Graphics2D g2d=(Graphics2D)g;
    
    // Antialiasing einschalten
    g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    
    // benoetigte Fonts erzeugen
    Font f1 = new Font("Arial", Font.PLAIN, 36);
    Font f2 = new Font("Times New Roman", Font.PLAIN, 60);

    // String mit Default-Attributen erzeugen
    AttributedString as = new AttributedString("Informatik ist toll, aber auch Mathematik oder Sport");
    
    // gesamter Text in Schrift f1
    as.addAttribute( TextAttribute.FONT, f1 );
    
    // "red" in rot-weißem, zyklischen Farbverlauf
   if (cb1)
    as.addAttribute( TextAttribute.FOREGROUND, new GradientPaint( 
          0,0, 
          Color.red, 
          0,10,
          Color.white ,true ),   15,19);

     
    // "red" in Times New Roman
   if (cb2)
    as.addAttribute( TextAttribute.FONT, f2, 0, 4 );  // zweiten Font setzen
    

    // "blue" in schwarz-blauem Farbverlauf
   if (cb3)
    as.addAttribute( TextAttribute.FOREGROUND, new GradientPaint( 
          0,0, 
          Color.blue, 
          10,0,
          Color.green ,true ), 26,31);  // "auch" setzen
    

    // "auch"  auch in Times New Roman
   if (cb4)
    as.addAttribute( TextAttribute.FONT, f2, 26, 31 );  
    
    // "Mathematik" unterstrichen mit gestrichelter Linie
   if (cb5)
    as.addAttribute( TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DASHED, 31,41 );
    
    // "Violets invertiert"
   if (cb6)
    as.addAttribute( TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON, 22,27 );


 
    // formatierten String ausgeben
    g2d.drawString( as.getIterator(), 30,100 );  


  }

}


