Skip to content

GraphWin

Kaiser Fechner edited this page Mar 23, 2025 · 3 revisions

GraphWin Class

Overview

The GraphWin class is part of the graphics package and provides a graphical window where graphical objects can be drawn and interacted with. It supports various graphical objects like shapes, text, and images and allows mouse and keyboard interactions. The class is primarily used to create graphical windows in interactive applications.

Attributes

  • public static final Color[] STANDARD_COLORS
    A set of standard colors used in graphical objects.

  • private int width
    The width of the window.

  • private int height
    The height of the window.

  • private int preferedX
    Preferred width of the window (calculated during initialization).

  • private int preferedY
    Preferred height of the window (calculated during initialization).

  • public Panel panel
    The panel within the window that handles rendering graphical objects.

  • private ArrayList<GraphicsObject> items
    A list of graphical objects in the window.

  • public boolean autoflush
    Indicates whether the window should automatically flush and repaint after every change.

  • private CountDownLatch latch
    A latch for synchronizing mouse click events.

  • private Point mousePosition
    The current position of the mouse in the window.

  • private double deltaTime
    The time difference between the last update and the current update.

  • private long lastTime
    The timestamp of the last update.

  • private int lastKey
    The last key pressed by the user.

  • public boolean redraw
    Indicates whether the window needs to be redrawn.

  • private final ArrayList<Integer> keysPressed
    A list of keys that are currently pressed.

Constructors

GraphWin(String Name, int w, int h, boolean Autoflush)

Creates a graphical window with the specified width, height, title, and autoflush setting.

Parameters:

  • Name - The title of the window.
  • w - The width of the window.
  • h - The height of the window.
  • Autoflush - Whether the window should automatically flush and repaint.

GraphWin(String Name, int w, int h)

Creates a graphical window with the specified width, height, and title. The autoflush setting is set to false by default.

Parameters:

  • Name - The title of the window.
  • w - The width of the window.
  • h - The height of the window.

Methods

void deleteItem(GraphicsObject object)

Removes a graphical object from the window.

Parameters:

  • object - The graphical object to remove.

void addItem(GraphicsObject object)

Adds a graphical object to the window.

Parameters:

  • object - The graphical object to add.

void update()

Updates the window by calculating delta time and forcing a repaint.

double getDeltaTime()

Returns the time difference (delta time) between the last update and the current update.

Returns:

  • double - The delta time in seconds.

Color getColorAtPoint(int x, int y)

Retrieves the color at the specified point in the window.

Parameters:

  • x - The x-coordinate of the point.
  • y - The y-coordinate of the point.

Returns:

  • Color - The color at the point, or null if no component exists at the point.

boolean getMouse()

This method blocks until the mouse is clicked then returns True.

Returns:

  • boolean - true.

Point getCurrentMousePosition()

Returns the current position of the mouse as a Point.

Returns:

  • Point - Current mouse position relative to the window.

int getKey()

Waits for a key press and returns the key that was pressed. This method blocks until a key is pressed.

Returns:

  • int - The key that was pressed.

Point checkMouse()

Checks and returns the current position of the mouse without waiting for a click.

Returns:

  • Point - The current mouse position.

int getWidth()

Returns the current width of the GraphWin object.

Returns:

  • int - The current width.

int getHeight()

Returns the current height of the GraphWin object.

Returns:

  • int - The current height.

void setBackground(Color clr)

Sets the background color of the window.

Parameters:

  • clr - The color to set the background.

void setPreferredSize(Dimension d)

Sets the preferred size of the panel inside the window.

Parameters:

  • d - The preferred size of the panel.

ArrayList<Integer> checkKeys()

Returns a list of the keys that are currently pressed.

Returns:

  • ArrayList<Integer> - A list of pressed keys.

Inner Classes

private class Panel extends JPanel

An inner class that represents the panel within the window that handles rendering graphical objects. It overrides the paintComponent method to draw the graphical objects onto the panel.

See Also

Table of Contents

Home

Shapes
Advanced Shapes
Components
Animations
Instructions

Clone this wiki locally