SlideShare a Scribd company logo
Import java.awt.*;
Import acm.program.*;
Import acm.graphics.*;
public class Sierpinski extends GraphicsProgram
{
public void run()
{
GRect box = new GRect(20, 20, 242, 242);
box.setFilled(true);
add(box);
drawGasket(20, 20, 243);
}
private void drawFigure(int x, int y, int side) {
int sub = side / 3;
GRect box = new GRect(x + sub, y + sub, sub - 1, sub - 1);
box.setFilled(true);
box.setColor(Color.WHITE);
add(box);
if (sub >= 3) {
drawFigure(x, y, sub);
drawFigure(x + sub, y, sub);
drawFigure(x + 2 * sub, y, sub);
drawFigure(x, y + sub, sub);
drawFigure(x + 2 * sub, y + sub, sub);
drawFigure(x, y + 2 * sub, sub);
drawFigure(x + sub, y + 2 * sub, sub);
drawFigure(x + 2 * sub, y + 2 * sub, sub);
}
}
C.8 SQUARE in recursive method:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SierpinskiCarpet extends JPanel implements ActionListener
{
public static void main(String[] args)
{
JFrame window = new JFrame("Sierpinski Carpet");
window.setContentPane( new SierpinskiCarpet() );
window.pack();
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int x = Math.max(10, (screensize.width - window.getWidth()) / 2);
int y = Math.max(10, (screensize.height - window.getHeight()) / 2);
window.setLocation(x,y);
window.setVisible(true);
}
private int level;
private JRadioButton[] levelButton;
private JCheckBox[][] regionCheckbox;
private Display display;
private class Display extends JPanel
{
Display()
{
setBackground(Color.WHITE);
setPreferredSize(new Dimension(729,729));
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
drawCarpet(g,level,0,0,729);
}
}
public SierpinskiCarpet()
{
setLayout(new BorderLayout(3,3));
setBackground(Color.GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY,3));
display = new Display();
add(display,BorderLayout.CENTER);
Box controls = Box.createVerticalBox();
controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
controls.setBackground(Color.WHITE);
controls.setOpaque(true);
add(controls, BorderLayout.EAST);
controls.add(new JLabel("Controls"));
controls.add(Box.createVerticalStrut(30));
levelButton = new JRadioButton[7];
ButtonGroup grp = new ButtonGroup();
for (int i = 0; i < 7; i++)
{
levelButton[i] = new JRadioButton("Recursion Level " + i);
grp.add(levelButton[i]);
levelButton[i].addActionListener(this);controls.add(levelButton[i]);
}
level = 4;
levelButton[4].setSelected(true);
regionCheckbox = new JCheckBox[3][3];
JPanel checks = new JPanel();
checks.setLayout(new GridLayout(3,3,5,5));
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
regionCheckbox[r][c] = new JCheckBox();
checks.add(regionCheckbox[r][c]);
regionCheckbox[r][c].addActionListener(this);
regionCheckbox[r][c].setSelected(true);
}
}
checks.setMaximumSize(new Dimension(90,90));
checks.setAlignmentX(LEFT_ALIGNMENT);
checks.setBackground(Color.WHITE);
regionCheckbox[1][1].setSelected(false);
controls.add(Box.createVerticalStrut(30));
controls.add(new JLabel("Regions To Include:"));
controls.add(Box.createVerticalStrut(15));
controls.add(checks);
}
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
for (int i = 0; i < 7; i++)
{
if (src == levelButton[i])
{
level = i;
break;
}
}
}
private void drawCarpet(Graphics g, int level, int x, int y, int size)
{
g.fillRect(x,y,size,size);
}
}
Solution
Import java.awt.*;
Import acm.program.*;
Import acm.graphics.*;
public class Sierpinski extends GraphicsProgram
{
public void run()
{
GRect box = new GRect(20, 20, 242, 242);
box.setFilled(true);
add(box);
drawGasket(20, 20, 243);
}
private void drawFigure(int x, int y, int side) {
int sub = side / 3;
GRect box = new GRect(x + sub, y + sub, sub - 1, sub - 1);
box.setFilled(true);
box.setColor(Color.WHITE);
add(box);
if (sub >= 3) {
drawFigure(x, y, sub);
drawFigure(x + sub, y, sub);
drawFigure(x + 2 * sub, y, sub);
drawFigure(x, y + sub, sub);
drawFigure(x + 2 * sub, y + sub, sub);
drawFigure(x, y + 2 * sub, sub);
drawFigure(x + sub, y + 2 * sub, sub);
drawFigure(x + 2 * sub, y + 2 * sub, sub);
}
}
C.8 SQUARE in recursive method:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SierpinskiCarpet extends JPanel implements ActionListener
{
public static void main(String[] args)
{
JFrame window = new JFrame("Sierpinski Carpet");
window.setContentPane( new SierpinskiCarpet() );
window.pack();
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int x = Math.max(10, (screensize.width - window.getWidth()) / 2);
int y = Math.max(10, (screensize.height - window.getHeight()) / 2);
window.setLocation(x,y);
window.setVisible(true);
}
private int level;
private JRadioButton[] levelButton;
private JCheckBox[][] regionCheckbox;
private Display display;
private class Display extends JPanel
{
Display()
{
setBackground(Color.WHITE);
setPreferredSize(new Dimension(729,729));
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
drawCarpet(g,level,0,0,729);
}
}
public SierpinskiCarpet()
{
setLayout(new BorderLayout(3,3));
setBackground(Color.GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY,3));
display = new Display();
add(display,BorderLayout.CENTER);
Box controls = Box.createVerticalBox();
controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
controls.setBackground(Color.WHITE);
controls.setOpaque(true);
add(controls, BorderLayout.EAST);
controls.add(new JLabel("Controls"));
controls.add(Box.createVerticalStrut(30));
levelButton = new JRadioButton[7];
ButtonGroup grp = new ButtonGroup();
for (int i = 0; i < 7; i++)
{
levelButton[i] = new JRadioButton("Recursion Level " + i);
grp.add(levelButton[i]);
levelButton[i].addActionListener(this);controls.add(levelButton[i]);
}
level = 4;
levelButton[4].setSelected(true);
regionCheckbox = new JCheckBox[3][3];
JPanel checks = new JPanel();
checks.setLayout(new GridLayout(3,3,5,5));
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
regionCheckbox[r][c] = new JCheckBox();
checks.add(regionCheckbox[r][c]);
regionCheckbox[r][c].addActionListener(this);
regionCheckbox[r][c].setSelected(true);
}
}
checks.setMaximumSize(new Dimension(90,90));
checks.setAlignmentX(LEFT_ALIGNMENT);
checks.setBackground(Color.WHITE);
regionCheckbox[1][1].setSelected(false);
controls.add(Box.createVerticalStrut(30));
controls.add(new JLabel("Regions To Include:"));
controls.add(Box.createVerticalStrut(15));
controls.add(checks);
}
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
for (int i = 0; i < 7; i++)
{
if (src == levelButton[i])
{
level = i;
break;
}
}
}
private void drawCarpet(Graphics g, int level, int x, int y, int size)
{
g.fillRect(x,y,size,size);
}
}

More Related Content

PDF
PDF
Creating an Uber Clone - Part VIII.pdf
PPT
Mobile Game and Application with J2ME - Collision Detection
PPT
Mobile Game and Application with J2ME
PDF
662305 LAB12
DOC
Ocr code
PDF
662305 LAB13
PDF
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf
Creating an Uber Clone - Part VIII.pdf
Mobile Game and Application with J2ME - Collision Detection
Mobile Game and Application with J2ME
662305 LAB12
Ocr code
662305 LAB13
write a prgoram that displays four images or objects in a 2 x 2 grid.pdf

Similar to Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf (20)

PDF
import java.util.Scanner;public class Main {    public static in.pdf
PDF
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
PDF
Write a GUI application to simulate writing out a check. The value o.pdf
PDF
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
PDF
This is Java,I am currently stumped on how to add a scoreboard for.pdf
PDF
In Java Write a GUI application to simulate writing out a check. The.pdf
DOC
PDF
PROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGE
DOCX
PDF
I dont know what is wrong with this roulette program I cant seem.pdf
PDF
package chapter15;import javafx.application.Application;import j.pdf
PDF
Why am I getting an out of memory error and no window of my .pdf
PDF
Creating an Uber Clone - Part XIX.pdf
PDF
PDF
Chat application in java using swing and socket programming.
PDF
Java programs
PDF
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
PDF
import java.awt.;import java.awt.event.;import javax.swing.;.pdf
PDF
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdf
PPT
Graphical User Components Part 2
import java.util.Scanner;public class Main {    public static in.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
Write a GUI application to simulate writing out a check. The value o.pdf
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
This is Java,I am currently stumped on how to add a scoreboard for.pdf
In Java Write a GUI application to simulate writing out a check. The.pdf
PROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGE
I dont know what is wrong with this roulette program I cant seem.pdf
package chapter15;import javafx.application.Application;import j.pdf
Why am I getting an out of memory error and no window of my .pdf
Creating an Uber Clone - Part XIX.pdf
Chat application in java using swing and socket programming.
Java programs
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
import java.awt.;import java.awt.event.;import javax.swing.;.pdf
Tic Tac Toe game with GUI written in java.SolutionAnswerimp.pdf
Graphical User Components Part 2
Ad

More from apexcomputer54 (20)

PDF
Using gordon growth model Value of stock=Dividend paid(1+growt.pdf
PDF
not able to sign in .pdf
PDF
moles of NH3 = 3417 = 2 moles of O2 = 4032 = 1..pdf
PDF
It would be . like for hydrated compound of.pdf
PDF
NO has got a strong bond because it has got an co.pdf
PDF
total number of moles of liquid increase, .pdf
PDF
Yearscost of machin7 Year MACRS rateannual depreciation = cost.pdf
PDF
what this meansSolutionwhat this means.pdf
PDF
we will use tables of normal distribution ( Z distribution)P( z .pdf
PDF
The Northeast blackout of 2003 was a widespread power outage that oc.pdf
PDF
The chemical reaction responsible for formation of water2H2+ O2--.pdf
PDF
SolutionDomain name system is used to determine the IP address of.pdf
PDF
sample mean = population mean = 2.11standard deviation = popul.pdf
PDF
Polymerase chain reaction (PCR) is a process used to replicate DNA i.pdf
PDF
Glutamic acid and Lycine Hydrogen bonding could .pdf
PDF
Need a clear pictureSolutionNeed a clear picture.pdf
PDF
Li2SSolutionLi2S.pdf
PDF
For H2SO3 pK1 = 1.8 and pK2 = 7.2 and thus K1 = .pdf
PDF
find the largest electronegativity difference (mo.pdf
PDF
let an=(-1)^(2n+1)and L=-1let us ahow that L=lim anindeed if e.pdf
Using gordon growth model Value of stock=Dividend paid(1+growt.pdf
not able to sign in .pdf
moles of NH3 = 3417 = 2 moles of O2 = 4032 = 1..pdf
It would be . like for hydrated compound of.pdf
NO has got a strong bond because it has got an co.pdf
total number of moles of liquid increase, .pdf
Yearscost of machin7 Year MACRS rateannual depreciation = cost.pdf
what this meansSolutionwhat this means.pdf
we will use tables of normal distribution ( Z distribution)P( z .pdf
The Northeast blackout of 2003 was a widespread power outage that oc.pdf
The chemical reaction responsible for formation of water2H2+ O2--.pdf
SolutionDomain name system is used to determine the IP address of.pdf
sample mean = population mean = 2.11standard deviation = popul.pdf
Polymerase chain reaction (PCR) is a process used to replicate DNA i.pdf
Glutamic acid and Lycine Hydrogen bonding could .pdf
Need a clear pictureSolutionNeed a clear picture.pdf
Li2SSolutionLi2S.pdf
For H2SO3 pK1 = 1.8 and pK2 = 7.2 and thus K1 = .pdf
find the largest electronegativity difference (mo.pdf
let an=(-1)^(2n+1)and L=-1let us ahow that L=lim anindeed if e.pdf
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Lesson notes of climatology university.
PPTX
Institutional Correction lecture only . . .
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Final Presentation General Medicine 03-08-2024.pptx
Lesson notes of climatology university.
Institutional Correction lecture only . . .
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025
202450812 BayCHI UCSC-SV 20250812 v17.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Supply Chain Operations Speaking Notes -ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Computing-Curriculum for Schools in Ghana
human mycosis Human fungal infections are called human mycosis..pptx
O7-L3 Supply Chain Operations - ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf

  • 1. Import java.awt.*; Import acm.program.*; Import acm.graphics.*; public class Sierpinski extends GraphicsProgram { public void run() { GRect box = new GRect(20, 20, 242, 242); box.setFilled(true); add(box); drawGasket(20, 20, 243); } private void drawFigure(int x, int y, int side) { int sub = side / 3; GRect box = new GRect(x + sub, y + sub, sub - 1, sub - 1); box.setFilled(true); box.setColor(Color.WHITE); add(box); if (sub >= 3) { drawFigure(x, y, sub); drawFigure(x + sub, y, sub); drawFigure(x + 2 * sub, y, sub); drawFigure(x, y + sub, sub); drawFigure(x + 2 * sub, y + sub, sub); drawFigure(x, y + 2 * sub, sub); drawFigure(x + sub, y + 2 * sub, sub); drawFigure(x + 2 * sub, y + 2 * sub, sub); } } C.8 SQUARE in recursive method:- import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SierpinskiCarpet extends JPanel implements ActionListener {
  • 2. public static void main(String[] args) { JFrame window = new JFrame("Sierpinski Carpet"); window.setContentPane( new SierpinskiCarpet() ); window.pack(); window.setResizable(false); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); int x = Math.max(10, (screensize.width - window.getWidth()) / 2); int y = Math.max(10, (screensize.height - window.getHeight()) / 2); window.setLocation(x,y); window.setVisible(true); } private int level; private JRadioButton[] levelButton; private JCheckBox[][] regionCheckbox; private Display display; private class Display extends JPanel { Display() { setBackground(Color.WHITE); setPreferredSize(new Dimension(729,729)); } protected void paintComponent(Graphics g) { super.paintComponent(g); drawCarpet(g,level,0,0,729); } }
  • 3. public SierpinskiCarpet() { setLayout(new BorderLayout(3,3)); setBackground(Color.GRAY); setBorder(BorderFactory.createLineBorder(Color.GRAY,3)); display = new Display(); add(display,BorderLayout.CENTER); Box controls = Box.createVerticalBox(); controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); controls.setBackground(Color.WHITE); controls.setOpaque(true); add(controls, BorderLayout.EAST); controls.add(new JLabel("Controls")); controls.add(Box.createVerticalStrut(30)); levelButton = new JRadioButton[7]; ButtonGroup grp = new ButtonGroup(); for (int i = 0; i < 7; i++) { levelButton[i] = new JRadioButton("Recursion Level " + i); grp.add(levelButton[i]); levelButton[i].addActionListener(this);controls.add(levelButton[i]); } level = 4; levelButton[4].setSelected(true); regionCheckbox = new JCheckBox[3][3]; JPanel checks = new JPanel(); checks.setLayout(new GridLayout(3,3,5,5));
  • 4. for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { regionCheckbox[r][c] = new JCheckBox(); checks.add(regionCheckbox[r][c]); regionCheckbox[r][c].addActionListener(this); regionCheckbox[r][c].setSelected(true); } } checks.setMaximumSize(new Dimension(90,90)); checks.setAlignmentX(LEFT_ALIGNMENT); checks.setBackground(Color.WHITE); regionCheckbox[1][1].setSelected(false); controls.add(Box.createVerticalStrut(30)); controls.add(new JLabel("Regions To Include:")); controls.add(Box.createVerticalStrut(15)); controls.add(checks); } public void actionPerformed(ActionEvent e) { Object src = e.getSource(); for (int i = 0; i < 7; i++) { if (src == levelButton[i]) { level = i; break; } }
  • 5. } private void drawCarpet(Graphics g, int level, int x, int y, int size) { g.fillRect(x,y,size,size); } } Solution Import java.awt.*; Import acm.program.*; Import acm.graphics.*; public class Sierpinski extends GraphicsProgram { public void run() { GRect box = new GRect(20, 20, 242, 242); box.setFilled(true); add(box); drawGasket(20, 20, 243); } private void drawFigure(int x, int y, int side) { int sub = side / 3; GRect box = new GRect(x + sub, y + sub, sub - 1, sub - 1); box.setFilled(true); box.setColor(Color.WHITE); add(box); if (sub >= 3) { drawFigure(x, y, sub); drawFigure(x + sub, y, sub); drawFigure(x + 2 * sub, y, sub); drawFigure(x, y + sub, sub); drawFigure(x + 2 * sub, y + sub, sub); drawFigure(x, y + 2 * sub, sub);
  • 6. drawFigure(x + sub, y + 2 * sub, sub); drawFigure(x + 2 * sub, y + 2 * sub, sub); } } C.8 SQUARE in recursive method:- import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SierpinskiCarpet extends JPanel implements ActionListener { public static void main(String[] args) { JFrame window = new JFrame("Sierpinski Carpet"); window.setContentPane( new SierpinskiCarpet() ); window.pack(); window.setResizable(false); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); int x = Math.max(10, (screensize.width - window.getWidth()) / 2); int y = Math.max(10, (screensize.height - window.getHeight()) / 2); window.setLocation(x,y); window.setVisible(true); } private int level; private JRadioButton[] levelButton; private JCheckBox[][] regionCheckbox; private Display display; private class Display extends JPanel { Display() { setBackground(Color.WHITE); setPreferredSize(new Dimension(729,729)); }
  • 7. protected void paintComponent(Graphics g) { super.paintComponent(g); drawCarpet(g,level,0,0,729); } } public SierpinskiCarpet() { setLayout(new BorderLayout(3,3)); setBackground(Color.GRAY); setBorder(BorderFactory.createLineBorder(Color.GRAY,3)); display = new Display(); add(display,BorderLayout.CENTER); Box controls = Box.createVerticalBox(); controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); controls.setBackground(Color.WHITE); controls.setOpaque(true); add(controls, BorderLayout.EAST); controls.add(new JLabel("Controls")); controls.add(Box.createVerticalStrut(30)); levelButton = new JRadioButton[7]; ButtonGroup grp = new ButtonGroup(); for (int i = 0; i < 7; i++) { levelButton[i] = new JRadioButton("Recursion Level " + i); grp.add(levelButton[i]); levelButton[i].addActionListener(this);controls.add(levelButton[i]); }
  • 8. level = 4; levelButton[4].setSelected(true); regionCheckbox = new JCheckBox[3][3]; JPanel checks = new JPanel(); checks.setLayout(new GridLayout(3,3,5,5)); for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { regionCheckbox[r][c] = new JCheckBox(); checks.add(regionCheckbox[r][c]); regionCheckbox[r][c].addActionListener(this); regionCheckbox[r][c].setSelected(true); } } checks.setMaximumSize(new Dimension(90,90)); checks.setAlignmentX(LEFT_ALIGNMENT); checks.setBackground(Color.WHITE); regionCheckbox[1][1].setSelected(false); controls.add(Box.createVerticalStrut(30)); controls.add(new JLabel("Regions To Include:")); controls.add(Box.createVerticalStrut(15)); controls.add(checks); } public void actionPerformed(ActionEvent e) { Object src = e.getSource();
  • 9. for (int i = 0; i < 7; i++) { if (src == levelButton[i]) { level = i; break; } } } private void drawCarpet(Graphics g, int level, int x, int y, int size) { g.fillRect(x,y,size,size); } }