0% found this document useful (0 votes)
15 views

Import Import Import Class Public Static Void New Double New New

This document contains code for a Java program that draws triominoes on a grid. It includes classes for a drawing panel, background grid, and individual triominoes. The background class initializes a boolean array to track filled squares and draws the grid with white, red, or black squares. Each triomino object stores its position and draws itself on the grid. The main method creates the components, frame, and displays the drawing panel with triominoes on the initialized background grid.

Uploaded by

Michael Wright
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Import Import Import Class Public Static Void New Double New New

This document contains code for a Java program that draws triominoes on a grid. It includes classes for a drawing panel, background grid, and individual triominoes. The background class initializes a boolean array to track filled squares and draws the grid with white, red, or black squares. Each triomino object stores its position and draws itself on the grid. The main method creates the components, frame, and displays the drawing panel with triominoes on the initialized background grid.

Uploaded by

Michael Wright
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

*; class DrawingPanelTest2{ public static void main(String[] args) { Scanner input = new Scanner(System.in); double pw = input.nextDouble(); myPan panel = new myPan(pw); JFrame application = new JFrame(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(panel); application.setSize(400, 400); application.setVisible(true); } } class myPan extends JPanel{ public double pow; public tri play[]; public background back; public myPan(double p){ pow = p; back = new background(p); play = new tri[10]; //this is where you create all of your triominoes //you can do it a different way, but this is an //example } public void paintComponent(Graphics g){ super.paintComponent(g); back.paintBackground(g); for(int i = 0; i < play.length; i++){ //paint all of the triominos play[i].paintTri(g); } } } class background{ public double pow; public boolean filled[][]; public background(double p){ pow = p; filled = new boolean[up][across]; //this is if it is filled with a tri //you'll need to figure out a way to set //the filled ones to "true" } public paintBackground(Graphics g){ public void paintComponent(Graphics g){

super.paintComponent(g); double num = Math.pow(2,pow); double across; double up; if(pow % 2 == 0){ //is a square across = Math.pow(num,0.5); up = across; } else{ double x = Math.floor(pow/2); double y = x + 1; across = Math.pow(2,x); up = Math.pow(2,y); } System.out.println(across); System.out.println(up); // // double wid = 400/across; //width of one double hi = 400/up; //height of one double nowX = 0; double nowY = 0; for(int i = 0; i < up; i++){ //top to bottom nowX = 0; for(int j = 0; j < across; j++){ g.setColor(Color.BLACK); g.drawRect((int)nowX, (int)nowY, (int)wid, (int)hi); if(...the square is OK...){ //<--you'll need a way to check this using the "filled" array g.setColor(Color.WHITE); g.fillRect((int)nowX, (int)nowY, (int)wid, (int)hi); } else{//the square is not ok g.setColor(Color.RED); g.fillRect((int)nowX, (int)nowY, (int)wid, (int)hi); } nowX = nowX + wid; } nowY = nowY + hi; } } } } class tri{ public tri(int x, int y, int width, int height){ //initialize all of your values } public paintTri(Graphics g){

super.paintComponent(g); //draw it here } }

You might also like