I am trying to export my sketch to pdf. The issue that I have is that for some reason only a portion of my sketch is exported to pdf, as if the original sketch was cropped! When I run my sketch, 64 lines show up (as intended) and indeed when I save it to png all the 64 lines are there and sketch looks just the same as when I run it.
When I export the sketch to pdf though, only 16 line show up, as if the pdf was cropping my sketch and only the cropped portion was being exported.
This is the png showing what the sketch is supposed to look like:
This is what the pdf has exported:
And this is my code:
import processing.pdf.*;
import java.util.Random;
int cols, rows;
int videoScale = 100;
boolean recordPDF = false;
void setup() {
size(800,800);
pixelDensity(2);
frameRate(0.5);
cols = width/videoScale;
rows = height/videoScale;
}
void draw() {
if (recordPDF) {
beginRecord(PDF, "pdfs/" + str(random(1000)) + ".pdf");
}
background(255);
strokeWeight(1.5);
drawLines();
if (recordPDF) {
endRecord();
recordPDF = false;
println("Printed pdf.");
}
}
void keyPressed() {
if (key == 'p') {
recordPDF = true;
}
if (key == 's') {
saveFrame("img.png");
}
}
void drawLines() {
// Begin loop for columns
for (int i = 0; i < cols; i++) {
// Begin loop for rows
for (int j = 0; j < rows; j++) {
int x = i*videoScale;
int y = j*videoScale;
line(x,y,x+30,y+30);
}
}
}
I have looked into the relevant documentation on PDF exports but could not find a solution to this. Any help would be greatly appreciated!
Remove pixelDensity(2) from the setup() to fix. PixelDensity of 2 was designed to allow retina monitors to use all of the pixels. If you must use it, then you would need to write a separate drawLines() for the pdf file (example follows). Note that for the pdf drawLines() the videoScale is cut in half and the second set of x,y coordinates for each line is +15 instead of +30. You will also have to change the path for each file saved to be correct for your system. I used a different method for creating a pdf from pGraphics, which should be irrelevant.
/*
If pixelDensity(2) is used, need to modify second drawLines() for pdf.
Change file paths to suit your system.
*/
import processing.pdf.*;
import java.util.Random;
int cols, rows;
int videoScale = 100;
void drawLines() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int x = i*videoScale;
int y = j*videoScale;
strokeWeight(1.5);
line(x, y, x+30, y+30);
}
}
}
void setup() {
size(800, 800);
background(255);
pixelDensity(2);
// frameRate(0.5);
cols = width/videoScale;
rows = height/videoScale;
drawLines();
}
void draw() {
}
void keyPressed() {
if (key == 'p') {
PGraphics pdf = createGraphics(width, height, PDF, "/Users/me/Desktop/output.pdf"); // change this
videoScale = 50; // cut in half
pdf.beginDraw();
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int x = i*videoScale;
int y = j*videoScale;
pdf.line(x, y, x+15, y+15); // +15 instead of +30 for second x,y coordinates
}
}
pdf.dispose();
pdf.endDraw();
println("Printed pdf.");
}
if (key == 's') {
saveFrame("/Users/me/Desktop/img.png"); // change this
println("Image saved.");
}
}
Related
Edit:
I included an example of k value. Also to be clear I produce three separate arrays from an RGB image . I also include code for loading the image
public static final int[][] SHARPEN = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
Load image
BufferedImage inputImage = ImageIO.read(new File("bridge-rgb.png")); // load the image from this current folder
When I convolute an image in java using 3*3 kernel, the resultant image produced has some of the properties that you would expect from the given kernel but is extremely dark, black being the dominant colour. If I process the image with an identity kernel then identity is returned so I guess that means that Ive selected the correct setting for creating a bufferedImage and hence the problem must be with my convolution algorithm, however I did test the convolution algorithm with a test array and it does seem to be producing accurate output. I wonder could any one make any comment on what I have or point me in the right direction?
for (int j = 0; j < kernelWidth; ++j) {
try {
output+=(input[y-1][x-1+j] * k[0][j]);
counter++;
}catch(Exception e) {
continue;
}
}
for (int j = 0; j < kernelWidth;++j) {
try {
output+=(input[y][x-1+j] * k[1][j]);
counter1++;
}catch(Exception e) {
continue;
}
}
for (int j = 0; j < kernelWidth;++j) {
try {
output+=(input[y+1][x-1+j] * k[2][j]);
counter2++;
}catch(Exception e) {
continue;
}
}
if((output>>bitshiftValue)>255) {
return ((255& 0xff)<<bitshiftValue);
}
else if ((output>>bitshiftValue)<0) {
return 0;
}else {
return output;
} }
I got the arrays to be convoluted with the following method
private static int[][] convertTo2DWithoutUsingGetRGBgreen(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
int[][] result = new int[height][width];
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel + 3 < pixels.length; pixel += pixelLength) {
result[row][col] = ((int) ((pixels[pixel + 2] & 0xff) )<<8);
col++;
if (col == width) {
col = 0;
row++;
}
}
return result;
}
and post convolution I pimply added them together like so
int[][] finalConv = new int[convRedArray.length][convRedArray[0].length];
for(int c =0; c<convRedArray.length;c++) {
for(int p =0;p<convRedArray[0].length;p++) {
finalConv[c][p]=(convBlueArray[c][p])+(convGreenArray[c[p])+(convRedArray[c][p]);
I have a picture and I am trying to remove all the green pixels. How do I do this using simple java and a 2D array?
So far, my code looks like this:
public void removeGreen() {
Picture pic = new Picture("IMG_7320.JPG");
Pixel pixel = null;
for (int row = 0; row < pic.getHeight(); row++) {
for (int col = 0; col < pic.getWidth(); col++) {
pixel = getPixel(row,col);
pixel.getColor();
if(pixel.getRed() < 40 & pixel.getBlue() < 160 & pixel.getGreen() > 220) {
Color white = new Color(255,255,255);
pixel.setColor(white);
}
}
}
}
(Right now I am trying only replacing the green pixel with a white pixel because I'm not sure how to remove the pixel altogether.)
And the code in my main method that I am using to test the removeGreen() method,looks like this:
//method to test removeGreen
public static void testRemoveGreen() {
Picture me = new Picture("IMG_7320.JPG");
me.explore();
me.removeGreen();
me.explore();
}
So, my code now looks like this:
public void removeGreen(Picture pic) {
for (int row = 0; row < pic.getHeight(); row++) {
for (int col = 0; col < pic.getWidth(); col++) {
Pixel pixel = pic.getPixel(row,col);
if((pixel.getRed() < 40) && (pixel.getBlue() < 160) && (pixel.getGreen() > 220)) {
Color white = new Color(255,255,255);
pixel.setColor(white);
}
}
}
}
and my main method is still the same. I still do not understand why the method is not working properly.
The following makes clear that the passed pic parameter is altered.
public static void removeGreen(Picture pic) {
for (int row = 0; row < pic.getHeight(); row++) {
for (int col = 0; col < pic.getWidth(); col++) {
Pixel pixel = pic.getPixel(row,col);
if (pixel.getRed() < 40 && pixel.getBlue() < 160
&& pixel.getGreen() > 220) {
pixel.setColor(Color.white);
}
}
}
}
The && is a short-cut AND: x && y() will not call y is x is false.
Mind the pic.getPixel. If removeGreen is intended as method of Picture, remove the parameter and the static keyword, and pic..
public static void testRemoveGreen() {
Picture me = new Picture("IMG_7320.JPG");
me.explore();
removeGreen(me);
me.explore();
}
As the .jpg, JPEG, format does not have transparency, some colour like white is needed to be shown as "background."
I've been trying to denoise my image by using a median filter as described in this article
I'm only doing one pass until I get this thing working. The result is largely a washed out image, as seen below.
A minimal working version of my code is below:
import java.awt.image.BufferedImage;
import java.util.Arrays;
public class Denoise {
public static void main(String args[]) {
String directory = "C:\\Users\\Speedy Octopus\\Desktop\\Place Car Folders Here\\Original\\15.JPG";
BufferedImage image = ImageUtility.loadImage(directory);
for (int iterationCount = 0; iterationCount < 1; iterationCount++){
for (int i = 1; i < image.getWidth()-1; i++) {
for (int j = 1; j < image.getHeight()-1; j++) {
image.setRGB(i, j, getMedianPixelValue(image, i, j));
}
}
}
String directory2 = "C:\\Users\\Speedy Octopus\\Desktop\\Place Car Folders Here\\Original\\152.JPG";
Controller.saveImage(image, directory2);
}
public static int getMedianPixelValue(BufferedImage image, int i, int j) {
int[] surroundingPixels = new int[8];
int iter = 0;
for (int q = i-1; q<=i+1; q++) {
for (int r = j-1; r<=j+1;r++) {
if (!(q == i && r == j)) {
surroundingPixels[iter] = image.getRGB(q, r);
iter++;
}
}
}
Arrays.sort(surroundingPixels);
int medianIndex = surroundingPixels.length/2;
int medianPixel = surroundingPixels[medianIndex];
return medianPixel;
}
}
As I answered in this question Applying Mean filter on an image using java getRGB "Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB)" so you have to extract and remove the alpha (A) component before you do any comparisons:
pixel=image.getRGB(i, j)&0x00ffffff;
in the media sorting etc
And you can extract the R, G, and B and process them separately, or do the comparison on the whole pixel RGB - you can experiment either way.
I'm trying to calculate the edges of a black object within a .png file. And by that I mean find the column and row values that makeup a box that encapsulates an object. I attached a link to the photo I created which draws the box according to the values I find. As you can see the top, bottom, and right lines seem to line up correctly, but if you zoom in to the left line, part of the image is outside of the box. Why is that? I have an algorithm which I'll post below that searches every pixel in the array and finds the last occurrence of pixel values != 0 for the top, bottom, left and right sides. For some reason that extra image on the left side is registering pixels that are == 0... Are the values being rounded down to zero? If somebody could explain what is happening that would be great.
Here is a link to the image: http://i.imgur.com/UG8Cghe.png. You really have to zoom into the left side to see what I am talking about. Downloading the image and viewing is probably a necessity. It is a VERY small detail.
Here is the method that converts the BufferedImage(.png) to a 2D Array:
private static int[][] loadImageTo2D(String file_path)
{
img = null;
try { img = ImageIO.read(new File(file_path)); }
catch (IOException e) { System.out.println(e); }
int width = img.getWidth();
int height = img.getHeight();
int[][] pix = new int[height][width];
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
pix[row][col] = img.getRGB(col, row);
}//for
}//for
return pix;
}//loadImageTo2D
Here is how I am searching for the sides:
private static int[] getPerim(int[][] pix)
{
//Array holds object boundary edges.
int[] lines = new int[4];
lines[BOTTOM] = 0;
lines[TOP] = pix.length;
lines[LEFT] = pix[0].length;
lines[RIGHT] = 0;
//Top down iteration, find the first and last row and column of the
//actual graphic.
for (int row = 0; row < pix.length; row++)
{
for(int col = 0; col < pix[0].length; col++)
{
if (pix[row][col] != 0)
{
if (row < lines[TOP]) { lines[TOP] = row; }
else if (row > lines[BOTTOM]) { lines[BOTTOM] = row; }
else if (col < lines[LEFT]) { lines[LEFT] = col; }
else if (col > lines[RIGHT]) { lines[RIGHT] = col; }
}//if
}//for
}//for
return lines;
}//getPerim
I'm then using lines[] to draw the blue box you see in image. Help!
Drop the else part of if else and make all of them ifs . Only one of those if elses can be executed. What happens if pixel is the most down and most left pixel? It will be only used as the most bottom one, as the if-else statement won't get to the col part. I suggest you change it to
if (row < lines[TOP]) { lines[TOP] = row; }
if (row > lines[BOTTOM]) { lines[BOTTOM] = row; }
if (col < lines[LEFT]) { lines[LEFT] = col; }
if (col > lines[RIGHT]) { lines[RIGHT] = col; }
And no, you can't group left border with right border, as they can be on the same pixel.
The idea here is to create a grid of boxes. underneath the black grid is another grid of multi-colored boxes. when you click a box it's mask disappears showing the colored box beneath. You then click a second box if the colors match hurray, if not then the game continues. Here is the code.
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GuessingGame extends Applet{
/**
*
*/
private static final long serialVersionUID = 1L;
private final int START_X = 20;
private final int START_Y = 40;
private final int ROWS = 4;
private final int COLS = 4;
private final int BOX_WIDTH = 20;
private final int BOX_HEIGHT = 20;
//this is used to keep track of boxes that have been matched.
private boolean matchedBoxes[][];
//this is used to keep track of two boxes that have been clicked.
private MaskableBox chosenBoxes[];
private MaskableBox boxes[][];
private Color boxColors[][];
private Button resetButton;
public void init() {
boxes = new MaskableBox[ROWS][COLS];
boxColors = new Color[ROWS][COLS];
resetButton = new Button("Reset Colors");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomizeColors();
buildBoxes();
repaint();
}
});
add(resetButton);
//separate building colors so we can add a button later
//to re-randomize them.
randomizeColors();
buildBoxes();
}
public void paint(Graphics g) {
for (int row =0; row < boxes.length; row ++) {
for (int col = 0; col < boxes[row].length; col++) {
if(boxes[row][col].isClicked()) {
//boxes[row][col].setMaskColor(Color.black);
//boxes[row][col].setMask(!boxes[row][col].isMask());
//boxes[row][col].setClicked(false);
//}
if (!matchedBoxes[row][col]) {
gameLogic(boxes[row][col]);
//boxes[row][col].draw(g);
}
}
}
}
//loop through the boxes and draw them.
for (int row = 0; row < boxes.length; row++) {
for (int col = 0; col < boxes[row].length; col++) {
boxes[row][col].draw(g);
}
}
}
public void gameLogic(MaskableBox box) {
if ((chosenBoxes[0] != null)&&(chosenBoxes[1] != null)) {
if(chosenBoxes[0].getBackColor() == chosenBoxes[1].getBackColor()) {
for (int i=0; 0 < chosenBoxes.length; i++ ) {
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
if( boxes[row][col] == chosenBoxes[i] ) {
System.out.println("boxes [row][col] == chosenBoxes[] at index: " + i );
matchedBoxes[row][col] = true;
break;
}
}
}
}
}else {
chosenBoxes[0].setMask(true);
chosenBoxes[1].setMask(true);
}
chosenBoxes = new MaskableBox[2];
}else {
if (chosenBoxes[0] == null) {
chosenBoxes[0] = box;
chosenBoxes[0].setMask(false);
return;
}else{
if (chosenBoxes[1] == null) {
chosenBoxes[1] = box;
chosenBoxes[1].setMask(false);
}
}
}
}
private void removeMouseListeners() {
for(int row = 0; row < boxes.length; row ++) {
for(int col = 0; col < boxes[row].length; col++) {
removeMouseListener(boxes[row][col]);
}
}
}
private void buildBoxes() {
// need to clear any chosen boxes when building new array.
chosenBoxes = new MaskableBox[2];
// create a new matchedBoxes array
matchedBoxes = new boolean [ROWS][COLS];
removeMouseListeners();
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
boxes[row][col] =
new MaskableBox(START_X + col * BOX_WIDTH,
START_Y + row * BOX_HEIGHT,
BOX_WIDTH,
BOX_HEIGHT,
Color.gray,
boxColors[row][col],
true,
true,
this);
addMouseListener(boxes[row][col]);
}
}
}
private void randomizeColors() {
int[] chosenColors = {0,0,0,0,0,0,0,0};
Color[] availableColors = {Color.red, Color.blue, Color.green,
Color.yellow, Color.cyan, Color.magenta, Color.pink, Color.orange };
for(int row = 0; row < boxes.length; row++) {
for (int col = 0; col < boxes[row].length; col++) {
for (;;) {
int rnd = (int) (Math.random() * 8);
if (chosenColors[rnd]< 2) {
chosenColors[rnd]++;
boxColors[row][col] = availableColors[rnd];
break;
}
}
}
}
}
}
here is the second batch of code containing maskablebox
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
public class MaskableBox extends ClickableBox {
private boolean mask;
private Color maskColor;
Container parent;
public MaskableBox(int x, int y, int width, int height, Color borderColor,
Color backColor, boolean drawBorder, boolean mask, Container parent ) {
super(x, y, width, height, borderColor, backColor, drawBorder, parent);
this.parent = parent;
this.mask = mask;
}
public void draw(Graphics g) {
if(mask=false) {
super.draw(g);
// setOldColor(g.getColor());
// g.setColor(maskColor);
// g.fillRect(getX(),getY(),getWidth(), getHeight());
// if(isDrawBorder()) {
// g.setColor(getBorderColor());
// g.drawRect(getX(),getY(),getWidth(),getHeight());
// }
// g.setColor(getOldColor());
}else {
if(mask=true) {
//super.draw(g);
setOldColor(g.getColor());
g.setColor(maskColor);
g.fillRect(getX(),getY(),getWidth(), getHeight());
if(isDrawBorder()) {
g.setColor(getBorderColor());
g.drawRect(getX(),getY(),getWidth(),getHeight());
}
g.setColor(getOldColor());
}
}
}
public boolean isMask() {
return mask;
}
public void setMask(boolean mask) {
this.mask = mask;
}
public Color getMaskColor() {
return maskColor;
}
public void setMaskColor(Color maskColor) {
this.maskColor = maskColor;
}
}
I now get these error messages.
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at GuessingGame.gameLogic(GuessingGame.java:74)
at GuessingGame.paint(GuessingGame.java:55)
at java.awt.Container.update(Container.java:1801)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
at sun.awt.RepaintArea.paint(RepaintArea.java:216)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
at java.awt.Component.dispatchEventImpl(Component.java:4706)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Look, the error message you see there, is called: "the stack trace"
It contains very helpful information on what the error was:
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 2
at GuessingGame.gameLogic(GuessingGame.java:77) // This is where the error happened
at GuessingGame.paint(GuessingGame.java:55) // This is your code
at java.awt.Container.update(Container.java:1801) // not your code
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239) // not your code
If you see it says: GuessingGame.java:77 that means the error occurred in the file GuessingGame.java in the line 77. The other files are not in your source code ( Container.java and Repaint.java ) so, they are not where the problem happened.
In GuessingGame.java at line 77, you attempted to access an index beyond the array bounds ( hence ArrayIndexOutOfBoundException ) furthermore, the number 2 it the index you tried to use.
With that information you can take a look at the source code and see that line 77 ( at least from the posted code ) is:
if( boxes[row][col] == chosenBoxes[i] ) {
So, boxes is not the problem ( its size is 4 it was declared asboxes = new MaskableBox[ROWS][COLS]; and both ROWS and COLS are declared as 4 ). So the responsible must be: chosenBoxes
It was initialized as:
chosenBoxes = new MaskableBox[2];
Size 2, that means the only valid indexes are 0 and 1. When you attempt to use index=2 the exception raised.
Now having this information in mind you may search for code that might cause it.
As other answers points the cause is
for (int i=0; 0 <= chosenBoxes.length; ++i ) {
Just 3 lines above!. It reads, "while zero is lower or equals than 2..." which will always return true, because 0 will always be < than 2.
So, you just have to fix that part and re-test.
I hope this explanation help you to solve this kind of problems in the future. It is very important to learn to read the stacktrace.
I can even imagine an acronym in the future: RTFST ;)
In your gameLogic method you have chosenBoxes which has two elements, but you loop to i <= chosenBoxes.length (which would loop i = 0, 1, 2). That's why you get the exception. Change it to i < chosenBoxes.length.
Edit
In fact, you have:
for (int i=0; 0 <= chosenBoxes.length; ++i ) {
change that to:
for (int i=0; i < chosenBoxes.length; i++ ) {
I'm not sure why you were prefix incrementing i in that loop, either.
Here:
for (int i=0; 0 <= chosenBoxes.length; ++i ) {
Your condition is always true ;)