Esercizio sugli assi cartesiani svolto in classe

import javax.swing.*;
import java.awt.*;
public class Retta extends JPanel{
  int x1=0;
  int y1=32;
  int x2=10;
  int y2=50;
  int x0=50;
  int y0;
  public void paintComponent(Graphics g){
    x2=this.getWidth();
    y0=getHeight()-50;
    y2=(int)1.8*x2+32;
    g.setColor(Color.RED);
    g.drawLine(x1+x0,y0-y1,x2+x0,y0-y2);
    g.setColor(Color.BLUE);
    g.drawLine(x0,y0,x2-20,y0);
    g.drawLine(x0,y0,x0,50);
  }
}

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestaRetta{
  public static void main(String Args[]){
    JFrame f1=new JFrame("testa retta");
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Retta r=new Retta();
    f1.setSize(500,500);
    f1.add(r);
    f1.setVisible(true);
  }
}

 

image

Commenti