I am not great at programming but I am trying to learn, I have been following a tutorial on youtube and the youtuber uses 4 shades of monochromatic colours but I find it difficult to make sprites with that limitation so I am trying to up it to 8 shades but when I try and change it it just loops the first 4 colours.
Below is the colour class, screen class and tile class. I can upload any other classes ye need.
package com.alan.game.level.tiles;
import com.alan.game.gfx.Colours;
import com.alan.game.gfx.Screen;
import com.alan.game.level.Level;
public abstract class Tile {
public static final Tile[] tiles = new Tile[256];
public static final Tile VOID = new BasicTile(0, 0, 0, Colours.get(000, -1, -1, -1, -1, -1, -1, -1));
public static final Tile STONE = new BasicTile(1, 1, 0, Colours.get(000, 002, 003, 004, 000, 000, 000, 000));
public static final Tile GRASS = new BasicTile(2, 2, 0, Colours.get(-1, 131, 141, -1, -1, -1,- 1, -1));
protected byte id;
protected boolean solid;
protected boolean emitter;
public Tile(int id, boolean isSolid, boolean isEmitter){
this.id = (byte) id;
if(tiles[id] != null) throw new RuntimeException("Duplicate tile id on " + id);
this.solid = isSolid;
this.emitter = isEmitter;
tiles[id] = this;
}
public byte getId(){
return id;
}
public boolean isSolid(){
return solid;
}
public boolean isEmitter(){
return emitter;
}
public abstract void render(Screen screen, Level level, int x, int y);
}
package com.alan.game.gfx;
public class Screen {
public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
public int[] pixels;
public int xOffset = 0;
public int yOffset = 0;
public int width;
public int height;
public SpriteSheet sheet;
public Screen(int width, int height, SpriteSheet sheet){
this.width = width;
this.height = height;
this.sheet = sheet;
for(int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++){
pixels = new int[width * height];
}
}
public void setOffset(int xOffset, int yOffset){
this.xOffset = xOffset;
this.yOffset = yOffset;
}
public void render(int xPos, int yPos, int tileIndex, int colour){
render(xPos, yPos, tileIndex, colour, false, false);
}
public void render(int xPos, int yPos, int tileIndex, int colour, boolean mirrorX, boolean mirrorY){
xPos -= xOffset;
yPos -= yOffset;
int xTile = tileIndex % 32;
int yTile = tileIndex / 32;
int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
for(int y = 0; y < 8; y++){
int ySheet = y;
if(mirrorY) ySheet = 7 - y;
if(y + yPos < 0 || y + yPos >= height) continue;
for(int x = 0; x < 8; x++){
int xSheet = x;
if(mirrorX) xSheet = 7 - x;
if(x + xPos < 0 || x + xPos >= width) continue;
int col = (colour >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) & 255;
if(col < 255) pixels[(x + xPos) + (y + yPos) * width] = col;
}
}
}
}
package com.alan.game.gfx;
public class Colours {
public static int get(int colour1, int colour2, int colour3, int colour4, int colour5, int colour6, int colour7, int colour8){
return (get(colour8) << 56) + (get(colour7) << 48) + (get(colour6) << 40) + (get(colour5) << 32) + (get(colour4) << 24) + (get(colour3) << 16) + (get(colour2) << 8) + get(colour1);
}
private static int get(int colour){
if(colour < 0) return 255;
int r = colour / 100 % 10;
int g = colour / 10 % 10;
int b = colour % 10;
return r * 36 + g * 6 + b;
}
}
I ran into the same problem. First thing I already think is a problem is that we're bitshifting 56 on colour 8. An integer can contain a maximum of 32 bits. So by shifting it 56, we're completely getting rid of any data that we had there (if I'm correct).
So I tried changing it to return a long, but that doesn't solve anything either. I know this is not a real answer, but I hope this regains some attention, because I'm stuck on this now too!
Related
I implemented Gaussian Blur in Java, it seems to work on smaller radiuses but not on bigger ones. I'm not sure why on bigger radiuses the image darkens, I followed the same formula and the steps to apply the blur. Generating the blur matrix and applying it on the original image, and setting the pixel value to be the sum of the result of multiplying the image matrix with the blur matrix. I added the code I wrote below:
public class GaussianBlur extends ImageFilter {
private int radius;
private double sigma;
public GaussianBlur(String imageFilename, int radius) {
super(imageFilename);
this.radius = radius;
this.sigma = ((2.0 * radius) + 1.0) / 2.0;
}
#Override
public void applyFilter() throws IOException {
init();
Matrix<Double> gaussianMatrix = getGaussianMatrix();
Matrix<Color> imageMatrix, weightedImageMatrix;
Color weightedPixelSum;
for(int i = 0; i < getWidth(); i++) {
for(int j = 0; j < getHeight(); j++) {
imageMatrix = getImageMatrix(i, j);
weightedImageMatrix = multiplyImageMatrixWithWeight(imageMatrix, gaussianMatrix);
weightedPixelSum = getWeightedGaussianBlurValue(weightedImageMatrix);
getFilter().setRGB(i, j, weightedPixelSum.getRGB());
}
}
}
private Matrix<Double> getGaussianMatrix() {
Matrix<Double> gaussianMatrix = new Matrix<>(Double.class, radius);
double weightedSum = 0.0;
int matrixI = 0, matrixJ;
double gaussianValue;
for(int i = -radius; i <= radius; i++) {
matrixJ = 0;
for(int j = -radius; j <= radius; j++) {
gaussianValue = getGaussianValue(i, j);
weightedSum += gaussianValue;
gaussianMatrix.setValue(matrixI, matrixJ, gaussianValue);
matrixJ++;
}
matrixI++;
}
for(int i = 0; i < gaussianMatrix.getMatrix().length; i++) {
for(int j = 0; j < gaussianMatrix.getMatrix()[i].length; j++) {
gaussianMatrix.setValue(i, j, gaussianMatrix.getValue(i, j) / weightedSum);
}
}
return gaussianMatrix;
}
private double getGaussianValue(int x, int y) {
return 1.0 / (2.0 * Math.PI * sigma * sigma) * Math.pow(Math.E, -((x * x) + (y * y)) / (2.0 * (sigma * sigma)));
}
private Color getWeightedGaussianBlurValue(Matrix<Color> weightedImageMatrix) {
int r = 0, g = 0, b = 0;
for(int i = 0; i < weightedImageMatrix.getMatrix().length; i++) {
for(int j = 0; j < weightedImageMatrix.getMatrix()[i].length; j++) {
if(weightedImageMatrix.getValue(i, j) != null) {
r += weightedImageMatrix.getValue(i, j).getRed();
g += weightedImageMatrix.getValue(i, j).getGreen();
b += weightedImageMatrix.getValue(i, j).getBlue();
}
}
}
return new Color(r, g, b);
}
/*
* Multiply each image pixel with its matrix value to get a new matrix with the weighted pixel values.
*/
private Matrix<Color> multiplyImageMatrixWithWeight(Matrix<Color> imageMatrix, Matrix<Double> gaussianMatrix) {
Matrix<Color> weightedImageMatrix = new Matrix<>(Color.class, this.radius);
Color weightedValue;
for(int i = 0; i < weightedImageMatrix.getMatrix().length; i++) {
for(int j = 0; j < weightedImageMatrix.getMatrix()[i].length; j++) {
if(imageMatrix.getValue(i, j) != null) {
weightedValue = new Color(
(int) ((double) imageMatrix.getValue(i, j).getRed() * gaussianMatrix.getValue(i, j)),
(int) ((double) imageMatrix.getValue(i, j).getGreen() * gaussianMatrix.getValue(i, j)),
(int) ((double) imageMatrix.getValue(i, j).getBlue() * gaussianMatrix.getValue(i, j))
);
weightedImageMatrix.setValue(i, j, weightedValue);
} else {
weightedImageMatrix.setValue(i, j, null);
}
}
}
return weightedImageMatrix;
}
/*
* Given the center points (i, j), construct a matrix from the image to blur.
*/
private Matrix<Color> getImageMatrix(int i, int j) {
Matrix<Color> imageMatrix = new Matrix<>(Color.class, radius);
int matrixI = 0, matrixJ;
for(int x = i - radius; x <= i + radius; x++) {
matrixJ = 0;
for(int y = j - radius; y <= j + radius; y++) {
if(x > -1 && y > -1 && x < getOriginal().getWidth() && y < getOriginal().getHeight()) {
imageMatrix.setValue(matrixI, matrixJ, new Color(getOriginal().getRGB(x, y)));
} else {
imageMatrix.setValue(matrixI, matrixJ, null);
}
matrixJ++;
}
matrixI++;
}
return imageMatrix;
}
private class Color {
private int r, g, b;
public Color(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public Color(int rgb) {
this((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff);
}
public int getRed() {
return r;
}
public int getGreen() {
return g;
}
public int getBlue() {
return b;
}
public int getRGB() {
return (r << 16) | (g << 8) | b;
}
#Override
public String toString() {
return "(" + r + "," + g + "," + b + ")";
}
}
private class Matrix<T> {
private T[][] matrix;
public Matrix(Class<T> clazz, int radius) {
int length = (2 * radius) + 1;
matrix = (T[][]) Array.newInstance(clazz, length, length);
}
public T getValue(int i, int j) {
return matrix[i][j];
}
public void setValue(int i, int j, T value) {
matrix[i][j] = value;
}
public T[][] getMatrix() {
return matrix;
}
}
}
The class ImageFilter is just an abstract class with two instances of BufferedImage (one for the original image and one for the blurred image), and the displayImage function just displays the image in a message dialog.
The main method using this class is
public static void main(String[] args) throws IOException {
String filename = "res" + File.separator + "TajMahal.jpeg";
GaussianBlur gaussianBlur = new GaussianBlur(filename, 2);
gaussianBlur.applyFilter();
gaussianBlur.displayImage();
}
And below are the resulting images
The original image:
Blurred with radius 2:
Blurred with radius 7:
How come blurring it with radius 7 is darkening the image? Is there something missing in the formula or something that I have missed?
I've put all the black pixels of the image in an array and I want them to get the color of their left neighbor. I run the code without errors but the result is not really what I'm expecting.
Where those black stripes comes form? I was expecting it to be all red.
Here's my code and results.
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.*;
public class ImageTest {
public static BufferedImage Threshold(BufferedImage img) {
int height = img.getHeight();
int width = img.getWidth();
BufferedImage finalImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r = 0;
int g = 0;
int b = 0;
List<Integer> blackpixels = new ArrayList<Integer>();
for (int x = 0; x < width; x++) {
try {
for (int y = 0; y < height; y++) {
//Get RGB values of pixels
int rgb = img.getRGB(x, y);
r = ImageTest.getRed(rgb);
g = ImageTest.getGreen(rgb);
b = ImageTest.getBlue(rgb);
int leftLoc = (x-1) + y*width;
if ((r < 5) && (g < 5) && (b < 5)) {
blackpixels.add(rgb);
Integer[] simpleArray = new Integer[ blackpixels.size() ];
System.out.print(simpleArray.length);
int pix = 0;
while(pix < simpleArray.length) {
r = leftLoc;
pix = pix +1;
}
}
finalImage.setRGB(x,y,ImageTest.mixColor(r, g,b));
}
}
catch (Exception e) {
e.getMessage();
}
}
return finalImage;
}
private static int mixColor(int red, int g, int b) {
return red<<16|g<<8|b;
}
public static int getRed(int rgb) {
return (rgb & 0x00ff0000) >> 16;
}
public static int getGreen(int rgb) {
return (rgb & 0x0000ff00) >> 8;
}
public static int getBlue(int rgb) {
return (rgb & 0x000000ff) >> 0;
}
}
The following might work.
The main change is that it first collects the locations of ALL dark pixels, then goes over them to assign the colour from their left neighbours.
import java.awt.image.BufferedImage;
import java.util.*;
public class BlackRedImage
{
public static BufferedImage Threshold( BufferedImage img )
{
int height = img.getHeight();
int width = img.getWidth();
BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
List<Integer> blackpixels = new ArrayList<Integer>();
for ( int x = 0; x < width; x++ )
{
for ( int y = 0; y < height; y++ )
{
int rgb = img.getRGB(x, y); // Get the pixel in question
int r = BlackRedImage.getRed(rgb);
int g = BlackRedImage.getGreen(rgb);
int b = BlackRedImage.getBlue(rgb);
if ( (r < 5) && (g < 5) && (b < 5) )
{ // record location of any "black" pixels found
blackpixels.add(x + (y * width));
}
finalImage.setRGB(x, y, rgb);
}
}
// Now loop through all "black" pixels, setting them to the colour found to their left
for ( int blackPixelLocation: blackpixels )
{
if ( blackPixelLocation % width == 0 )
{ // these pixels are on the left most edge, therefore they do not have a left neighbour!
continue;
}
int y = blackPixelLocation / width;
int x = blackPixelLocation - (width * y);
int rgb = img.getRGB(x - 1, y); // Get the pixel to the left of the "black" pixel
System.out.println("x = " + x + ", y = " + y + ", rgb = " + rgb);
finalImage.setRGB(x, y, rgb);
}
return finalImage;
}
private static int mixColor( int red, int g, int b )
{
return red << 16 | g << 8 | b;
}
public static int getRed( int rgb )
{
return (rgb & 0x00ff0000) >> 16;
}
public static int getGreen( int rgb )
{
return (rgb & 0x0000ff00) >> 8;
}
public static int getBlue( int rgb )
{
return (rgb & 0x000000ff) >> 0;
}
}
EDIT: Here is a simpler version (doesn't collect the black pixels)
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.*;
public class ColourMove
{
public static BufferedImage Threshold( BufferedImage img )
{
int width = img.getWidth();
int height = img.getHeight();
BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for ( int x = 1; x < width; x++ ) // Start at 1 as the left most edge doesn't have a left neighbour
{
for ( int y = 0; y < height; y++ )
{
Color colour = new Color(img.getRGB(x, y));
int red = colour.getRed();
int green = colour.getGreen();
int blue = colour.getBlue();
if ( (red < 5) && (green < 5) && (blue < 5) )
{ // Encountered a "black" pixel, now replace it with it's left neighbour
finalImage.setRGB(x, y, img.getRGB(x - 1, y));
}
else
{ // Non-black pixel
finalImage.setRGB(x, y, colour.getRGB());
}
}
}
return finalImage;
}
}
I have a small game project where I have to use RMI to create a multiplayer game. I'm getting the following error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalAccessError: tried to
access class PlayArea from class com.sun.proxy.$Proxy1
at com.sun.proxy.$Proxy1.getPlayArea(Unknown Source)
at GameClient.getPlayArea(GameClient.java:100)
at PlayBoard.paintComponent(GUI.java:264)
Now those references are as follows:
Part of GameClient.java
public PlayArea getPlayArea() {
PlayArea result = null;
try {
result = myServer.getPlayArea(clientID); // line 100 of GameClient.java
} catch (Exception e) {
System.out.println("Error: " + e);
System.exit(1);
}
return result;
}
And this is part of GUI.java
public void paintComponent(Graphics g) {
PlayArea playArea = gui.getGameEngine().getPlayArea(); // line 264 of GUI.java
super.paintComponent(g);
LinkedList bricks = new LinkedList();
int pixCoeff = brickSize - 1;
for (int y = 0; y < bricksPerSide; y++) {
int pixY = y * pixCoeff;
for (int x = 0; x < bricksPerSide; x++) {
int brick = playArea.getBrick(x, y);
if (Colour.isBase(brick))
continue;
if (gui.displayMode == GUI.NOMARKED && Colour.isMarked(brick)) {
continue;
}
int pixX = x * pixCoeff;
bricks.add(new BoardBrick(pixX, pixY,
gui.displayMode == GUI.MARKEDASORIGINAL ? ColourMapper
.map(Colour.original(brick)) : (Colour
.isMarked(brick) ? Color.WHITE : ColourMapper
.map(brick))));
}
}
g.setColor(ColourMapper.map(Colour.BASE));
g.fillRect(0, 0, (brickSize - 1) * bricksPerSide + 1, (brickSize - 1)
* bricksPerSide + 1);
while (bricks.size() > 0) {
BoardBrick brick = (BoardBrick) (bricks.removeFirst());
brick.drawBrick(g);
}
if (gui.getGameEngine().isGameOver())
gameOver(g);
}
And this is PlayArea.java
import java.util.Random;
class PlayArea implements java.io.Serializable {
public static final int NORTHSIDE = 0;
public static final int SOUTHSIDE = 1;
public static final int EASTSIDE = 2;
public static final int WESTSIDE = 3;
private int[][] grid;
public PlayArea(int size) {
grid = new int[size][size];
}
public static int randomSide(Random rng) {
return rng.nextInt(4);
}
public int getSize() {
return grid.length;
}
public int getBrick(int x, int y) {
return grid[x][y];
}
public int getBrick(Coord c) {
return getBrick(c.getX(), c.getY());
}
public void setBrick(int x, int y, int val) {
grid[x][y] = val;
}
public void setBrick(Coord c, int val) {
setBrick(c.getX(), c.getY(), val);
}
// --> play_area_init_collision_flipDraw
public void init() {
int size = getSize();
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
if (x == 0 || y == 0 || x == size - 1 || y == size - 1)
setBrick(x, y, Colour.WALL);
else
setBrick(x, y, Colour.BASE);
setBrick(size / 2, size / 2, Colour.WALL);
setBrick(size / 2 - 2, size / 2 - 2, Colour.WALL);
setBrick(size / 2 - 2, size / 2 + 2, Colour.WALL);
setBrick(size / 2 + 2, size / 2 - 2, Colour.WALL);
setBrick(size / 2 + 2, size / 2 + 2, Colour.WALL);
}
public boolean collision(PlayingBlock block) {
int topLeftX = block.getPosition().getX();
int topLeftY = block.getPosition().getY();
int height = block.getHeight();
int length = block.getLength();
for (int x = 0; x < length; x++)
for (int y = 0; y < height; y++)
if (!Colour.isBase(getBrick(topLeftX + x, topLeftY + y)))
return true;
return false;
}
public void flipDraw(PlayingBlock block) {
int topLeftX = block.getPosition().getX();
int topLeftY = block.getPosition().getY();
int height = block.getHeight();
int length = block.getLength();
for (int x = 0; x < length; x++)
for (int y = 0; y < height; y++)
setBrick(topLeftX + x, topLeftY + y, getBrick(topLeftX + x,
topLeftY + y)
^ block.getColour());
}
// --<
// --> play_area_getSquareColour_mark
public int getSquareColour(int topLeftX, int topLeftY, int size) {
// return 0 if no square found
// square colour otherwise
if (topLeftX + size > getSize() || topLeftY + size > getSize())
return 0;
int colour = getBrick(topLeftX, topLeftY);
if (!Colour.isPlayBrick(colour))
return 0;
for (int x = topLeftX; x < topLeftX + size; x++)
for (int y = topLeftY; y < topLeftY + size; y++)
if (!Colour.isSameColour(colour, getBrick(x, y)))
return 0;
return colour;
}
public void mark(int topLeftX, int topLeftY, int size) {
for (int x = topLeftX; x < topLeftX + size; x++)
for (int y = topLeftY; y < topLeftY + size; y++)
setBrick(x, y, Colour.mark(getBrick(x, y)));
}
// --<
}
Now, what's weird is, this runs in Windows using Java 1.6, but on OS X it doesn't and I don't have access to a Windows machine right now. I'm using 1.6.0_65 on OS X 10.9.
I have found many errors on the web regarding AWT-EventQueue-0 but most of them are NullPointerException errors, not IllegalAccessError. I would appreciate any help, I'm quite new to Java.
UPDATE: The code most definitely works in 1.5 and 1.6. This is most likely related to the included libraries. I will keep looking and edit this post accordingly.
Aloha,
i have trouble finding the error in my java code. In my opinion everything is fine and correct but the function is not executed correctly and I dont understand why. The function should detect the difference between the colors and calculate the arithmetic mean of them.
The resilt of it should be draw under the original picture. What did I miss, please help me?
package edge;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import javax.swing.JComponent;
import javax.swing.JFrame;
/**
*
* #author Alaska
*/
public class Edge extends JComponent {
final int W = 500;
final int H = 300;
Image m_TrgImg, m_SrcImg;
public Edge(JFrame father) {
try {
FileDialog diag = new FileDialog(father);
diag.setVisible(true);
m_SrcImg = getToolkit().getImage(diag.getDirectory() + diag.getFile()).getScaledInstance(W, H, Image.SCALE_SMOOTH);
MediaTracker mt = new MediaTracker(this);
mt.addImage(m_SrcImg, 0);
mt.waitForAll();
int[] srcPix = new int[W * H];
int[] trgPix = new int[W * H];
PixelGrabber grab = new PixelGrabber(m_SrcImg, 0, 0, W, H, srcPix, 0, W);
grab.getPixels();
MemoryImageSource imgProd = new MemoryImageSource(W, H, trgPix, 0, W);
m_TrgImg = createImage(imgProd);
detectEdges(srcPix, trgPix);
m_TrgImg.flush();
} catch (InterruptedException e) {
System.out.println(e);
}
}
#Override
public void paintComponent(Graphics g) {
g.drawImage(m_SrcImg, 0, 0, this);
g.drawImage(m_TrgImg, 0, H, this);
}
#Override
public Dimension getPreferredSize() {
return getMinimumSize();
}
#Override
public Dimension getMinimumSize() {
return new Dimension(W, H * 2);
}
private void detectEdges(int[] srcPix, int[] trgPix) {
for (int x = 0; x < W; ++x) {
for (int y = 0; y < H; ++y) {
trgPix[y * W + x] = compColor(srcPix, x, y);
}
}
}
private int getRed(int col) {
return (col >> 16) & 255;
}
private int getGreen(int col) {
return (col >> 8) & 255;
}
private int getBlue(int col) {
return col & 255;
}
private int compColor(int[] srcPix, int x, int y) {
int red = 0;
int green = 0;
int blue = 0;
int cnt = 0;
final int IDX = y * W + x;
final int RED = getRed(srcPix[IDX]);
final int GREEN = getGreen(srcPix[IDX]);
final int BLUE = getBlue(srcPix[IDX]);
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (dx != 0 || dy != 0) {
final int X = x + dx;
final int Y = y + dy;
final int LOCAL_IDX = Y * W + X;
if (0 <= X && X < W && 0 <= Y && Y < H) {
++cnt;
red += Math.abs(RED - getRed(srcPix[LOCAL_IDX]));
green += Math.abs(GREEN - getGreen(srcPix[LOCAL_IDX]));
blue += Math.abs(BLUE - getBlue(srcPix[LOCAL_IDX]));
}
}
}
}
return 0xff000000 | (255 - (red / cnt) << 16) | (255 - (green / cnt) << 8) | (255 - (blue / cnt));
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.getContentPane().add(new Edge(f));
f.pack();
f.setVisible(true);
}
}
You need to grab.grabPixels(), not grab.getPixels().
http://docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html
Also what trashgod said about Initial Threads. You need to create your GUI with SwingUtilities.invokeLater().
Edit
The method is executed correctly but you are getting all black values on the input because your pixel array contains only the initialized values which is 0.
I recently took all my code a manually imported it into an eclipse project from BlueJ, I then got use to the settings up "Run Configurations", finally thinking I was home free. Then I ran the code, and I got this error
java.lang.NoSuchMethodError: main
Exception in thread "main"
so I figured I had to add a main method (I never had to do this in BlueJ, why?). So I did that just called the constructor method (in BlueJ I would just create a new object and the JFrame would show). So I did that, same error. After trying different things (such as moving the code in the constructor to a different method etc.). I just put this in for the main method:
public void main(String[] args)
{
System.out.println("Hello, this is main why won't Java find this.");
}
After that I still got the same error, so I then decided to add it to all my classes to make sure it wasn't using another class as the main class. Still same error, so I come to you wondering if any of you have encountered this problem. Also I did search Google and all I found was problems with private classes etc and sense all my classes are public (hey I come from Python :) ). I knew that wasn't the problem. Help Please :)
Picture of my Run Configuration
This is my main method code
WARNING: LONG
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class AppFrame extends JFrame
{
public String status = "Status:";// Status of Applet
public int paint_count = 1;// Number of times applet has been painted
public int[] mousePos = {-1, -1};// Stores Mouse's Last Clicked X and Y Cordinates
public int[] boardPos = {-1, -1};//Stores The Board's X and Y Cordinates
public int[] selectedSquarePos = {-1, -1};
public int[] boardSquare = {-1, -1};//Stores Last Clicked Square
public Color P1_Color = Color.GRAY;
public Color P2_Color = Color.WHITE;
public Color SquareEven = Color.BLACK;
public Color SquareOdd = Color.RED;// pieces move on this one
public int boardHeight = 400;
public int boardWidth = 400;
public boolean pieceSelected = false;
public boolean CheckersPiece = false;
public Board CheckersBoard = new Board();
public Image buffer = null;
public Graphics bg = null;
public void main(String[] args)
{
System.out.println("Hello, this is main why won't Java find this.");
}
public AppFrame()
{
super("JCheckers");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,900);
setVisible(true);
buffer = createImage(getWidth(), getHeight());
boardHeight = getHeight() - 40; // 20 pixel border at top and bottom and 20 pixels for blue bar
boardWidth = getWidth() - 40; // 20 pixel border at left and right
bg = buffer.getGraphics();
addMouseListener(new MouseAdapter()
{
public void mouseClicked (MouseEvent e)
{
handleClick(e);
}
}
);
}
public void handleClick(MouseEvent e)
{
/* Handles tracking of mouse clicks; DOES NOT HANDLE DISPLAY, it just updates the data and calls redraw */
mousePos[0] = e.getX();
mousePos[1] = e.getY();
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
render(g);
}
public void render(Graphics g)
{
bg.clearRect(0,0, getWidth(), getHeight());
//Draw Chess Board and Pieces
renderChessBoard(bg, 20, 20);
// Display Info
System.out.println(status);
System.out.println(String.format("Last Mouse Click # (X:%d Y:%d)", mousePos[0], mousePos[1]) );
System.out.println("Paint #" + paint_count );
System.out.println(String.format("Board Square (x:%s, y:%s) %b", boardSquare[0], boardSquare[1], CheckersPiece) );
System.out.println(CheckersBoard.status );
paint_count += 1;
// Draw Image to Screen
g.drawImage(buffer, 0, 25, null);// so it does not get hidden by the blue close/min/max room
}
public boolean isValidSquare(int col, int row)
{
if (col > -1 & col < 8) {return true;}
return false;
}
public void renderChessBoard(Graphics g, int x, int y)
{
/* Renders board and pieces*/
// sense the row squares are well squares then the
// board will be a square also; so we draw it with whatever
// side is smallest, width or height
boardPos[0] = x;
boardPos[1] = y;
drawBoard(g, x, y, boardWidth, boardHeight);
boardSquare = getBoardSquare(mousePos[0], mousePos[1]);
CheckersPiece = isCheckersPiece(boardSquare[0], boardSquare[1]);
boolean validSquare = isValidSquare(boardSquare[0], boardSquare[1]);
if (validSquare)
{
if (CheckersPiece)
{
selectSquare(g, boardSquare[0], boardSquare[1]);
}
else
{
if (pieceSelected)
{
int selectedCol = selectedSquarePos[0];
int selectedRow = selectedSquarePos[1];
int toCol = boardSquare[0];
int toRow = boardSquare[1];
System.out.println(selectedCol + " " + selectedRow + " " + toCol + " " + toRow);
if (!CheckersBoard.move(selectedSquarePos, boardSquare)) // not a valid move;
{
pieceSelected = false;
}
}
}
}
parseBoard(CheckersBoard.board, g);
}
public void drawBoard(Graphics g, int Bx, int By, int Bw, int Bh)
{
int numberRowsDrawed = 0;
int rH = Bh / 8;
int rW = Bw; // Row width is the same as the Board's width because the board and the row share the same sides
while (numberRowsDrawed < 8)
{
int rY = (numberRowsDrawed * rH) + By;
// Row X is the same as the Board X because the board and the row share the same sides
int rX = Bx;
Color EVEN = SquareEven;
Color ODD = SquareOdd;
// Yes Yes The EVEN Color is now odd and vica versa its because rows only now there row counts and so they start at 0 and don't
// include the rows above
if ((numberRowsDrawed % 2) != 0) {EVEN = SquareOdd; ODD = SquareEven;}
drawRow(g, rX, rY, rW, rH, EVEN, ODD);
numberRowsDrawed +=1;
}
}
public void drawRow(Graphics g, int x, int y, int width, int height, Color EVEN, Color ODD)
{
System.out.println("Board Y: " + y);
int squareW = width / 8;
int squareH = height;
int numberSquaresCreated = 0;
while (numberSquaresCreated < 8)
{
// needs a special case because Java's modulo uses division (so it would give a divide by 0 error) STUPID JAVA!!!!!!
if (numberSquaresCreated == 0)
{
g.setColor(EVEN);
g.fillRect(x, y, squareW, squareH);
}
else
{
if (numberSquaresCreated % 2 == 0){g.setColor(EVEN);}
else {g.setColor(ODD);}
int sX = x + (squareW * numberSquaresCreated);
g.fillRect(sX, y, squareW, squareH);
}
numberSquaresCreated +=1;
}
}
public void drawMan(Graphics g, int boardRow, int boardCol, Color pieceColor)
{
int x = boardPos[0];
int y = boardPos[1];
int pixelPosX = x + ((boardWidth / 8) * boardRow);
int pixelPosY = y + ((boardHeight / 8) * boardCol);
g.setColor(pieceColor);
g.fillOval(pixelPosX + 13, pixelPosY + 13, (boardWidth / 8) - 26, (boardHeight / 8) - 26);
}
public void drawKing(Graphics g, int boardRow, int boardCol, Color pieceColor, Color crownColor)
{
drawMan(g, boardRow, boardCol, pieceColor);
g.setColor(crownColor);
int x = boardPos[0];
int y = boardPos[1];
double DsizeFactor = ( (float) boardHeight / 8.0) / 3.75;
int sizeFactor = (int) DsizeFactor;
int pixelPosX = x + ((boardWidth / 8) - sizeFactor) / 2 + ((boardWidth / 8) * boardRow);
int pixelPosY = y + ((boardHeight / 8) - sizeFactor) / 2 + ((boardHeight / 8) * boardCol);
int[] xPoints = {pixelPosX, pixelPosX, pixelPosX + sizeFactor, pixelPosX + sizeFactor, pixelPosX + ((sizeFactor * 3) / 4), pixelPosX + (sizeFactor / 2), pixelPosX + (sizeFactor / 4) };
int[] yPoints = {pixelPosY, pixelPosY + sizeFactor, pixelPosY + sizeFactor, pixelPosY, pixelPosY + (sizeFactor / 2), pixelPosY, pixelPosY + (sizeFactor / 2)};
g.fillPolygon(xPoints, yPoints, 7);
}
public void selectSquare(Graphics g, int bSX, int bSY)
{
g.setColor(Color.YELLOW);
/*+10 is to offset text (the xy cordinates are the bottom left side of the text NOT top left.*/
pieceSelected = true;
int squareX = boardPos[0] + (boardWidth / 8) * bSX;
int squareY = 10 + boardPos[1] + (boardHeight / 8) * bSY;
selectedSquarePos[0] = bSX;
selectedSquarePos[1] = bSY;
g.drawString("Selected", squareX, squareY);
}
// Data Handling and Retreiving methods
public void parseBoard(String[][] Board, Graphics g)
{
int row = 0;
int col = 0;
for (String[] rowOfPieces : Board)
{
for (String piece : rowOfPieces)
{
if (piece != "X")
{
Color PIECE_COLOR = P1_Color;
if (piece.contains("P2")) {PIECE_COLOR = P2_Color;}
if (piece.contains("C"))
{
drawMan(g, col, row, PIECE_COLOR);
}
if (piece.contains("K"))
{
Color Crown_Color = P2_Color;
if (PIECE_COLOR != P1_Color) {Crown_Color = P1_Color;}
drawKing(g, col, row, PIECE_COLOR, Crown_Color);
}
}
col+=1;
}
row +=1;
col = 0;
}
}
public int[] getBoardSquare(int x, int y)
{
//row or col = boardpos - offset / row height or width
if ((x < boardPos[0]) | (y < boardPos[1]) | (x > (boardPos[0] + boardWidth)) | (y > (boardPos[1] + boardHeight)) )
{
int[] BS = {-1, -1};
return BS;
}
int col = (x - boardPos[0]) / (boardWidth / 8);
int row = (y - boardPos[1]) / (boardHeight / 8);
int[] BS = {col, row};
return BS;
}
public boolean isCheckersPiece(int BoardSquareX, int BoardSquareY)
{
int Px = BoardSquareX;
int Py = BoardSquareY;
if (Px == -1 & Py == -1)
{
return false;
}
String Square = CheckersBoard.board[Py][Px];
return Square != "X";
}
}
You forgot static:
public static void main(String[] args)
But in order to really start your application, you should launch it from that method, not merely have it. Here is how to start it:
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
AppFrame frame = new AppFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
It's important to note the EXIT_ON_CLOSE - this will make the JVM exit when you press the X button. Otherwise your application will continue running, and you might even not notice.
Make main static.