Computer Science 11 - Unit 4 (GUI)

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/27

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

28 Terms

1
New cards

Constructor

Has same name as class,

is used to initialize variables, JFrame, objects, timers, etc

does NOT have a return type

2
New cards

Method

A method in Java is used to perform a specific task or action within a program.

Helps organize and debug code

it HAS a return type

3
New cards

Method header

access level, return type, method name, args/parameters being passed in

4
New cards

Overloading constructors

When you have the same name within same class but the args are different

What's being passed in is different

it allows a class to provide multiple ways of initializing objects, accommodating different scenarios and enhancing code flexibility

5
New cards

How to make a font object

Font f = new Font("Arial", Font.PLAIN, 18);

g2.setFont(f);

g2.setColor(Color.BLUE);

g2.drawString("Hello", 0, 0);

6
New cards

Setting up timer

*needs to be imported from SWING package

Timer t;

t = new Timer(1000, this);

t.start();

7
New cards

KeyListener: Methods

KeyTyped(KeyEvent e)

KeyPressed(KeyEvent e)

KeyReleased(KeyEvent e)

8
New cards

KeyListener: needed to SETUP keyboard input

setLayout(null);

addKeyListener(this):

setFocusable(true);

requestFocus();

9
New cards

KeyListener: how to detect a key (spacebar)

if (e.getKeySource() == KeyEvent.VK_SPACE)

10
New cards

MouseListener: The 5 methods

MouseClicked(MouseEvent e)

MousePressed(MouseEvent e)

MouseReleased(MouseEvent e)

MouseEntered(MouseEvent e)

MouseExited(MouseEvent e)

11
New cards

MouseMotionListener: The 2 Methods

MouseMoved(MouseEvent e)

MouseDragged(MouseEvent e)

12
New cards

Mouse/Motion Listener: needed to SETUP mouse input

addMouseListener(this); //ONLY IF USED)

addMouseMotionListener(this); //ONLY IF USED)

setFocusable(true);

requestFocus();

13
New cards

To center a JOptionPane

put "null" in the first parent component

14
New cards

Collision

when 2 image MASKS collide

uses "intersects" method & returns boolean

one of them has to be a rectangle mask to be true

15
New cards

Storing images in array

ImageIcon[] Images = new ImageIcon[1];

Images[0] = new ImageIcon("name1.png");

16
New cards

What is an object

An instance of a class

Used to represent entities in a program

17
New cards

Accessor methods

"get" methods

To return SOMETHING usually a class variable, back to us

18
New cards

Mutator methods

"set" methods

To change something about the class variable

19
New cards

how to set RGB color (for blue)

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

20
New cards

UML: acronym

Unified Modeling Language

21
New cards

UML Symbols:

"-" is private

"+" is public

"#" is protected

22
New cards

Graphics2D: Drawing masks

mask = new Rectangle2D.Double(playerX, playerY, img.getIconWidth(), img.getIconHeight())

23
New cards

Graphics2D: Drawing images

g2.drawImage(ImageIcon.getImage(), xPos, yPos, this);

24
New cards

The 5 JOptionPane Icons

ERROR_MESSAGE

INFORMATION_MESSAGE

WARNING_MESSAGE

QUESTION _MESSAGE

PLAIN_MESSAGE

25
New cards

Dragging an image

use "MouseDragged(MouseEvent e)" method

check if user clicks inside image then update the image's x and y position to the mouse's position as its being dragged.

26
New cards

How to determine mouse's position

mouseX = e.getX();

mouseY = e.getY();

27
New cards

customized images to JOptionPane (message, input, option):

JOptionPane.showMessageDialog(null, "Text", " Title", Message Type, ImageIcon);

JOptionPane.showInputDialog (null, "age:", "Dialog", JOptionPane. PLAIN_MESSAGE, ImageIcon, null, null));

JOptionPane.showOptionDialog (null, "Question", " Title", Message Type, 0, ImageIcon, null, null);

28
New cards

Get input from JOptionPane (string & number)

str = JOptionPane.showInputDialog(null, "Text", "Title", JOptionPane.PLAIN_MESSAGE));

age = Integer.parseInt((String) JOptionPane.showInputDialog (null, "age:", "Dialog", JOptionPane. PLAIN_MESSAGE));