Home The Project Plans Mission
Login Register
Home
Let's draw the loneliness of prime numbers and all the factors of natural numbers analyzed

Let's draw the loneliness of prime numbers and all the factors of natural numbers analyzed

Here is the code on two files to execute the printing of prime numbers graphically

package disegno;
import javax.swing.*;
public class Disegno {
public static void main(String[] args) {
JFrame t= new JFrame("Grafica Iterativa");
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tela p = new Tela(); t.add(p); t.setSize(1800, 1000); t.setVisible(true);

}
}

Here is the print code

package disegno;
import java.awt.*;
import javax.swing.*;
public class Tela extends JPanel{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLACK);
// this following instruction publishes
// a rectangle in the position 5,5 at the top left
int n=1;
String sn="";
for (int r=45;r<1000;r=r+41) { for(int i=0;i<1800;i=i+41) { if (IsPrimeFactor(n)>2||IsPrimeFactor(n)==1)
{g.setColor(Color.BLACK);
}
else
{g.setColor(Color.white);}
 g.fillRect(i,r,40,40); g.setColor(Color.black); if (IsPrimeFactor(n)==2){ sn=Integer.toString(n);} else{ sn=Integer.toString(IsPrimeFactor(n)); g.setColor(Color.white);} if (IsPrimeFactor(n)>2||IsPrimeFactor(n)==1){ g.drawString(sn, i+10, r+30); g.drawString(Integer.toString(n), i+10, r+10);} else{ g.drawString(sn, i+10, r+15);} n++; } }
}
public int IsPrime(int n)
{ int primo=1;
 int fattore=2; boolean exit=false; if (n==1) {exit=true;primo=0;} while(fattore<=n/2 && !exit) { if (n%fattore==0) { primo=0; exit=true; } fattore ++; } return primo;

}

public int IsPrimeFactor(int n){
int x=2,fattori=2;
if(n!=1){
while(x<=n/2)
{if (n%x==0)
{fattori+=1;}
x++;}}
else{fattori=1;}
return fattori;
}

}

here is the result

To run the program above, you need to create the disegno project with the main and add the Tela class

Follow the online Java course and video lessons. Have fun!

🤖

Studio Assistito con l'Intelligenza Artificiale

Vuoi padroneggiare al meglio i concetti di questo articolo? Avvia lo studio assistito per generare riassunti automatici, mappe concettuali o farti interrogare con il metodo socratico.

🎯 Scegli l'Agente AI

Come preferisci studiare questo articolo?

📝
Riassuntore
Genera un riassunto strutturato dell'articolo
🧠
Schematizzatore
Crea mappe concettuali e schemi gerarchici
🎓
Tutor Socratico
Ti interroga sull'articolo con domande progressive

Contact us for a free trial