Last active
March 23, 2021 00:36
-
-
Save YashTotale/ad47f2c1573033844a4998ca0ceb77d8 to your computer and use it in GitHub Desktop.
MarioChar: AP Computer Science Assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.util.*; | |
public class MarioChar { | |
String name; | |
//TODO: Add Superpower class | |
ArrayList<String> superpowers; | |
int x; | |
int y; | |
/** | |
* Changing circle radius proportionally changes the other shapes/lines | |
*/ | |
public static final int CIRCLE_RADIUS = 75; | |
/** | |
* For testing | |
*/ | |
public static void main(String[] args) throws Exception { | |
ArrayList superpowers = new ArrayList<String>(); | |
superpowers.add("test superpower"); | |
MarioChar m = new MarioChar("Testing", superpowers, 150, 150); | |
// Testing toString | |
if (!m.toString().equals("<MarioChar> Testing (x: 150, y: 150)")) { | |
throw new Exception("toString does not work"); | |
} | |
// Testing setters + getters | |
String newName = "New name"; | |
m.setName(newName); | |
if (!m.getName().equals(newName)) { | |
throw new Exception("get/set name does not work"); | |
} | |
String superpowerToAdd = "invincible"; | |
m.addSuperpower(superpowerToAdd); | |
if(!m.getSuperpower(1).equals(superpowerToAdd)){ | |
throw new Exception("add/get superpower does not work"); | |
} | |
m.removeSuperpower(1); | |
if(m.getSuperpowers().size() != 1) { | |
throw new Exception("remove superpower or getSuperpowers does not work"); | |
} | |
int newX = 200; | |
m.setX(newX); | |
if (m.getX() != newX) { | |
throw new Exception("get/set x does not work"); | |
} | |
int newY = 200; | |
m.setY(newY); | |
if (m.getY() != newY) { | |
throw new Exception("get/set y does not work"); | |
} | |
// Testing move | |
int xMove = 20; | |
int yMove = -20; | |
m.move(xMove, yMove); | |
if (m.getX() != newX + xMove) { | |
throw new Exception("move with x does not work"); | |
} | |
if (m.getY() != newY + yMove) { | |
throw new Exception("move with y does not work"); | |
} | |
// Testing draw | |
JPanel panel = new JPanel() { | |
public void paintComponent(Graphics g) { | |
super.paintComponent(g);// basically clears the canvas | |
m.draw(g); | |
} | |
}; | |
panel.setBackground(Color.GREEN); | |
panel.setPreferredSize(new Dimension(800, 600)); | |
JFrame frame = new JFrame("Testing MarioChar"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.add(panel); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public MarioChar(String name, ArrayList<String> superpowers, int x, int y) { | |
this.name = name; | |
this.superpowers = superpowers; | |
this.x = x; | |
this.y = y; | |
} | |
public MarioChar(String name, int x, int y) { | |
this(name, new ArrayList<String>(), x, y); | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public void addSuperpower(String superpower) { | |
this.superpowers.add(superpower); | |
} | |
public String getSuperpower(int index) { | |
return this.superpowers.get(index); | |
} | |
public ArrayList<String> getSuperpowers() { | |
return this.superpowers; | |
} | |
public void removeSuperpower(int index) { | |
this.superpowers.remove(index); | |
} | |
public void setX(int x) { | |
this.x = x; | |
} | |
public int getX() { | |
return this.x; | |
} | |
public void setY(int y) { | |
this.y = y; | |
} | |
public int getY() { | |
return this.y; | |
} | |
public void move(int dx, int dy) { | |
this.x += dx; | |
this.y += dy; | |
} | |
public void draw(Graphics g) { | |
g.setColor(Color.BLACK); | |
g.drawOval(this.x, this.y, MarioChar.CIRCLE_RADIUS, MarioChar.CIRCLE_RADIUS); | |
g.drawRect(this.x, this.y + MarioChar.CIRCLE_RADIUS, MarioChar.CIRCLE_RADIUS, MarioChar.CIRCLE_RADIUS * 2); | |
g.drawLine((int) (this.x + MarioChar.CIRCLE_RADIUS / 1.5), this.y + MarioChar.CIRCLE_RADIUS * 3, this.x + MarioChar.CIRCLE_RADIUS, (int) (this.y + MarioChar.CIRCLE_RADIUS * 4.25)); | |
g.drawLine((int) (this.x + MarioChar.CIRCLE_RADIUS / 2.5), this.y + MarioChar.CIRCLE_RADIUS * 3, this.x, (int) (this.y + MarioChar.CIRCLE_RADIUS * 4.25)); | |
g.setColor(Color.YELLOW); | |
g.fillOval(this.x, this.y, MarioChar.CIRCLE_RADIUS, MarioChar.CIRCLE_RADIUS); | |
g.setColor(Color.RED); | |
g.fillRect(this.x, this.y + MarioChar.CIRCLE_RADIUS, MarioChar.CIRCLE_RADIUS, MarioChar.CIRCLE_RADIUS * 2); | |
} | |
public String toString() { | |
return "<MarioChar> " + this.name + " (x: " + this.x + ", y: " + this.y + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment