Skip to content

Commit

Permalink
Fixed Flickering, Adapted from Frame to Canvas, Removed Constants class
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliu2016 committed Jun 16, 2017
1 parent beb4980 commit eecf524
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 164 deletions.
4 changes: 2 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ Arrows : Pan the view
Delete : Removes selected entities
P : Create a point

ICS3U, Introduction to computer sciece
Last updated June 12, 2017
ICS3U, Introduction to computer science
Last updated June 15, 2017
52 changes: 52 additions & 0 deletions src/ca/wtcs/ics3u/gc/BufferedCanvas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ca.wtcs.ics3u.gc;


/*
* Created by Yu Liu on 2017-06-15
* ICS3U - Culminating Project - A compass-and-straightedge construction tool
*
* More information at https://github.com/yuliu2016/GeometryConstructor
*/


import java.awt.*;


/**
* A <code>BufferedCanvas</code> is a Canvas that
* updates by painting onto a image
* and then copies it onto the component.
* This allows the program to be flicker-free
* when it's drawing multiple objects at each frame
*/


class BufferedCanvas extends Canvas {


/**
* Overrides Canvas' update method.
* Creates a buffer image from the current component,
* then calls the component's paint method with the graphic
* context created from the image. Finally it calls drawImage
* on the component's graphic context to "blit" the image onto
* the screen
*
* @param g the onscreen component's graphic context
*/

@Override
public void update(Graphics g) {

Image bufferedImage = createImage(getWidth(), getHeight());
Graphics bufferedGraphics = bufferedImage.getGraphics();

bufferedGraphics.setColor(getBackground());
bufferedGraphics.fillRect(0, 0, getWidth(), getHeight());
bufferedGraphics.setColor(getForeground());

paint(bufferedGraphics);
g.drawImage(bufferedImage, 0, 0, this);

}
}
43 changes: 0 additions & 43 deletions src/ca/wtcs/ics3u/gc/Const.java

This file was deleted.

49 changes: 26 additions & 23 deletions src/ca/wtcs/ics3u/gc/Geometry.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ca.wtcs.ics3u.gc;


import java.awt.*;


/*
* Created by Yu Liu on 2017-06-06
* ICS3U - Culminating Project - A compass-and-straightedge construction tool
Expand All @@ -12,6 +9,8 @@
*/


import java.awt.*;

/**
* <code>Geometry</code> is a top-level interface for
* all geometric entities used in this application. An
Expand All @@ -29,17 +28,19 @@ public interface Geometry {

/**
* Paints the geometry entity
* @param g the graphic context to be drawn on
*
* @param g the graphic context to be drawn on
* @param viewPort the viewport context to determine position
*/

void paint (Graphics g, ViewPort viewPort);
void paint(Graphics g, ViewPort viewPort);


/**
* Determines if a mouse position is hovering over the geometry
* @param mouseX the X position of the mouse
* @param mouseY the Y position of the mouse
*
* @param mouseX the X position of the mouse
* @param mouseY the Y position of the mouse
* @param viewPort the viewport context to determine position
* @return if the mouse is over geometry
*/
Expand All @@ -49,41 +50,43 @@ public interface Geometry {

/**
* Determines if the geometry is selected
*
* @return if the geometry is selected
*/

boolean isSelected();

boolean getSelected();

/**
* Determines if the geometry is hidden
* @return if the geometry is hidden
* Sets the selected state of geometry
*
* @param selected the selected state of geometry
*/

boolean isHidden();

void setSelected(boolean selected);

/**
* Sets the hovered state of geometry
* @param hovered the hovered state of geometry
* Determines if the geometry is hidden
*
* @return if the geometry is hidden
*/

void setHovered(boolean hovered);

boolean getHidden();

/**
* Sets the selected state of geometry
* @param selected the selected state of geometry
* Sets the hidden state of geometry
*
* @param hidden the hidden state of geometry
*/

void setSelected(boolean selected);
void setHidden(boolean hidden);


/**
* Sets the hidden state of geometry
* @param hidden the hidden state of geometry
* Sets the hovered state of geometry
*
* @param hovered the hovered state of geometry
*/

void setHidden(boolean hidden);
void setHovered(boolean hovered);

}
2 changes: 2 additions & 0 deletions src/ca/wtcs/ics3u/gc/Geometry1D.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* @see Geometry
*/


interface Geometry1D
extends Geometry {


}
62 changes: 60 additions & 2 deletions src/ca/wtcs/ics3u/gc/GeometryConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
*/


import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* Contains the wrapper main method to initiate a GraphView
* A <code>GeometryConstructor</code> initiates a window with a
* {@link GraphView} filled into it
*
* @author Yu Liu
* @see GraphView
Expand All @@ -19,10 +26,61 @@

public class GeometryConstructor {


/**
* Default dimension of the application
*/

private static final Dimension DEFAULT_DIMENSION;

/**
* The title of the application shown on the window
*/

private static final String TITLE;


static {
DEFAULT_DIMENSION = new Dimension(600, 450);
TITLE = "Geometry Constructor";
}


/**
* Starts the application by intiating a Frame containing a GraphView
*
* @param args System arguments
*/

public static void main(String[] args) {

new GraphView();
Frame application = new Frame(TITLE);
GraphView graphView = new GraphView();

application.setSize(DEFAULT_DIMENSION);
application.setLayout(null);
application.setResizable(true);

application.add(graphView);

application.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
application.dispose();
System.exit(0);
}
});

application.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Dimension newSize = application.getSize();
graphView.setSize(newSize.width, newSize.height);
}
});

application.setMinimumSize(GraphView.MINIMUM_DIMENSION);
application.setVisible(true);
}

}
Loading

0 comments on commit eecf524

Please sign in to comment.