This is a homework problem. There are a few other java files involved. Note that shapes.Rectangle and shapes.Oval contain a getArea method. I'm having a maddening time trying to figure this out any help is appreciated.
package model;
import java.awt.Color;
import java.awt.Container;
import shapes.Line;
import shapes.Oval;
import shapes.Rectangle;
import shapes.Shape;
import shapes.Triangle;
import interfaces.ComparableShape;
import interfaces.Resettable;
public class Model implements Resettable,ComparableShape {
private Container container;
private String message;
private String action = DRAW;
private boolean fill = false;
private String currentShapeType;
private Shape currentShape;
private Color fillColor = Color.gray;
public Color lineColor;
public final static String DRAW = "Draw";
public final static String MOVE = "Move";
public final static String REMOVE = "Remove";
public final static String RESIZE = "Resize";
public final static String FILL = "Fill";
public final static String CHANGE = "Change";
public final static String RECTANGLE = "Rectangle";
public final static String OVAL = "Oval";
public final static String LINE = "Line";
public final static String TRIANGLE = "Triangle";
public static String[] selections = {"Rectangle", "Oval", "Line", "Triangle"};
//project 9 begin
public Shape[] myShapes = new Shape[2];
//project 9 stop
public Shape createShape() {
if(currentShapeType == RECTANGLE){
currentShape = new Rectangle(0, 0, 0, 0, lineColor, fillColor, fill);
}
if(currentShapeType == OVAL) {
currentShape = new Oval(0,0,0,0, lineColor, fillColor, fill);
}
if(currentShapeType == LINE) {
currentShape = new Line(0,0,0,0, lineColor, fillColor, fill);
}
if(currentShapeType == TRIANGLE) {
currentShape = new Triangle(0,0,0,0, lineColor, fillColor, fill);
}
//project 9 start
if(myShapes[0] == null) {
myShapes[0]=currentShape;
}
else {
myShapes[1]=currentShape;
}
//project 9 stop
return currentShape;
}
public Shape getCurrentShape() {
return currentShape;
}
//project 9 begin
public Shape[] getMyShapearray() {
return myShapes;
}
//project 9 end
public String getCurrentShapeType(){
return currentShapeType;
}
public void setCurrentShapeType(String shapeType){
currentShapeType = shapeType;
}
public Model(Container container) {
this.container = container;
}
public void repaint() {
container.repaint();
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public boolean isFill() {
return fill;
}
public void setFill(boolean fill) {
this.fill = fill;
}
public void setMessage(String msg) {
this.message = msg;
}
public String getMessage() {
return this.message;
}
public Color getLineColor() {
return this.lineColor;
}
public void setLineColor(Color c) {
this.lineColor = c;
}
public String toString() {
return "Model:\n\tAction: " + action + "\n\tFill: " + fill + "\n\tArea: " ;
}
public void resetComponents() {
action = DRAW;
currentShape = null;
myShapes = null;
if (container instanceof Resettable) {
((Resettable) container).resetComponents();
}
}
//Add a method to your model called compareShapes(),
//which will return either 0, 1, or 2--1 if the area of the first Shape is bigger than the second,
//2 if it is smaller, and 0 if the two Shapes are the same size.
//Create an interface named ComparableShape that will be used by the shape objects.
//The interface should require implementing classes to have a
//method getArea() capable of returning the area of the object. Obviously, only closed shapes can do this.
//The instanceof operator will be handy here.
public int getArea() {
return getWidth()*getHeight();
}
private int getHeight() {
///what goes here?!
return 0;
}
private int getWidth() {
//what goes here?!
return 0;
}
public int compareShapes(ComparableShape b) {
ComparableShape oneToCompare = null;
if (b instanceof ComparableShape) {
oneToCompare = (ComparableShape)b;
if (getArea() < oneToCompare.getArea()) return 2; // this one is smaller
if (getArea() > oneToCompare.getArea()) return 1; // this one is larger
return 0;
}
return 0;
}
}
Your getArea() should have something like this:-
If currentShapeType is not a LINE (since a line is not closed shape), then calculate and return area of currentShape based on the currentShapeType.
The area of a rectangle is length*width. It's different for the other shapes though. So for a rectangle you need access to its length and width. For a circle you would need its radius. The area of a circle is piR^2.
As someone mentioned below, for an ellipse:
The area of an ellipse is pi*a*b, where a and b are half the length of the major and minor axes. The area of a circle could be written as pi*r*r or pi*r^2 since a and b would be equal.
Related
Say I have this view with two subviews A and B, A and B has a color property mColor,
I want a function in the parent view that has this signature: int getColor(int x, int y), which for any given x and y coordinate, return the color at that position, if the x and y coordinates land and the two shapes are overlapped, return the average color of A and B
The problem I am running into is i see myself doing a lot of conditional checks, checking if A is left of B or if A is right of B etc. I feel like I am missing some key intuition
I have a Point class that handles the coordinates"
public class Point {
private final int mX;
private final int mY;
public Point(int mX, int mY) {
this.mX = mX;
this.mY = mY;
}
public int getX() {
return mX;
}
public int getY() {
return mY;
}
}
Here is my subview class:
public class Chart {
private int mColor;
private Point mTopLeft;
private Point mBottomRight;
public Point getTopLeft() {
return mTopLeft;
}
public Point getBottomRight() {
return mBottomRight;
}
public Chart(int mColor, Point topLeft, Point bottomRight) {
this.mColor = mColor;
this.mTopLeft = topLeft;
this.mBottomRight = bottomRight;
}
public int getColor() {
return mColor;
}
public Point getTopRightCorner() {
return new Point(getBottomRight().getX(), getTopLeft().getY());
}
public Point getBottomLeftCorner() {
return new Point(getTopLeft().getX(), getBottomRight().getY());
}
}
My parent view class:
public class View {
private Chart mChartA;
private Chart mChartB;
public View(Chart chartA,
Chart chartB) {
mChartA = chartA;
mChartB = chartB;
}
public boolean doChartsOverlap() {
boolean isOverlapped = true;
if(isChartALeftOfChartBInX() || isChartARightOfChartBInX()) {
isOverlapped = false;
}
if(isChartABottomOfChartBInY() || isChartATopOfChartBInY()) {
isOverlapped = false;
}
return isOverlapped;
}
public final boolean isChartALeftOfChartBInX() {
return mChartA.getBottomRight().getX() <= mChartB.getTopLeft().getX();
}
public final boolean isChartARightOfChartBInX() {
return mChartA.getTopLeft().getX() >= mChartB.getBottomRight().getX();
}
public final boolean isChartATopOfChartBInY() {
return mChartA.getBottomRight().getY() <= mChartB.getTopLeft().getY();
}
public final boolean isChartABottomOfChartBInY() {
return mChartA.getTopLeft().getY() >= mChartB.getBottomRight().getY();
}
public void setChartA(Chart mChartA) {
this.mChartA = mChartA;
}
public void setChartB(Chart mChartB) {
this.mChartB = mChartB;
}
}
Logically, you want to test if the point is in both rectangles; so test if it is in one rectangle and also in the other rectangle. Everything else about whether one rectangle is above, below, left or right of the other is a red herring.
Add this method to the Chart class (use < instead of <= if you only want to paint the interiors):
public boolean contains(Point p) {
return mTopLeft.getX() <= p.getX() && p.getX() <= mBottomRight.getX()
&& mTopLeft.getY() <= p.getY() && p.getY() <= mBottomRight.getY();
}
And this method to the View class:
public int getColor(Point p) {
boolean inA = mChartA.contains(p), inB = mChartB.contains(p);
if(inA && inB) {
// ...
} else if(inA) {
// ...
} else if(inB) {
// ...
} else {
// ...
}
}
You can then overload public int getColor(int x, int y) to return getColor(new Point(x, y)).
Rectangle r1 = new Rectangle(x1, y1, width1, height1 );
Rectangle r2 = new Rectangle(x2, y2, width2, height2);
if ( r1.contains( x3, y3 ) && r2.contains( x3, y3 ) ) {
// (x3,y3) is in both rectangles
}
I just wrote some code to make my player move in my little maze game, but nothing happens. Also my maze is not drawn correct as in the matrix input. I don't figure out why is wrong this code...any help is well appeciated.
Thank you!
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.*;
public class Main extends JPanel {
private static Image white;
private static Image black;
private static Image finish;
private static Image player;
private static int x = 1;
private static int y = 1;
private String matrix[][];
public Main() {
addKeyListener(new Keys());
setFocusable(true);
}
public static String[][] load(String input) {
List<String[]> rows = new ArrayList<>();
try (Scanner scanner = new Scanner(input)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] cols = new String[line.length()];
for (int i = 0; i < cols.length; i++) {
cols[i] = line.substring(i, i + 1);
}
rows.add(cols);
}
}
return rows.toArray(new String[rows.size()][]);
}
public static JFrame buildFrame() {
JFrame frame = new JFrame("Labyrinth Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 950);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
return frame;
}
public void moveUp() {
x += 0;
y += -1;
}
public void moveLeft() {
x += -1;
y += 0;
}
public void moveDown() {
x += 0;
y += 1;
}
public void moveRight() {
x += 1;
y += 0;
}
public class Keys extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
// repaint();
if (keycode == KeyEvent.VK_W) {
if (!matrix[getX()][getY() - 1].equals("1")) {
moveUp();
}
}
if (keycode == KeyEvent.VK_A) {
if (!matrix[getX() - 1][getY()].equals("1")) {
moveLeft();
}
}
if (keycode == KeyEvent.VK_S) {
if (!matrix[getX()][getY() + 1].equals("1")) {
moveDown();
}
}
if (keycode == KeyEvent.VK_D) {
if (!matrix[getX() + 1][getY()].equals("1")) {
moveRight();
}
}
}
#Override
public void keyReleased(KeyEvent event) {
}
}
public static void main(String[] args) {
String input = "1111111111111111111111111111111111111111111\n"
+ "1000000010001000001000000010000000100000001\n"
+ "1010111010101010101111101011111010111111101\n"
+ "1010001010100010100000001010000010000010001\n"
+ "1011101010111110101111111010111111111010111\n"
+ "1000101010100000101000001000100010000010001\n"
+ "1011101011101011111011101111111010111110101\n"
+ "1010001000001010000010100000001010000010101\n"
+ "1010111111111010111110111111101011111011101\n"
+ "1010100000100010100000000000101000000000101\n"
+ "1110101111101110111110111011101011111110101\n"
+ "1000100000000010000010100010001000100010001\n"
+ "1011111111111111111011101010111111101011101\n"
+ "1000000000000000100010001010000000001010001\n"
+ "1011111111111011101110111011111111111010111\n"
+ "1000100010001000001010001000100000001010101\n"
+ "1110101011101111111010101110111110111010101\n"
+ "1000101010001000100000101000100000100010001\n"
+ "1011101010111010101111101011101110101111111\n"
+ "1000001010000010000000101000001000100010001\n"
+ "1111111011111110111111101111111011111010101\n"
+ "1000001010000010100010001000000010000010101\n"
+ "1011111010111011101010111011111110101110101\n"
+ "1010000010001010001010001000100000101010101\n"
+ "1010111111101010111011101111101111101011101\n"
+ "1000100000001010101010001000100010101000101\n"
+ "1011111011111010101010111010111010101011101\n"
+ "1010000010001000101010000010001010001000001\n"
+ "1010101110101111101011101111101011111010101\n"
+ "1010101000101000001000101000001000000010101\n"
+ "1011101011111010111110111011101111111110111\n"
+ "1000001000000010000000000010000000000010021\n"
+ "1111111111111111111111111111111111111111111\n";
String[][] matrix = load(input);
JFrame frame = buildFrame();
ImageIcon img = new ImageIcon("C:/Users/Desktop/black20.png");
black = img.getImage();
img = new ImageIcon("C:/Users/Desktop/gri20.png");
white = img.getImage();
img = new ImageIcon("C:/Users/Desktop/finish20.png");
finish = img.getImage();
img = new ImageIcon("C:/Users/Desktop/smiley20.png");
player = img.getImage();
// frame.add(new Player());
JPanel pane = new JPanel() {
#Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j].equals("1")) {
g.drawImage(black, i * 20, j * 20, null);
}
if (matrix[i][j].equals("0")) {
g.drawImage(white, i * 20, j * 20, null);
}
if (matrix[i][j].equals("2")) {
g.drawImage(finish, i * 20, j * 20, null);
}
}
}
g.drawImage(player, x * 20, y * 20, null);
}
};
frame.add(pane);
frame.add(new Main());
}
}
It should look like:
Problems/Suggestions:
You're adding more than one component to the JFrame in a default fashion. Since the JFrame's contentPane uses BorderLayout, only one component will display, the last one added, and the other will be completely covered and will remain invisible.
Your Main JPanel is not being used as a true JPanel. Nothing is being added to it, and in fact it looks like it should be a logical class and not a component class.
Rather than using KeyListeners, which are very fidgety when it comes to focus problems, use Key Bindings which will allow you to get around focus issues in a clean and higher level way.
Don't override the JPanel's paint method but rather its paintComponent method as this will give you several advantages, including default use of double buffering for your animation.
Don't give component classes a public int getX() and public int getY() method without care since these override key methods that place the component within its container. Since the Main class shouldn't even extend JPanel, this will end up to be a non-issue for your code, but in the future, it would mess your program up.
When you run into similar problems, such as a KeyListener not working, remove the issue from your big program and try to reproduce it in a small separate program. This will give you a much cleaner environment for helping you to isolate and understand your problem and thereby help you fix it.
Your program is mis-using the static modifier. Your x and y fields should not be static and should not be accessed in a static way.
You've got way too much code within your main method, which is one of the reasons why you likely made x and y static, because you were forced to do so since you're trying to access them within main. The solution is not to make the fields static but to get all that code out of the static world and into the instance world.
Edit
For some reason the question intrigued me, and so I decided to try to M-V-C or Model-View-Controller it if possible and see what I could come up with. So here goes a bunch of classes that sort of work, beginning with a text file that holds the data.
The GUI looks like:
It must be in the same location as the class files since it is obtained as a resource and must have the file name "input.txt"
1111111111111111111111111111111111111111111
1000000010001000001000000010000000100000001
1010111010101010101111101011111010111111101
1010001010100010100000001010000010000010001
1011101010111110101111111010111111111010111
1000101010100000101000001000100010000010001
1011101011101011111011101111111010111110101
1010001000001010000010100000001010000010101
1010111111111010111110111111101011111011101
1010100000100010100000000000101000000000101
1110101111101110111110111011101011111110101
1000100000000010000010100010001000100010001
1011111111111111111011101010111111101011101
1000000000000000100010001010000000001010001
1011111111111011101110111011111111111010111
1000100010001000001010001000100000001010101
1110101011101111111010101110111110111010101
1000101010001000100000101000100000100010001
1011101010111010101111101011101110101111111
1000001010000010000000101000001000100010001
1111111011111110111111101111111011111010101
1000001010000010100010001000000010000010101
1011111010111011101010111011111110101110101
1010000010001010001010001000100000101010101
1010111111101010111011101111101111101011101
1000100000001010101010001000100010101000101
1011111011111010101010111010111010101011101
1010000010001000101010000010001010001000001
1010101110101111101011101111101011111010101
1010101000101000001000101000001000000010101
1011101011111010111110111011101111111110111
1000001000000010000000000010000000000010021
1111111111111111111111111111111111111111111
Next the main program, the one that creates the model, the view, and the controller, hooks them all together, and then displays the GUI:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* link: http://stackoverflow.com/a/41418250/522444
*
* #author Pete
*
*/
#SuppressWarnings("serial")
public class Main2 extends JPanel {
private View mainPanel;
public Main2(MatrixModel matrixModel) {
mainPanel = new View(matrixModel);
new Controller(matrixModel, mainPanel);
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
}
private static void createAndShowGui(MatrixModel model) {
Main2 mainPanel = new Main2(model);
JFrame frame = new JFrame("Main2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
final MatrixModel model = MatrixUtil.getInput(MatrixUtil.PATH_TO_RSC);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui(model);
}
});
}
}
Next a utility class with static methods for reading in the text file as a resource and converting it into a Model object:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MatrixUtil {
// again this text file must be in the jar file or the code base
// at the same location as the class / java files
public static final String PATH_TO_RSC = "input.txt";
public static MatrixModel getInput(String resourcePath) {
InputStream is = MatrixUtil.class.getResourceAsStream(resourcePath);
if (is == null) {
String text = "resourcePath is not found and not loading text: " + resourcePath;
throw new IllegalArgumentException(text);
}
return getInput(is);
}
public static MatrixModel getInput(InputStream is) {
MatrixModel model = null;
try (Scanner scan = new Scanner(is)) {
List<List<MatrixPosition>> listOfLists = new ArrayList<>();
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.trim().isEmpty()) {
continue;
}
List<MatrixPosition> list = new ArrayList<>();
for (char c : line.toCharArray()) {
list.add(MatrixPosition.getMatrixPosition(String.valueOf(c)));
}
listOfLists.add(list);
}
MatrixPosition[][] grid = new MatrixPosition[listOfLists.size()][];
for (int i = 0; i < grid.length; i++) {
List<MatrixPosition> list = listOfLists.get(i);
grid[i] = list.toArray(new MatrixPosition[] {});
}
model = new MatrixModel(grid, new SpritePosition(1, 1));
}
return model;
}
}
Basic enum to represent direction:
public enum Direction {
UP, DOWN, LEFT, RIGHT
}
Another enum to represent a location point in the grid, and whether it is a wall, a coridor or the end as well as static methods to convert from number to MatrixPosition:
public enum MatrixPosition {
WALL(1), CORRIDOR(0), END(2);
private int value;
private MatrixPosition(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static MatrixPosition getMatrixPosition(int value) {
for (MatrixPosition position : MatrixPosition.values()) {
if (value == position.getValue()) {
return position;
}
}
String text = "value of " + value;
throw new IllegalArgumentException(text);
}
public static MatrixPosition getMatrixPosition(String strValue) {
int value = -1;
try {
value = Integer.parseInt(strValue);
} catch (NumberFormatException e) {
String text = "NumberFormatException for strValue " + strValue;
throw new IllegalAccessError(text);
}
return getMatrixPosition(value);
}
}
A class to represent a position of our sprite, similar to the java.awt.Point class but with row and column fields instead of x and y:
public class SpritePosition {
int row;
int column;
public SpritePosition(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
public void setRowColumn(int row, int column) {
this.row = row;
this.column = column;
}
}
The model has property change support code so that it can notify any classes listening to it of any changes in its state. The controller will be the class listening to the model
import java.beans.PropertyChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;
public class MatrixModel {
public static final String SPRITE_POINT = "sprite point";
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(this);
private MatrixPosition[][] grid;
private SpritePosition spritePosition;
public MatrixModel(MatrixPosition[][] grid, SpritePosition spritePosition) {
this.grid = grid;
this.spritePosition = spritePosition;
}
public int getRows() {
return grid.length;
}
public int getColumns() {
return grid[0].length;
}
public MatrixPosition getPosition(SpritePosition p) {
return getPosition(p.row, p.column);
}
public MatrixPosition getPosition(int row, int col) {
return grid[row][col];
}
public void setSpritePoint(SpritePosition spritePosition) {
SpritePosition oldValue = this.spritePosition;
SpritePosition newValue = spritePosition;
this.spritePosition = spritePosition;
pcSupport.firePropertyChange(SPRITE_POINT, oldValue, newValue);
}
public boolean isPointValid(SpritePosition p) {
if (p.column < 0 || p.row < 0) {
return false;
}
if (p.column >= grid[0].length || p.row >= grid.length) {
return false;
}
return grid[p.row][p.column] == MatrixPosition.CORRIDOR;
}
public boolean isMoveValid(Direction direction) {
int row = spritePosition.row;
int column = spritePosition.column;
switch (direction) {
case UP:
return isPointValid(new SpritePosition(row - 1, column));
case DOWN:
return isPointValid(new SpritePosition(row + 1, column));
case LEFT:
return isPointValid(new SpritePosition(row, column - 1));
case RIGHT:
return isPointValid(new SpritePosition(row, column + 1));
default:
return false;
}
}
public void move(Direction direction) {
if (!isMoveValid(direction)) {
String text = "For move to " + direction + "spritePosition: " + spritePosition;
throw new IllegalArgumentException(text);
}
int row = spritePosition.row;
int column = spritePosition.column;
switch (direction) {
case UP:
setSpritePoint(new SpritePosition(row - 1, column));
break;
case DOWN:
setSpritePoint(new SpritePosition(row + 1, column));
break;
case LEFT:
setSpritePoint(new SpritePosition(row, column - 1));
break;
case RIGHT:
setSpritePoint(new SpritePosition(row, column + 1));
break;
default:
break;
}
}
public SpritePosition getSpritePosition() {
return spritePosition;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
public void addPropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(name, listener);
}
public void removePropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(name, listener);
}
}
Controller class, one that sets up key bindings on the view so that it can listen for key presses, checks if they represent a valid move, and if so then tells the model to make the move. It also adds a listener to the model so that when its state changes, it will tell the view to move the sprite
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.EnumMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class Controller {
private MatrixModel model;
private View view;
private Map<Direction, KeyStroke> dirKeyMap = new EnumMap<>(Direction.class);
public Controller(MatrixModel model, View view) {
this.model = model;
this.view = view;
dirKeyMap.put(Direction.DOWN, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0));
dirKeyMap.put(Direction.UP, KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0));
dirKeyMap.put(Direction.LEFT, KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0));
dirKeyMap.put(Direction.RIGHT, KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
model.addPropertyChangeListener(new ModelListener());
setUpKeyBindings(view);
}
private void setUpKeyBindings(View view) {
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = view.getInputMap(condition);
ActionMap actionMap = view.getActionMap();
for (Direction dir : Direction.values()) {
KeyStroke keyStroke = dirKeyMap.get(dir);
hookUp(inputMap, actionMap, dir, keyStroke);
}
}
private void hookUp(InputMap inputMap, ActionMap actionMap, Direction dir, KeyStroke key) {
inputMap.put(key, key.toString());
actionMap.put(key.toString(), new MoveAction(dir, model));
}
public MatrixModel getModel() {
return model;
}
public View getView() {
return view;
}
class ModelListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (MatrixModel.SPRITE_POINT.equals(evt.getPropertyName())) {
SpritePosition p = model.getSpritePosition();
view.setSpritePoint(p);
}
}
}
}
#SuppressWarnings("serial")
class MoveAction extends AbstractAction {
private Direction dir;
private MatrixModel model;
public MoveAction(Direction dir, MatrixModel model) {
super(dir.toString());
this.dir = dir;
this.model = model;
}
public void actionPerformed(ActionEvent e) {
if (model.isMoveValid(dir)) {
model.move(dir);
}
}
}
Finally the View class, that extends JPanel, that displays the maze and the sprite:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class View extends JPanel {
private static final int CELL_WIDTH = 20;
private static final Color CORRIDOR_COLOR = Color.LIGHT_GRAY;
private static final Color WALL_COLOR = Color.DARK_GRAY;
private static final Color END_COLOR = Color.ORANGE;
private static final Color SPRITE_COLOR = Color.RED;
private static final int GAP = 1;
private BufferedImage gridImg = null;
private SpritePosition spritePosition;
private JPanel mainPanel = new JPanel();
public View(MatrixModel matrixModel) {
gridImg = createImg(matrixModel);
spritePosition = matrixModel.getSpritePosition();
}
public JPanel getMainPanel() {
return mainPanel;
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || gridImg == null) {
return super.getPreferredSize();
}
int prefW = gridImg.getWidth();
int prefH = gridImg.getHeight();
return new Dimension(prefW, prefH);
}
public void setSpritePoint(SpritePosition spritePosition) {
this.spritePosition = spritePosition;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (gridImg != null) {
g.drawImage(gridImg, 0, 0, this);
}
g.setColor(SPRITE_COLOR);
int y = spritePosition.row * CELL_WIDTH + GAP;
int x = spritePosition.column * CELL_WIDTH + GAP;
g.fillRect(x, y, CELL_WIDTH - 2 * GAP, CELL_WIDTH - 2 * GAP);
}
private BufferedImage createImg(MatrixModel matrixModel) {
BufferedImage img = null;
if (matrixModel != null && matrixModel.getRows() > 0) {
int w = matrixModel.getColumns() * CELL_WIDTH;
int h = matrixModel.getRows() * CELL_WIDTH;
img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
for (int row = 0; row < matrixModel.getRows(); row++) {
for (int col = 0; col < matrixModel.getColumns(); col++) {
MatrixPosition position = matrixModel.getPosition(row, col);
Color c = null;
switch (position) {
case CORRIDOR:
c = CORRIDOR_COLOR;
break;
case WALL:
c = WALL_COLOR;
break;
case END:
c = END_COLOR;
break;
}
g2.setColor(c);
int x = col * CELL_WIDTH;
int y = row * CELL_WIDTH;
g2.fillRect(x, y, CELL_WIDTH, CELL_WIDTH);
}
}
g2.dispose();
}
return img;
}
}
So, i kinda wrote this Code for the game called "towers of hanoi".
The code i had before was showing the moves for every step in the console. I am trying to show the moves graphically.
I think i came pretty far, but i cant implement the paint Method.
I tried several things like g.drawRect(s1.x,s1.y+200,s1.width,s1.height); but its just showing an rectangle, no moves etc.
Heres my Code:
import java.io.*;
import java.util.*;
import java.awt.*;
public class hanoi {
public static void ziehe_scheibe(MyFrame f,Pole von, Pole nach) {
int hs = von.onPole.size();
int ht = nach.onPole.size();
int hy = -25*(ht-hs+1);
int hx = (nach.xPos - von.xPos);
String d = von.scheibenehmen();
nach.scheibelegen(d);
f.moveDisk(d,hx,hy);
}
public static void hanoi(MyFrame f,int n, Pole platz1, Pole platz2, Pole hilfsplatz) {
if ( n==1 )
ziehe_scheibe(f,platz1,platz2);
else
{ hanoi(f,n-1,platz1,hilfsplatz,platz2);
ziehe_scheibe(f,platz1,platz2);
hanoi(f,n-1,hilfsplatz,platz2,platz1);
}
}
public static int xSize(String d) {
if ( d == "klein" ) return 30;
if ( d == "mittel" ) return 40;
else return 50;
}
public static void main(String args[]) {
Pole p1 = new Pole("a",60);
Pole p2 = new Pole("b",180);
Pole p3 = new Pole("c",300);
p1.scheibelegen("groß");
p1.scheibelegen("mittel");
p1.scheibelegen("klein");
Rectangle r1 = new Rectangle(p1.xPos-50,50,100,20);
Rectangle r2 = new Rectangle(p1.xPos-40,25,80,20);
Rectangle r3 = new Rectangle(p1.xPos-30,0,60,20);
MyFrame fff = new MyFrame(r1,r2,r3);
fff.setSize(800,600);
fff.setVisible(true);
hanoi(fff,3,p1,p3,p2);
}
}
// Frame-Klasse zur Anzeige des Zustandes
class MyFrame extends Frame {
public Rectangle s1, s2, s3;
public MyFrame(Rectangle a, Rectangle b, Rectangle c) {
s1 = a; s2 = b; s3 = c;
}
public void paint(Graphics g) {
}
public Rectangle xRect(String d) {
if ( d == "klein" ) return s3;
if ( d == "mittel" ) return s2;
else return s1;
}
public void moveDisk(String name,int dx,int dy)
{ xRect(name).translate(dx,dy);
repaint();
try
{ System.in.read();System.in.read(); }
catch (Exception e) {}
}
}
// Pole-Klasse: Repräsentation eines Stabes
class Pole {
public String label;
public Vector onPole;
public int xPos;
public Pole(String s, int i) {
onPole=new Vector();
label = s; xPos = i;
}
public void scheibelegen(String d) {
this.onPole.addElement(d);
}
public String scheibenehmen() {
Object lastEl = this.onPole.lastElement();
String lastElStr = lastEl.toString();
this.onPole.removeElement(lastEl);
return lastElStr;
}
}
Maybe you guys can take a look on it
btw excuse my bad english
Try
public static int xSize(String d) {
if ( d.equals("klein")) return 30;
if ( d.equals("mittel")) return 40;
else return 50;
}
I (A novice programmer) am trying to paint an oval over a JPanel. I am trying to use the method paint. However, it requires a Graphics argument. I get a NullPointerException when I include my Graphics as a argument because it is null, but I do not know how else to paint the oval. I tried repaint instead but nothing happened. Any help would be appreciated. Here is my main class:
public class Checkers extends JPanel{
public static final int BOARDSQUARES = 8;
public static final int BOARDSIZE = 75;
private JPanel[][] board;
private final Point point1;
private final LineBorder border1;
public JFrame frame;
checkerPiece piece = new checkerPiece(this);
Graphics g;
public Checkers(){
board = new JPanel[BOARDSQUARES][BOARDSQUARES];
point1 = new Point (BOARDSIZE,BOARDSIZE);
border1=new LineBorder(Color.BLACK, 1);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Checkers checkers = new Checkers();
checkers.frame=checkers.createJFrame();
checkers.board =checkers.createBoard(checkers.board, checkers.point1, checkers.border1);
for (int y=0;y<BOARDSQUARES;y++){
for (int x = 0;x<BOARDSQUARES;x++){
checkers.frame.getContentPane().add(checkers.board[y][x]);
}
}
checkers.paint(checkers.g);
}
private JPanel[][] createBoard (JPanel[][] board, Point point, LineBorder border1){
for (int row = 0; row<BOARDSQUARES;row++){
point.y=BOARDSIZE;
for (int col = 0; col <BOARDSQUARES;col++){
board[row][col] = new JPanel();
board[row][col].setLocation(point);
board[row][col].setVisible(true);
board[row][col].setSize(BOARDSIZE,BOARDSIZE);
board[row][col].setOpaque(true);
if ((row%2==0&&col%2==0)||(row%2==1&&col%2==1)){
board[row][col].setBackground(new Color (230,200,150));
} else if ((row%2==0&&col%2==1)||(row%2==1&&col%2==0)) {
board[row][col].setBackground(new Color (165, 42,42) );
}
board[row][col].setBorder(border1);
point.y = point.y+BOARDSIZE;
}
point.x=point.x+BOARDSIZE;
}
return board;
}
private JFrame createJFrame (){
JFrame mainFrame = new JFrame("Checkers");
mainFrame.setSize(1000,1000);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(new Checkers());
return mainFrame;
}
#Override
public void paint (Graphics g){
System.out.println("hi");
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
piece.paint(g2d,board[0][0].getLocation().x,board[0][0].getLocation().y,Color.BLACK);
}
}
A necessary snippet from my other class (cherkerPiece piece):
public void paint (Graphics2D g, int x, int y, Color color){
g.setColor(color);
g.fillOval(x, y, DIAMETER, DIAMETER);
}
Thank you for your help
To create something as complex as a checkers game, you should follow the model / view / controller pattern when creating your Java Swing GUI.
I created a model class for a checker board square and another model class for a checker piece. I created a model class for the checker board and another model class to hold all of the checker pieces.
I created a view class to draw the checker board. I created another view class to create the main window.
I did not create any controller classes. This code just shows you how to draw a checker board. I put all of the classes together to make it easier to paste the code. You should separate the classes into separate Java files.
package com.ggl.testing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CheckerBoard implements Runnable {
private Board board;
private CheckerBoardPanel checkerBoardPanel;
private JFrame frame;
private Pieces pieces;
public static void main(String[] args) {
SwingUtilities.invokeLater(new CheckerBoard());
}
public CheckerBoard() {
this.board = new Board();
this.pieces = new Pieces();
}
#Override
public void run() {
frame = new JFrame("Checker Board");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkerBoardPanel = new CheckerBoardPanel(board, pieces);
frame.add(checkerBoardPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class CheckerBoardPanel extends JPanel {
private static final long serialVersionUID = 3770663347384271771L;
private Board board;
private Pieces pieces;
public CheckerBoardPanel(Board board, Pieces pieces) {
this.board = board;
this.pieces = pieces;
this.setPreferredSize(board.getPreferredSize());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Square[][] checkerBoard = board.getBoard();
for (int row = 0; row < checkerBoard.length; row++) {
for (int column = 0; column < checkerBoard[row].length; column++) {
Square square = checkerBoard[row][column];
Rectangle drawingRectangle = square.getDrawingRectangle();
g.setColor(square.getColor());
g.fillRect(drawingRectangle.x, drawingRectangle.y,
drawingRectangle.width, drawingRectangle.height);
}
}
List<Piece> checkerPieces = pieces.getPieces();
for (Piece checkerPiece : checkerPieces) {
Point coordinate = checkerPiece.getCoordinate();
Rectangle drawingRectangle = checkerBoard[coordinate.x][coordinate.y]
.getDrawingRectangle();
int x = drawingRectangle.x + drawingRectangle.width / 2;
int y = drawingRectangle.y + drawingRectangle.height / 2;
int radius = board.getSquareWidth() * 2 / 6;
g.setColor(checkerPiece.getColor());
g.fillOval(x - radius, y - radius, radius + radius, radius
+ radius);
}
}
}
public class Board {
private static final int BOARD_WIDTH = 8;
private static final int SQUARE_WIDTH = 64;
private Square[][] board;
public Board() {
this.board = initalizeBoard(BOARD_WIDTH, SQUARE_WIDTH);
}
private Square[][] initalizeBoard(int boardWidth, int squareWidth) {
Square[][] board = new Square[boardWidth][boardWidth];
int x = 0;
int y = 0;
for (int row = 0; row < boardWidth; row++) {
for (int column = 0; column < boardWidth; column++) {
Square square = new Square();
if (isLightSquare(row, column)) {
square.setColor(Color.WHITE);
} else {
square.setColor(Color.LIGHT_GRAY);
}
square.setCoordinate(new Point(row, column));
square.setDrawingRectangle(new Rectangle(x, y, squareWidth,
squareWidth));
board[row][column] = square;
x += squareWidth;
}
x = 0;
y += squareWidth;
}
return board;
}
public boolean isLightSquare(int row, int column) {
if (row % 2 == 0) {
if (column % 2 == 0) {
return true;
} else {
return false;
}
} else {
if (column % 2 == 0) {
return false;
} else {
return true;
}
}
}
public Dimension getPreferredSize() {
int width = SQUARE_WIDTH * 8 + 1;
return new Dimension(width, width);
}
public Square[][] getBoard() {
return board;
}
public int getSquareWidth() {
return SQUARE_WIDTH;
}
}
public class Square {
private Color color;
private Point coordinate;
private Rectangle drawingRectangle;
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
public Rectangle getDrawingRectangle() {
return drawingRectangle;
}
public void setDrawingRectangle(Rectangle drawingRectangle) {
this.drawingRectangle = drawingRectangle;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
public class Pieces {
private List<Piece> pieces;
public Pieces() {
this.pieces = addPieces();
}
private List<Piece> addPieces() {
List<Piece> pieces = new ArrayList<Piece>();
Piece piece = new Piece();
piece.setColor(Color.RED);
piece.setCoordinate(new Point(2, 1));
pieces.add(piece);
piece = new Piece();
piece.setColor(Color.BLACK);
piece.setCoordinate(new Point(5, 0));
pieces.add(piece);
// Add the rest of the red and black pieces here
return pieces;
}
public List<Piece> getPieces() {
return pieces;
}
public void addPiece(Piece piece) {
this.pieces.add(piece);
}
}
public class Piece {
private Color color;
private Point coordinate;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
}
}
you need to add a class that extends Canvas and do all your painting in that class. like this
public class FirstWindow extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public FirstWindow()
{
super("Kayte does not go to water parks");
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}
}
public class Start extends Canvas implements Runnable
public class Main
{
public static void main(String arg[])
{
FirstWindow fw = new FirstWindow();
Start game = new Start(500, 500);
fw.add(game);
fw.setVisible(true);
new Thread(game).start();
}
}
I am trying to make a Sprite move across a background image in swing, but every time I update the position and it repaints, it repaints with the previous position of the image as well. However I am just trying to make it so that it moves 10 pixels for every time the right arrow is pressed. Here is the Main class, which controls the movement.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Main extends JFrame implements KeyListener, ActionListener{
private static final long serialVersionUID = 1L;
//Caitlan info
Image caitlanImage = new ImageIcon("Caitlan.png").getImage();
Person caitlan = new Person(caitlanImage, "Caitlan", 50, 200, true);
//Jake info
Image jakeImage = new ImageIcon("Jake.png").getImage();
Person jake = new Person(jakeImage, "Jake", 0, 200, true);
//Level objects
Image granadaBackground = new ImageIcon("Granada Background.jpg").getImage();
Level granada = new Level(granadaBackground, "Granada", new Point(600, 300));
Image elevatorBackground = new ImageIcon("Elevator.png").getImage();
Level elevator = new Level(elevatorBackground, "Elevator", new Point(0,0));
public static void main(String[] args) throws InterruptedException{
new Main();
}
public Main() throws InterruptedException{ //Constructor (only should be called once)
setSize(700,300);
setTitle("Project Anniversary");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addKeyListener(this);
}
public void paint(Graphics g){ //Overridden paint method.
if(checkIfLevelDone(granada, caitlan)){
granada.setDone();
}
if(!granada.isDone()){
g.drawImage(granada.getImage(), 0, 0, granadaBackground.getWidth(null), granadaBackground.getHeight(null),null);
g.drawImage(jake.getImage(), jake.getPosX(), jake.getPosY(), jake.getImage().getWidth(null), jake.getImage().getHeight(null),null);
g.drawImage(caitlan.getImage(), caitlan.getPosX(), caitlan.getPosY(), caitlan.getImage().getWidth(null), caitlan.getImage().getHeight(null), null);
}
else if(granada.isDone()){
setSize(300,300);
g.drawImage(elevator.getImage(), 0, 0,
elevator.getImage().getWidth(null), elevator.getImage().getHeight(null),null);
g.drawImage(jake.getImage(), jake.getPosX(), jake.getPosY(),
jake.getImage().getWidth(null), jake.getImage().getHeight(null),null);
g.drawImage(caitlan.getImage(), caitlan.getPosX(), caitlan.getPosY(),
caitlan.getImage().getWidth(null), caitlan.getImage().getHeight(null), null);
}
}
public void keyPressed(KeyEvent e) { //Method called whenever a key is pressed
caitlan.printLocation();
int code = e.getKeyCode();
if(code == KeyEvent.VK_RIGHT){ //When the user presses the right arrow
caitlan.setLocation(caitlan.getPosX() + 10, caitlan.getPosY());
repaint();
}
if(code == KeyEvent.VK_LEFT){ //When the user presses the left arrow
caitlan.setLocation(caitlan.getPosX() - 10, caitlan.getPosY());
repaint();
}
if(code == KeyEvent.VK_UP){ //Implement jumping code here
repaint();
}
}
public boolean checkIfLevelDone(Level l, Person p){
//Method to check if a given level is done
if(l.getCompletionPoint().x <= p.getPosX()){
return true;
}
return false;
}
public void followOtherPerson(Person a, Person b) throws InterruptedException{
//Method to follow the other character
}
public void jump(Person p) throws InterruptedException{ //Jump method
caitlan.setLocation(caitlan.getPosX(), caitlan.getPosY() - 60);
repaint();
}
public void keyReleased(KeyEvent e) { //IGNORE
}
public void keyTyped(KeyEvent e) { //IGNORE
}
public void actionPerformed(ActionEvent e) { //IGNORE
}
}
Here is the Level class:
import java.awt.Image;
import java.awt.Point;
public class Level {
//Private attributes
private Image backgroundImage; //Image
private String levelName; //Name of level
private boolean levelComplete; //Status that holds if the level is done
private Point completionPoint; //Point at which the level is completed
public Level(Image i, String l, Point p){ //Constructor
this.backgroundImage = i;
this.levelName = l;
this.completionPoint = p;
levelComplete = false;
}
public void setDone(){
levelComplete = true;
}
public boolean isDone(){ //Returns if the level is done or not
return levelComplete;
}
public Image getImage(){ //Getters and setters
return backgroundImage;
}
public String getLevelName(){
return levelName;
}
public void setImage(Image i){
this.backgroundImage = i;
}
public void setName(String name){
this.levelName = name;
}
public Point getCompletionPoint(){
return completionPoint;
}
public void setCompletionPoint(Point p){
this.completionPoint = p;
}
}
And finally, here is the Person class.
import java.awt.*;
public class Person{
//Attributes
private Image image;
private String name;
private int xlocation;
private int ylocation;
/*Boolean that you can publically access to determine if the given Person object is a
player*/
public boolean isPlayer;
public Person(Image i, String s){ //Really shouldn't be used
this.image = i;
this.name = s;
}
public Person(Image i){ //Really shouldn't be used
this.image = i;
}
public Person(Image i, String s, int xloc, int yloc){ //Basic constructor
this.name = s;
this.image = i;
this.xlocation = xloc;
this.ylocation = yloc;
}
public Person(Image i, String s, int xloc, int yloc, boolean isPlayer){ //Detailed constructor
this.name = s;
this.image = i;
this.xlocation = xloc;
this.ylocation = yloc;
this.isPlayer = isPlayer;
}
public void printLocation(){
System.out.println("Person " + name + " is at location (" + xlocation +", " + ylocation +").");
}
public void incrementPositionX(){ //Increase xlocation by 1
xlocation++;
}
public void incrementPositionY(){ //Increase ylocation by 1
ylocation++;
}
public void setName(String name){ //Method to change the name of a sprite
this.name = name;
}
public void setImage(Image image){ //Method to change the image of a sprite
this.image = image;
}
public Image getImage(){ //Get image
return image;
}
public int getPosX(){ //Get x position
return xlocation;
}
public int getPosY(){ //Get y position
return ylocation;
}
public String getName(){ //Get name
return name;
}
public void setLocation(int x, int y){ //Method to change the location of a sprite
this.xlocation = x;
this.ylocation = y;
}
}
You should not paint directly on top level container such as JFrame. Instead, use JComponent or JPanel. Override paintComponent() for painting rather than paint() and don't forget to call super.paintComponent(g)
Take a look at Performing Custom Painting tutorial for more information.
Also, key listener is a lower level interface. It is best to use Key Bindings instead. See How to Use Key Bindings for details and examples.
Also, there is no need to create ImageIcon to get Image. Look into ImageIO.read() methods.