Clilstore Facebook WA Linkedin Email
Login

This is a Clilstore unit. You can link all words to dictionaries.

An easy program in java to show a fun of rows

Problem: write a program that draws on a java canvas sized (800,500):

Java code for the exercise:

import java.awt.*;

class RigheSpesse  {

  public static void main(String[] args) {
        Frame f = new Frame("A graphic activity : Thick row"); // creation of a frame with a title (frame= form)
        f.setSize(800,500);                // initial dimension  (WIDTH/HEIGHT)
        f.setLocation(50,30);            // origin respect to the display
    //passo in input al canvas un nome
    String nome;
    if (args.length == 0) nome="Pippo&Pluto";
    else nome = args[0];
        f.add(new CanvasRighe(nome));  // add a component, a canvas
       //the input parameter of add
    //is an  anonymous object of type canvas prove
    
    //an alternative coul be:
    //      CanvasRighe c= new CanvasRighe();
    //      f.add(c);
    
    f.setVisible(true);                
 }
}

class CanvasRighe extends Canvas {

//internal variable
  String nome;

//constructor
  public CanvasRighe(String nome) {
     this.nome=nome;
  }

  public void paint(Graphics g) { //overriding of paint of Canvas
// 1 - declare an object g2 of type  Graphics2D and create it assigning
//     g "converted" in Graphics2D (g was Graphics)
    Graphics2D g2 =(Graphics2D) g;
// 2 - draw a row(diagonal) blue
        g2.setColor(Color.BLUE);                  
        g2.drawLine(0,0,getWidth(),getHeight()); // from the top-links to botton-right
// 3 - draw  7 parallel row more and more thicker
        g2.setColor(new Color(54,0,59));               
    for (int i=0; i <=7; i++) {
        g2.setStroke(new BasicStroke(2.0f * i)); // variable thickness
        g2.drawLine(100 + 20*i,20,20 + 20*i,200);
    }
// 4 - draw 7 parallel row more and more thicker, that start from the same point (700,350)

        g2.setColor(new Color(160,80,40));               
    for (int i=0; i <=7; i++) {
        g2.setStroke(new BasicStroke(1.0f * i)); // spessore variabile
        g2.drawLine(40*i + 300,20,700, 350);
    }
// 5 - write the name
    g2.setColor(new Color(60,180,150));
    g2.setFont(new Font("TimesRoman",Font.BOLD + Font.ITALIC,25));
    g2.drawString(nome,20,370);
    g2.setStroke(new BasicStroke(3.0f ));
    int lu=nome.length();
    g2.drawLine(20,400, lu * 16, 400);    
  }
}

 

 

 

result of the execution

 

 

 

 

 

 

 

Clilstore exercisequizcross

Short url:   https://clilstore.eu/cs/1279