Drawing in Java using Hexadecimal - java

I've seen this in a code of tetris game and I'm wondering how these values are able to draw the line, T-shape, S-shape, Z-shape, L-shape, inverted-L and a sqaure.
I get the four parametes maybe because of the four blocks needed per piece. But how did they come up with values like that? Have those something to do with the color too?
int blocks[][] = {
{0x0f00, 0x4444, 0x0f00, 0x4444}, // LINE
{0x04e0, 0x0464, 0x00e4, 0x04c4}, // T
{0x4620, 0x6c00, 0x4620, 0x6c00}, // S
{0x2640, 0xc600, 0x2640, 0xc600}, // Z
{0x6220, 0x1700, 0x2230, 0x0740}, // 7
{0x6440, 0x0e20, 0x44c0, 0x8e00}, // inverted 7
{0x0660, 0x0660, 0x0660, 0x0660}, // square
}
I'm new in Java and I want to learn to "draw" using those values. Thank you very much!

Each row of the table is one shape. Each column is a different rotation for the shape. The square, for instance, is the same no matter how you rotate it. The line (first row) flips between two patterns. I found this in a web search:
http://codeincomplete.com/posts/2011/10/10/javascript_tetris/

Related

MP Android Chart, custom xAxis labels only on entries

1) I'm practicing stuff with graphs in order to add that feture to my app, I want the upper labels ( the xAxis base ) to be shown only where entries occur.
I haven't found a suitable solution online yet, and currently it appears on every xAxis from first entry to last entry as in the picture below:
I want it to be without the one sI deleted, as shown in the picture below:
2) and the second question I'm struggling with it is that I want to be able to draw for example in (x=5, y=7) and after it to draw at (x=1, y =3), but it wont let me add an entry with a smaller x that any other entry that already in the graph.
You have to extend from ValueFormatter class.
for more detail take a look at link
You can pick your desired logic to make the label disappear with returning "".
for example:
public String getFormattedValue(float value) {
if ((int)value <= 0) //your logic to evaluate correctness
return ""; // make lable go away
//...
}
UPDATE 2 (in Kotlin):
There is another overload for getFormattedValue which have a AxisBase parameter and you can use mEntryCount or mEntries.
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
if (axis?.mEntryCount!! <= 0)
return ""
}

Custom Coloring in Tableau by Row

I've exhausted my understanding of Tableau. I'm trying to color every icon below the risk average the color red, and above the color green. Here is an attempt at an articulation in Java of my intention (this code may not be correct, but I want the intention translated into Tableau).
This is my Tableau code that doesn't work:
IF [Commercial Services] <= [Risk Average] *The variable I want to create*
THEN "Red"
ELSEIF [Commercial Services] > [Risk Average]
THEN "Green"
END
Here's the Java code I think is closer to what I need converted:
final int NUM_ROWS = 39;
int count = 0;
while(count < NUM_ROWS) {
if(count.commercial_services < count.risk_average) {
shape.color = "Red";
} elseif(count.commercial_services > count.risk_average) {
shape.color = "Green";
} else {
shape.color = "Blue";
}
count++;
}
Please help me create a variable that I could use to color each risk in reference to the risk average.
I have attached a spreadsheet and an image for reference.
Thanks in advance.
That is not the way Tableau works. For such purposes, you should first create a flag-like calculated field to form values according to your conditions. And then use that field as a shape or color identifier dragging it to the Marks card.
So for your case you may create a field like below (which is almost the same you did), which means it will have the value of "Red" for CS<=RA condition and "Green" for the other condition. Only that Tableau does not know what to do with these string variables at the moment. You may assign -1 or 1 instead of these strings, it does make no change.
IF [Commercial Services] <= [Risk Average]
THEN "Red"
ELSE "Green"
END
And now you have new column in your data which have "Red" or "Green" values at each row. You may now drag this field to Marks card and select is as Color or as normal pill and select its type as Shape then use standard arrows or make your custom shapes according to your needs. At this phase, you will be selecting with which colours or shapes should your "Red" & "Green" values be represented by.
I or someone else might be in more help if you share your tableau workbook with what you have done so far.

iText extract faulty locations of text chunks

I am developing an algorithm to extract text and images from PDF files in the reading order. I use iText java for this purpose and basically my algorithm works as follows.
Coordinates of every text chunk in the page is extracted using iText.
Rectangle object is created using the extracted coordinates. After this step we have whole bunch of rectangle objects representing actual text chunks in the page.
Group the rectangles into larger text blocks which will be corresponding to the actual columns in the pdf page.
Order the text blocks by Y then X
Apply the locationTextExtractionStrategy for text blocks one by one.
This approach gives my around 80% or slightly more results for the PDF files with medium to complex layouts. I know that it will be almost impossible to gain 100% accuracy because PDF files does not store information in the reading order.
What I want to do is to increase my accuracy here but the problem is iText stops me from doing that. I have identified a problem in iText. It sometimes extract false locations of text chunks which makes my algorithm incorrect. Following images are a good example for that.
You can see that in the actual PDF page there is a clear gap between columns. But the resulting rectangles contains some faulty rectangles in between that gap which prevents me from identifying the correct columns.
Following is the code that I use to extract locations of text chunks.
package com.InteliText.Extract;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.LineSegment;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import com.itextpdf.text.pdf.parser.Vector;
/*
* THIS CLASS ACT AS THE TEXT EXTRACTOR FOR THE PREPROCESSOR
*/
public class PreProcessorStrategy extends SimpleTextExtractionStrategy{
private StringBuilder result = new StringBuilder();
private ArrayList<Double> fontSizes = new ArrayList<Double>();
private ArrayList<Double> lineSpaces = new ArrayList<Double>();
private ArrayList<TextSegment> textSegments = new ArrayList<TextSegment>();
Vector previousBaseLine = null;
#Override
public void beginTextBlock() {
// TODO Auto-generated method stub
}
#Override
public void endTextBlock() {
// TODO Auto-generated method stub
}
#Override
public void renderImage(ImageRenderInfo arg0) {
// TODO Auto-generated method stub
}
#Override
public void renderText(TextRenderInfo renderInfo) {
//This code assumes that if the baseline changes then we're on a newline
Vector curBaseline = renderInfo.getBaseline().getStartPoint();
Vector topRight = renderInfo.getAscentLine().getEndPoint();
//System.out.println(renderInfo.getText()+"\t"+curBaseline.get(0)+"\t"+topRight.get(0));
if(curBaseline.get(1) < 800 && curBaseline.get(1) > 50 ) {
// Chunk of text as a rectangle
Rectangle rect = new Rectangle(curBaseline.get(0), curBaseline.get(1), topRight.get(0), topRight.get(1));
double curFontSize = rect.getHeight();
fontSizes.add(curFontSize);
String text = renderInfo.getText();
boolean isBullet = text.contains("•");
if(!(text.equals(" ") || text.equals(" ") || text.equals(" ")) && !isBullet) {
double endX = topRight.get(0);
if(text.endsWith(" "))
endX -= 8;
textSegments.add(new TextSegment(curBaseline.get(0),endX,curBaseline.get(1),topRight.get(1),renderInfo.getText(),curFontSize));
}
result.append(renderInfo.getText());
}
previousBaseLine = topRight;
}
#Override
public String getResultantText() {
// TODO Auto-generated method stub
return result.toString();
}
public ArrayList<TextSegment> getResultantTextSegments() {
return this.textSegments;
}
I use the resulting textSegments ArrayList to create rectangle objects by looking at the coordinates stored in those textSegments. I suspects that this is might be a bug in iText.
As you can see currently I'm shrinking the text chunks a little bit if the content of that text chunk ends with a white space. But this is a temporary fix and I don't want to do that because it shrink the correct text chunks too.
So is there a work around for this one? Or if it is a problem in my code please help me to fix that..
I am assuming here that if you knew where the columns were you could assign each rectangle to the correct column. It looks to me that if you drew a line down the left edge of the right hand column you could assign almost all of the rectangles correctly based on whether their centre was to the right or left of that edge. So the problem is to find the parameters that describe the data best (in particular the left hand edge of the rightmost column) in the presence of outliers.
The absolutely correct way is probably to fit some sort of statistical model, but I think there are a couple of easier hacks that might work.
1) All of the overlapping rectangles in your image seem to be very small. Perhaps you can simply remove rectangles below a given size, work out where the columns should be, and then assign each small rectangle according to whether its centre is to the left or right of the left hand edge of the right hand column.
2) There is a general strategy for fitting data contaminated by outliers you can derive from https://en.wikipedia.org/wiki/RANSAC.
2a) Start by fitting the model to only a small amount of the data. You will be repeating 2a and 2b multiple times, and picking the best result. You are hoping that the initial points chosen for one of these cases are completely free of outliers. Note that if there are N outliers and you divide the data into N+1 chunks, at least one of these chunks must be completely free of outliers.
2b) Once you have an initial fit, look at all the data and work out which points are outliers and ignore them temporarily (i.e. put aside the k worst fitting points). Then fit the model again using the remaining points. In many cases you can prove that if you repeat this step indefinitely it will eventually converge to something, because changing the points identified as the k worst fits improves the fit, as does re-fitting the model, so each iteration improves the fit until you there is no change, at which point you declare that the process has converged.

Median filter implementation using java

I am implementing a median filter using java, but the output i am getting is not satisfactory . I have done using a 3*3 neighborhood . Here is my code :
for(i=1;i<height-1;i++)
{
for(j=1;j<width-1;j++)
{
Color c=new Color(img.getRGB(j,i));
red=c.getRed();
green=c.getGreen();
blue=c.getBlue();
x=j;
y=i;
x1=x-1;
y1=y;
x2=x+1;
y2=y;
x3=x;
y3=y-1;
x4=x;
y4=y+1;
x5=x+1;
y5=y+1;
x6=x-1;
y6=y-1;
x7=x-1;
y7=y+1;
x8=x+1;
y8=y-1;
Color c1=new Color(img.getRGB(x1,y1));
red1=c1.getRed();
green1=c1.getGreen();
blue1=c1.getBlue();
Color c2=new Color(img.getRGB(x2,y2));
red2=c2.getRed();
green2=c2.getGreen();
blue2=c2.getBlue();
Color c3=new Color(img.getRGB(x3,y3));
red3=c3.getRed();
green3=c3.getGreen();
blue3=c3.getBlue();
Color c4=new Color(img.getRGB(x4,y4));
red4=c4.getRed();
green4=c4.getGreen();
blue4=c4.getBlue();
Color c5=new Color(img.getRGB(x5,y5));
red5=c5.getRed();
green5=c5.getGreen();
blue5=c5.getBlue();
Color c6=new Color(img.getRGB(x6,y6));
red6=c6.getRed();
green6=c6.getGreen();
blue6=c6.getBlue();
Color c7=new Color(img.getRGB(x7,y7));
red7=c7.getRed();
green7=c7.getGreen();
blue7=c7.getBlue();
Color c8=new Color(img.getRGB(x8,y8));
red8=c8.getRed();
green8=c8.getGreen();
blue8=c8.getBlue();
a[0]=red1;
a[1]=red2;
a[2]=red3;
a[3]=red4;
a[4]=red5;
a[5]=red6;
a[6]=red7;
a[7]=red8;
a=bubbleSort(a);
redf=a[2];
a[0]=green1;
a[1]=green2;
a[2]=green3;
a[3]=green4;
a[4]=green5;
a[5]=green6;
a[6]=green7;
a[7]=green8;
a=bubbleSort(a);
greenf=a[2];
a[0]=blue1;
a[1]=blue2;
a[2]=blue3;
a[3]=blue4;
a[4]=blue5;
a[5]=blue6;
a[6]=blue7;
a[7]=blue8;
a=bubbleSort(a);
bluef=a[2];
Color nc=new Color(redf,greenf,bluef);
img.setRGB(x,y,nc.getRGB());
}
}
The outer loop is from 1 to height-1 . It's not getting printed
As it was mentioned, you are using same image img for both reading and writing into, so it affects the pixels calculations. You may also visit this JavaScript tutorial http://fiveko.com/tutorials/dip/median. It describes a histogram based approach for median values extraction and seems promising for bigger kernel sizes.

Cities and colors java

the problem consists in that you have to solve a problem where exists n-cities and n-colors , every city have a color but the cities adjacent can't have the same color.
The teacher send this homework for investigate how to solve it , we are working on java - eclipse and we are so confused . Can you help us being really explicit , thanks so much. the last topic was recursion.
P.D: some people say that it solve by graphs but we are not sure and we dont understand really good .
If I were you, I would make 2 loops for, one inside the other, like this:
for (i = 0; i < n_cities; i++)
{
for (j = 0; j < n_colours; j++)
{
}
}
In this way, you will be able to walk all the possible combinations. In the second loop, you will have to make the comparison if the cities around citie i have the same colour, if not, try with the next colour, and this for all the n-iterations.
How to know if the cities adjacent are with the same colour? Well, you can create a n-vector in which you will save all the colours of all the cities. The position of the vector could be the i-citie and the value saved in the position could be the colour of this citie.
I expect it will be useful for you :)
There are probably many solutions, but the first one that came to mind is having an ArrayList containing CityColor instances. My CityColor class holds the city name and color. When adding to the ArrayList, you would check to make sure the adjacent elements of the ArrayList do not have the same color. If it does have the same color, use recursion to select another color until you get an acceptable color.
Your ArrayList would have objects such as:
public class CityColor {
private String city;
private String color;
public CityColor(String city, String color) {
this.city = city;
this.color = color;
}
// set/get methods for the variables
}
If you have an array of colors, you can use Math.Random to select a random color. If the color matches an adjacent color, use recursion to select another.

Categories

Resources