java assignement

So annoying^^ Yesterday until 3am I did my 1st java assignement for the CSC932 course I am currently taking at Stirling University. Seriously, the application could be improved, but they require the application to be similar to the one they present to us in a compiled format.(JNLP) Was quite a pain as they required the controllers of the program to be scrambled around? Just have a look at the code =D(i know it could be optimised, but i wanted the code to be as close as possible to the way they teach us to code in lectures so as not to be penalised by my style of coding ^^)

code of my app in the full article

So annoying^^ Yesterday until 3am I did my 1st java assignement for the CSC932 course I am currently taking at Stirling University. Seriously, the application could be improved, but they require the application to be similar to the one they present to us in a compiled format.(JNLP) Was quite a pain as they required the controllers of the program to be scrambled around? Just have a look at the code =D(i know it could be optimised, but i wanted the code to be as close as possible to the way they teach us to code in lectures so as not to be penalised by my style of coding ^^)

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; /** * CSC932 // Assignement 1 // Puzzle.java * * INFORMATIONS ABOUT THE CLASS * where cardinal positions are not used for deciding about coordinates, ?v? and ?h? will be used to define vertical and horizontal respectively * all measures not defined in the global variables will be proportional to those defined in the global variables * */ public class Puzzle extends JFrame implements ChangeListener { /** * These are the Global variables */ /** * You may play with these * (other settings might have to be adjusted in the ?main? function) */ //Variables relating to the squares of the Puzzle private int squareSize = 40; // Square size private int blockSize = 10; // Red Block size //Variables relating to the drawing area private int drawPanelSize = 300; //Size of the square drawing panel private int maxDrawingAreaX = drawPanelSize - squareSize; // Maximum drawing area for the squares on X (ie X coordinate of SE corner of allowed drawing region) private int maxDrawingAreaY = drawPanelSize - 50; // Maximum drawing area for the squares on Y (ie Y coordinate of SE corner of allowed drawing region while leaving space for text)) private int maxDrawingAreaTopLeftY = maxDrawingAreaY - squareSize; // Maximum Y value for the Top Left corner of the Square /** * You may NOT play with these as they define objects in the script */ //Random Variable private Random random; //Panel used to draw on private JPanel panel; //Sliders (h: horizontal|v: vertical) private JSlider slider1h;//Box1 private JSlider slider1v; private JSlider slider2h;//Box2 private JSlider slider2v; private JSlider slider3h;//Box3 private JSlider slider3v; private JSlider slider4h;//Box4 private JSlider slider4v; /** * The main method is the main action for the Puzzle program. * * @param args The launch arguments (though none are required). */ public static void main (String[] args) { // Determine the size of the screen and new window position Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int)dim.getWidth()/2 - 500/2; int y = (int)dim.getHeight()/2 - 400/2; //Create Frame Puzzle frame = new Puzzle(); frame.setSize(500,400); // size of window frame.setLocation(x, y); // location of the window frame.setTitle(?Children?s Sliding Squares Puzzle?); // title of the window frame.createGUI(); frame.setVisible(true); } // end of main /** * The createGUI method is called by the main method * to set up the graphical user interface. */ private void createGUI() { // New Random Class launched random = new Random(); // Set up main window characteristics setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = getContentPane(); window.setLayout(new FlowLayout() ); // Create the adjustable slider //Scrambled for 1A** ? should be used as a reference for the slider values in stateChanged, so as to keep the correct x/y limits for the values slider1v = new JSlider(JSlider.VERTICAL, 0, maxDrawingAreaTopLeftY, random.nextInt(maxDrawingAreaTopLeftY));//Box1V slider1h = new JSlider(JSlider.HORIZONTAL, 0, maxDrawingAreaX, random.nextInt(maxDrawingAreaX));//Box2H slider2v = new JSlider(JSlider.VERTICAL, 0, maxDrawingAreaTopLeftY, random.nextInt(maxDrawingAreaTopLeftY));//Box2V slider2h = new JSlider(JSlider.HORIZONTAL, 0, maxDrawingAreaX, random.nextInt(maxDrawingAreaX));//Box4V slider3v = new JSlider(JSlider.VERTICAL, 0, maxDrawingAreaX, random.nextInt(maxDrawingAreaX));//Box3H slider3h = new JSlider(JSlider.HORIZONTAL, 0, maxDrawingAreaTopLeftY, random.nextInt(maxDrawingAreaTopLeftY));//Box1H slider4v = new JSlider(JSlider.VERTICAL, 0, maxDrawingAreaX, random.nextInt(maxDrawingAreaX));//Box4H slider4h = new JSlider(JSlider.HORIZONTAL, 0, maxDrawingAreaTopLeftY, random.nextInt(maxDrawingAreaTopLeftY));//Box3V // Add half of Vertical Sliders to the window window.add(slider1v);//Box1 window.add(slider2v);//Box2 // Create the panel for drawing on panel = new JPanel(); panel.setPreferredSize(new Dimension(drawPanelSize, drawPanelSize)); panel.setBackground(Color.white); window.add(panel); // Add remaining Vertical Sliders to the window window.add(slider3v);//Box3 window.add(slider4v);//Box4 // Add the Horizontal Sliders to the window window.add(slider1h);//Box1 window.add(slider2h);//Box2 window.add(slider3h);//Box3 window.add(slider4h);//Box4 // Add ChangeListener to the slider slider1h.addChangeListener(this); slider1v.addChangeListener(this); slider2h.addChangeListener(this); slider2v.addChangeListener(this); slider3h.addChangeListener(this); slider3v.addChangeListener(this); slider4h.addChangeListener(this); slider4v.addChangeListener(this); } // end of createGUI /** * The stateChanged method is called automatically * when the slider is adjusted. * * @param event This brings information about the slider * adjustment event. This information is * ignored in this application as we know which slider * must have been adjusted! */ public void stateChanged(ChangeEvent e) { Graphics paper = panel.getGraphics(); // First erase the drawing on the paper paper.setColor(Color.white); paper.fillRect(0, 0, drawPanelSize, drawPanelSize); //Scramble slider values and actions int sliderValue1v = slider1v.getValue(); int sliderValue1h = slider2h.getValue(); int sliderValue2v = slider2v.getValue(); int sliderValue2h = slider4v.getValue(); int sliderValue3v = slider3h.getValue(); int sliderValue3h = slider1h.getValue(); int sliderValue4v = slider4h.getValue(); int sliderValue4h = slider3v.getValue(); //Draw Squares drawSquares(paper, Color.cyan, sliderValue1h, sliderValue1v, ?NW?, squareSize, blockSize); drawSquares(paper, Color.black , sliderValue2h, sliderValue2v, ?NE?, squareSize, blockSize); drawSquares(paper, Color.pink , sliderValue3h, sliderValue3v, ?SW?, squareSize, blockSize); drawSquares(paper, Color.magenta , sliderValue4h, sliderValue4v, ?SE?, squareSize, blockSize); //Find if the squares are next to each other if ( (sliderValue1h == sliderValue2h + squareSize) && (sliderValue3h == sliderValue4h + squareSize) && (sliderValue1h == sliderValue3h) && (sliderValue1v == sliderValue3v + squareSize) && (sliderValue2v == sliderValue4v + squareSize) && (sliderValue1v == sliderValue2v)){ drawSmiley(paper, sliderValue4h + (squareSize/4), sliderValue4v + (squareSize/4), squareSize*3/2); } //Draw Instructions paper.setColor(Color.blue); paper.drawString(?Use the sliders to move the four pieces??,20,maxDrawingAreaY+15); paper.drawString(?? but which are which?? Complete the red?,20,maxDrawingAreaY+30); paper.drawString(?square precisely? and get a surprise!? ,20,maxDrawingAreaY+45); } // end of stateChanged private void drawSquares(Graphics paper, Color c, int x, int y, String placementOfBlock, int size, int blockSize){ //Setting the position of the red blocks in the color squares int blockY=0; int blockX=0; if(placementOfBlock == ?center?){//Center of the square blockX = x + size/2 - blockSize/2; blockY = y + size/2 - blockSize/2; }else if(placementOfBlock == ?NW?){//North West Corner blockX = x; blockY = y; }else if(placementOfBlock == ?NN?){//North Centre blockX = x + size/2 - blockSize/2; blockY = y; }else if(placementOfBlock == ?NE?){//North East blockX = x + size - blockSize; blockY = y; }else if(placementOfBlock == ?EE?){//East Centre blockX = x + size - blockSize; blockY = y + size/2 - blockSize/2; }else if(placementOfBlock == ?SE?){//South East blockX = x + size - blockSize; blockY = y + size - blockSize; }else if(placementOfBlock == ?SS?){//South Centre blockX = x + size/2 - blockSize/2; blockY = y + size - blockSize; }else if(placementOfBlock == ?SW?){//South West blockX = x; blockY = y + size - blockSize; }else if(placementOfBlock == ?WW?){//West Centre blockX = x; blockY = y + size/2 - blockSize/2; } // draw Colored square paper.setColor(c); paper.fillRect(x, y, size, size); // draw Red block paper.setColor(Color.red); paper.fillRect(blockX, blockY, blockSize, blockSize); } // end of drawSquares private void drawSmiley(Graphics paper, int x, int y, int size){ //Draws a smiley //Background paper.setColor(Color.yellow); paper.fillOval(x,y,size,size); //Cicle around the background paper.setColor(Color.black); paper.drawOval(x,y,size,size); //Smile paper.drawOval(x+(size*3/10),y+(size*6/10),(size*4/10),(size*15/100)); paper.setColor(Color.yellow); paper.fillRect(x+(size*3/10),y+(size*6/10),(size*4/10),(size*15/100)/2); // Hiding the excess smile //Eyes paper.setColor(Color.black); paper.fillOval(x+(size*2/10),y+(size*3/10),(size*1/10),(size*1/10)); paper.fillOval(x+(size*7/10),y+(size*3/10),(size*1/10),(size*1/10)); } // end of drawSmiley } // end of Puzzle

original post from blog.nagrom.info on 11/03/2006

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>