appendix h: graphics program -
Class GraphPlotter

/*

Punit Shah and Jack Ingalls

2007 New Mexico Supercomputing Challenge

School: Albuquerque Academy

Team: 7

Project Name: Deriving Ramsey Numbers

This class is part of the graphing program

*/

 

 

 

 

//------------------------------------------------------------------------------------

// This class graphs to visually prove that the Ramsey Number is larger that the guess

// Uses the Edge Sequence Key in order to know how to color the map

//------------------------------------------------------------------------------------

 

 

import apcslib.*;

import java.awt.*;

import javax.swing.*;

 

 

class GraphPlotter

 

{

public GraphPlotter() {

try {

jbInit();

}

catch (Exception ex) {

ex.printStackTrace();

}

}

 

public static void main(String[] args)

 

{

// Define main variables to be used through the program**************************************

 

// tools for drawing

DrawingTool pencil;

SketchPad paper;

Color red;

Color blue;

Color black;

 

// essential information about the map we will graph

int ptsInMap;

int submapSize1;

int submapSize2;

int numEdges;

//int binaryColoringSeq[];

 

paper = new SketchPad(700,800);

pencil = new DrawingTool(paper);

red = new Color(255, 0, 0);

blue = new Color(0, 0, 255);

black = new Color(0, 0, 0);

 

paper.setTitle("Deriving Ramsey Numbers ");

setDrawSpeed(paper);

 

// Get the Edge Sequence Key

// NOTE: the key follows this format (no parenthesis in key):

// (ptsInMap)X(submapSize1)X(submapSize2)X(binaryColoringSequence)

String ESKey;

ESKey = JOptionPane.showInputDialog(null, "Please enter the Edge Sequence Key.",

"Graph Specific Coloring Sequence", JOptionPane.QUESTION_MESSAGE);

 

 

// Get the values of the essential values about the graph*******************************

int xLoc1;

int xLoc2;

String binaryColoringSequence; // actually used for this purpose after we have all the values

 

 

// get ptsInMap

xLoc2 = getNextXLoc(ESKey, 1);

binaryColoringSequence = ESKey.substring(0, xLoc2);

ptsInMap = Integer.parseInt(binaryColoringSequence);

 

// get submapSize1

xLoc1 = xLoc2 + 1;

xLoc2 = getNextXLoc (ESKey, xLoc1);

binaryColoringSequence = ESKey.substring(xLoc1, xLoc2);

submapSize1 = Integer.parseInt(binaryColoringSequence);

 

// get submapSize2

xLoc1 = xLoc2 + 1;

xLoc2 = getNextXLoc (ESKey, xLoc1);

binaryColoringSequence = ESKey.substring(xLoc1, xLoc2);

submapSize2 = Integer.parseInt(binaryColoringSequence);

 

// get binary coloring sequence

xLoc1 = xLoc2 + 1;

binaryColoringSequence = ESKey.substring(xLoc1);

numEdges = binaryColoringSequence.length();

// Print out some information about the key to the output console I/O window

// mostly printed for debugging purposes

System.out.println(ESKey);

System.out.println(binaryColoringSequence);

System.out.println(ptsInMap);

System.out.println(submapSize1 + " " + submapSize2);

System.out.println(numEdges);

 

// Calculate the locations of the nodes********************************

// first node is always at (1,0)

// location of subsequence nodes calculated by first (partially) getting their polar coordinates

// and then converting to Cartesian coordinates (note: radius = 150)

double coordinates[][];

coordinates = new double[ptsInMap][2];

double theta;

 

double radialDistance = (2 * Math.PI)/ (double)ptsInMap;

 

// polorCoordinates[node#][0] = x and polorCoordinates[node#][1] = y

for (int i = 0; i < ptsInMap; i++)

{

theta = (double) i * radialDistance;

 

// covert our polar information into Cartesian coordinates

coordinates[i][0] = 200.0 * Math.cos(theta);

coordinates[i][1] = 200.0 * Math.sin(theta);

 

}

 

 

// Now, actually start graphing**************************************

 

// Plot all the points

for (int i = 0; i < ptsInMap; i++)

{

pencil.up();

pencil.move(coordinates[i][0], coordinates[i][1]);

pencil.down();

pencil.drawOval(10,10);

}

 

//draw the lines

// true: blue

// false: red

 

 

int index = 0; // this is the index within the string

//int crntPt = 0; // this is the current node within the graph

int crntChar;

 

 

// in the loop, i is the current node within the graph

for (int i = 1; i < ptsInMap; i++)

{

for(int k = 0; k < i; k++)

{

crntChar = binaryColoringSequence.charAt(index);

if (crntChar == '0')

{

pencil.setColor(red);

drawLine(coordinates[i][0], coordinates[i][1], coordinates[k][0],coordinates[k][1], pencil);

}

else // current char is '1'

{

pencil.setColor(blue);

drawLine(coordinates[i][0], coordinates[i][1], coordinates[k][0],coordinates[k][1], pencil);

}

 

index++;

}

 

}

 

 

// Print vital information about the sequence onto the screen

pencil.up();

pencil.move(-325, 350);

pencil.down();

 

pencil.setColor(black);

drawString("Program to graph a map with specific coloring", pencil);

drawString("Supercomputing Challenge 2006-07, Team 7, Albuquerque Academy", pencil);

drawString("", pencil);

drawString("The previous program searched for the Ramsey number R(" +submapSize1 + ", "+ submapSize2 + ").", pencil);

drawString("The guess was " + ptsInMap + " nodes.", pencil);

drawString("Here is a graphical representation of why " +ptsInMap + " < the real value.", pencil);

drawString("", pencil);

drawString("The subgraph of size " + submapSize1 + " is blue, while the subgraph of size " + submapSize2 + " is red.", pencil);

 

 

}

//-------------------------------------------------

// Purpose: sets the drawing speed to instantaneous

//-------------------------------------------------

private static void setDrawSpeed(SketchPad paper)

{

// Gets to the speed button and changes the draw speed to "no delay"

JMenuBar menu = paper.getJMenuBar();

JMenu speed = menu.getMenu(1);

JMenuItem noDelay = speed.getItem(3);

noDelay.doClick();

}

//------------------------------------------------------------------------------------------------------------

// Purpose: finds the index in the string where the next X is separating the values of the different variables

//------------------------------------------------------------------------------------------------------------

private static int getNextXLoc (String ESKey, int startingXLoc)

{

while (ESKey.charAt(startingXLoc) != 'X')

{

startingXLoc++;

}

 

return startingXLoc;

}

//------------------------------------------------------------------

// Purpose: draws a line between the two points passed to the method

//------------------------------------------------------------------

private static void drawLine(double x1, double y1, double x2, double y2, DrawingTool pencil)

{

pencil.up();

pencil.move(x1, y1);

pencil.down();

pencil.move(x2,y2);

 

}

//-----------------------------------------------------------------------------------------------------

// Purpose: draws a string and then automatically moves the DrawingTool down 15 pixels after every line

//-----------------------------------------------------------------------------------------------------

private static void drawString (String theString, DrawingTool pencil)

{

pencil.up();

pencil.setDirection(-90);

pencil.forward(15);

pencil.down();

pencil.drawString(theString);

 

}

 

 

//---------------------

// Method part of class

//---------------------

private void jbInit() throws Exception

{

}

 

}




contact us & copyright | site design © 2007 Punit Shah | project © 2007 Punit Shah and Jack Ingalls albuquerque academy | new mexico supercomputing challenge
Google logo trademark of Google Inc. Chalkboard photo used under Creative Commons licensing. valid xhtml | valid css