1/27
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Constructor
Has same name as class,
is used to initialize variables, JFrame, objects, timers, etc
does NOT have a return type
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
Method header
access level, return type, method name, args/parameters being passed in
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
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);
Setting up timer
*needs to be imported from SWING package
Timer t;
t = new Timer(1000, this);
t.start();
KeyListener: Methods
KeyTyped(KeyEvent e)
KeyPressed(KeyEvent e)
KeyReleased(KeyEvent e)
KeyListener: needed to SETUP keyboard input
setLayout(null);
addKeyListener(this):
setFocusable(true);
requestFocus();
KeyListener: how to detect a key (spacebar)
if (e.getKeySource() == KeyEvent.VK_SPACE)
MouseListener: The 5 methods
MouseClicked(MouseEvent e)
MousePressed(MouseEvent e)
MouseReleased(MouseEvent e)
MouseEntered(MouseEvent e)
MouseExited(MouseEvent e)
MouseMotionListener: The 2 Methods
MouseMoved(MouseEvent e)
MouseDragged(MouseEvent e)
Mouse/Motion Listener: needed to SETUP mouse input
addMouseListener(this); //ONLY IF USED)
addMouseMotionListener(this); //ONLY IF USED)
setFocusable(true);
requestFocus();
To center a JOptionPane
put "null" in the first parent component
Collision
when 2 image MASKS collide
uses "intersects" method & returns boolean
one of them has to be a rectangle mask to be true
Storing images in array
ImageIcon[] Images = new ImageIcon[1];
Images[0] = new ImageIcon("name1.png");
What is an object
An instance of a class
Used to represent entities in a program
Accessor methods
"get" methods
To return SOMETHING usually a class variable, back to us
Mutator methods
"set" methods
To change something about the class variable
how to set RGB color (for blue)
Color blue = new Color(0, 0, 255);
UML: acronym
Unified Modeling Language
UML Symbols:
"-" is private
"+" is public
"#" is protected
Graphics2D: Drawing masks
mask = new Rectangle2D.Double(playerX, playerY, img.getIconWidth(), img.getIconHeight())
Graphics2D: Drawing images
g2.drawImage(ImageIcon.getImage(), xPos, yPos, this);
The 5 JOptionPane Icons
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION _MESSAGE
PLAIN_MESSAGE
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.
How to determine mouse's position
mouseX = e.getX();
mouseY = e.getY();
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);
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));