|
When I translate the C code to C# it works fine because in both C and C# there is unsigned 8 bit integer type "byte" (values from 0 to 255) while in Java "byte" is signed 8 bit integer (values from -128 to 127). What operations might have effect when signed used instead of unsigned?
|
|
|
|
|
If you use arithmetic operations on them then you will get incorrect results.
Veni, vidi, abiit domum
|
|
|
|
|
How do you mean, can you explain me more ?
|
|
|
|
|
Try this code:
public static void main(String[] args) throws Exception {
byte b1 = (byte)129;
byte b2 = 4;
byte b3 = (byte)(b1 + b2);
System.out.println("b1 is: " + b1);
System.out.println("b2 is: " + b2);
System.out.println("b3 is: " + b3);
If the byte values were unsigned you would expect the answer to be 133 , but in fact you will get -123 , since the 'real' value of b1 is, as you see, -127 .
Veni, vidi, abiit domum
|
|
|
|
|
Yes, but I am receiving un-correlated values, how to get 133 instead of -123 - I mean what modifications or things I have to do at the end?
|
|
|
|
|
You have to get the value as an integer and then mask off the low order 8 bits, to make it into an unsigned value, like this:
public static void main(String[] args) throws Exception {
byte b1 = (byte)129;
byte b2 = 4;
int i1 = b1;
int i2 = i1 & 0xFF;
int i3 = i2 + b2;
System.out.println("i1 is: " + i1);
System.out.println("i2 is: " + i2);
System.out.println("i3 is: " + i3);
}
[edit]Fixed typo; I meant low order 8 bits.[/edit]
Veni, vidi, abiit domum
modified 14-Jan-14 11:02am.
|
|
|
|
|
You can't expect 100% automatic conversion. Even commercial products offers only 95-99% conversion.
In Google you could find few free converters. Ex. C To Java Converter[^]
There's a better way if you have more time & resources. Code Rewrite[^]
thatrajaCode converters | Education Needed
No thanks, I am all stocked up. - Luc Pattyn
When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute
|
|
|
|
|
I wouldn't think that would work at all for any non-trivial business application.
And once translated it would be so mucked up that it would be significantly more costly to maintain.
|
|
|
|
|
Ok, here is the code to resize one rectangle.
I would like to display several rectangles and resize them independantly, so the best way is to create an arraylist, right ? I have modified the code (see at the bottom), but it displays only the rectangles. No error messages when I test the program, but impossible to resize them.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Resizing extends JPanel {
Rectangle rect = new Rectangle(100,100,150,150);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
g2.draw(rect);
}
public static void main(String[] args) {
Resizing test = new Resizing();
Resizer resizer = new Resizer(test);
test.addMouseListener(resizer);
test.addMouseMotionListener(resizer);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test);
f.setSize(400,400);
f.setLocation(100,100);
f.setVisible(true);
}
}
class Resizer extends MouseAdapter {
Resizing component;
boolean dragging = false;
final int PROX_DIST = 3;
public Resizer(Resizing r) {
component = r;
}
public void mousePressed(MouseEvent e) {
if(component.getCursor() != Cursor.getDefaultCursor()) {
dragging = true;
}
}
public void mouseReleased(MouseEvent e) {
dragging = false;
}
public void mouseDragged(MouseEvent e) {
if(dragging) {
Point p = e.getPoint();
Rectangle r = component.rect;
int type = component.getCursor().getType();
int dx = p.x - r.x;
int dy = p.y - r.y;
switch(type) {
case Cursor.N_RESIZE_CURSOR:
int height = r.height - dy;
r.setRect(r.x, r.y+dy, r.width, height);
break;
case Cursor.S_RESIZE_CURSOR:
height = dy;
r.setRect(r.x, r.y, r.width, height);
break;
default:
System.out.println("unexpected type: " + type);
}
component.repaint();
}
}
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
if(!isOverRect(p)) {
if(component.getCursor() != Cursor.getDefaultCursor()) {
component.setCursor(Cursor.getDefaultCursor());
}
return;
}
int outcode = getOutcode(p);
Rectangle r = component.rect;
switch(outcode) {
case Rectangle.OUT_TOP:
if(Math.abs(p.y - r.y) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.N_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_BOTTOM:
if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.S_RESIZE_CURSOR));
}
break;
default:
component.setCursor(Cursor.getDefaultCursor());
}
}
private int getOutcode(Point p) {
Rectangle r = (Rectangle)component.rect.clone();
r.grow(-PROX_DIST, -PROX_DIST);
return r.outcode(p.x, p.y);
}
private boolean isOverRect(Point p) {
Rectangle r = (Rectangle)component.rect.clone();
r.grow(PROX_DIST, PROX_DIST);
return r.contains(p);
}
}
MODIFIED CODE
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.*;
class Resizing extends JPanel {
ArrayList paths = new ArrayList();
public Resizing() {
paths.add(wrap(new Rectangle2D.Double(100, 100, 75, 50)));
paths.add(wrap(new Rectangle2D.Double(250, 100, 75, 50)));
}
private Path2D.Double wrap(Shape s) {
return new Path2D.Double(s);
}
<a href="/Members/override">@Override</a>
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
for(int j = 0; j < paths.size(); j++) {
Path2D.Double path = (Path2D.Double)paths.get(j);
g2.draw(path);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Resizing());
f.setSize(400,400);
f.setLocation(100,100);
f.setVisible(true);
}
class Resizer extends MouseAdapter {
Resizing component;
boolean dragging = false;
final int PROX_DIST = 3;
Path2D.Double selectedPath;
public Resizer(Resizing rz) {
component = rz;
component.addMouseListener(this);
component.addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e) {
if(component.getCursor() != Cursor.getDefaultCursor()) {
dragging = true;
}
}
public void mouseReleased(MouseEvent e) {
dragging = false;
}
public void mouseDragged(MouseEvent e) {
if(dragging) {
Point p = e.getPoint();
ArrayList list = component.paths;
for(int j = 0; j < list.size(); j++) {
Path2D.Double path = (Path2D.Double)list.get(j);
selectedPath = path;
Rectangle r = selectedPath.getBounds();
int type = component.getCursor().getType();
int dx = p.x - r.x;
int dy = p.y - r.y;
switch(type) {
case Cursor.N_RESIZE_CURSOR:
int height = r.height - dy;
r.setRect(r.x, r.y+dy, r.width, height);
break;
case Cursor.S_RESIZE_CURSOR:
height = dy;
r.setRect(r.x, r.y, r.width, height);
break;
default:
System.out.println("unexpected type: " + type);
}
component.repaint();
}}
}
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
if(!isOverRect(p)) {
if(component.getCursor() != Cursor.getDefaultCursor()) {
component.setCursor(Cursor.getDefaultCursor());
}
return;
}
int outcode = getOutcode(p);
ArrayList list = component.paths;
Rectangle r = selectedPath.getBounds();
switch(outcode) {
case Rectangle.OUT_TOP:
if(Math.abs(p.y - r.y) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.N_RESIZE_CURSOR));
}
break;
case Rectangle.OUT_BOTTOM:
if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
component.setCursor(Cursor.getPredefinedCursor(
Cursor.S_RESIZE_CURSOR));
}
break;
default:
component.setCursor(Cursor.getDefaultCursor());
}}
private int getOutcode(Point p) {
Rectangle r = (Rectangle)component.paths.clone();
r.grow(-PROX_DIST, -PROX_DIST);
return r.outcode(p.x, p.y);
}
private boolean isOverRect(Point p) {
Rectangle r = (Rectangle)component.paths.clone();
r.grow(PROX_DIST, PROX_DIST);
return r.contains(p);
}
}}
|
|
|
|
|
i am making an educational application using java technology.
where i need to send password on user's mobile in forgot password condition.
would you plz send me its code?
|
|
|
|
|
Here you go: Teh codez[^]
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Veni, vidi, abiit domum
|
|
|
|
|
|
I'm still new to Java. I'm having trouble while retrieving the RGB pixel values from my method, compareHexaRGB which in this method I could get the difference pixel values based on comparison of 2 similar images. I tried the printString2DArray method but it doesn't work. It stated either incompatible types: String[][] cannot be converted to String[] or produce errors such as [[Ljava.lang.String;@6d09a8df
After I get the values, I would like to combine it to one Hex value then convert it to ASCII characters. I couldn't proceed to this process as the retrieve value couldn't work.
Here is the code for compareHexaRGB() method:
public class compareHexaRGB
{
private static int w;
private static int h;
private static BufferedImage img;
private static BufferedImage img2;
private static String[][] check_hex2;
private static String[][] check_hex4;
private static String[][] message;
public static void compareHexaRGB(BufferedImage image, BufferedImage image2, int width, int height) throws IOException
{
w = width;
h = height;
img = image;
img2 = image2;
}
public void check() throws IOException
{
getPixelRGB1 pixel = new getPixelRGB1();
getPixelData1 newPD = new getPixelData1();
int[] rgb;
int count = 0;
int[][] pixelData = new int[w * h][3];
check_hex2 = new String[w][h];
check_hex4 = new String[w][h];
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
rgb = newPD.getPixelData(img, i, j);
for(int k = 0; k < rgb.length; k++)
{
pixelData[count][k] = rgb[k];
}
if(pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]))
{
System.out.println("\nPixel values at position 2 are the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j]);
}
if(pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))
{
System.out.println("\nPixel values at position 4 are the same." + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]);
}
if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]))
{
System.out.println("\nPixel values at position 2 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j]);
check_hex2[i][j] = pixel.display_img2Hex2()[i][j];
System.out.println("\nOutput Hex 2: " + check_hex2[i][j]);
}
if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))
{
System.out.println("\nPixel values at position 4 are not the same." + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]);
check_hex4[i][j] = pixel.display_img2Hex4()[i][j];
System.out.println("\nOutput Hex 4: " + check_hex4[i][j]);
}
if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])))
{
System.out.println("\nOne of the pixel values at position 2 and 4 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j] + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]);
if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j])))
{
check_hex2[i][j] = pixel.display_img2Hex2()[i][j];
System.out.println("\nOutput Hex 2: " + check_hex2[i][j]);
}
if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]) || (pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])))
{
check_hex4[i][j] = pixel.display_img2Hex4()[i][j];
System.out.println("\nOutput Hex 4: " + check_hex4[i][j]);
}
}
count++;
System.out.println("\nOutput Count: " + count);
}
}
}
public String[][] getCheck_hex2()
{
return check_hex2;
}
public String[][] getCheck_hex4()
{
return check_hex4;
}
}
Code for extractMessage() method:
public class extractMessage
{
private static String character;
private static String[][] char1;
private static String[][] char2;
private static int w;
private static int h;
private static void printString2DArray(String[][] in)
{
for (int i = 0; i < in.length; i++)
{
if (i != 0)
{
System.out.print(", ");
}
System.out.print(java.util.Arrays.toString(in[i]));
}
}
public static void charExtract()
{
compareHexaRGB hexRGB = new compareHexaRGB();
char1 = new String[][]
{
new String[] { hexRGB.getCheck_hex2() }
};
System.out.println("Char 1: ");
printString2DArray(hexRGB.getCheck_hex2());
}
public String convertHexToString()
{
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
extractMessage extract = new extractMessage();
extract.charExtract();
for(int i = 0; i < extract.length(); i+=2)
{
String output = extract.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
temp.append(decimal);
}
return sb.toString();
}
}
Any help I would appreciate!
modified 9-Jan-14 11:23am.
|
|
|
|
|
Yout compareHexaRGB method does nothing so serves no purpose. I am not sure what display_imgHex2 is supposed to do, but why do you need to convert pixels to strings in order to compare them? Why not just something like:
if(pixel1[i][j] == pixel2[i][j]))
You seem to be creating something that is far more complicated than necessary.
Veni, vidi, abiit domum
|
|
|
|
|
what is the approach to calculate the weight (influence of each feature that is collected from YouTube metadata to recognize harassment videos) and the score (similarity ratio of each feature with training dataset)of the features in data mining for classification purpose.How to set threshold value of accuracy to check whether our classifier is classifying the testing dataset correctly or not ?? Also how to make our own Logistics Regression classifier for the classification whether the given vidoe is Harassment type or not ??
Plz reply asap...
Thanks in advance...
|
|
|
|
|
Does this have anything to do with Java?
Veni, vidi, abiit domum
|
|
|
|
|
Hello,
I have the following code in C#:
public void encrypt()
{
SeaX11:
...
if (Cnt != 47) goto SeaX12;
goto SeaX21;
SeaX12:
...
goto SeaX11;
SeaX21:
...
if (Cnt != 1) goto SeaX22;
goto SeaX23;
SeaX22: goto SeaX21;
SeaX23:
return;
}
to convert in Java. Can anyone help with this?
|
|
|
|
|
http://stackoverflow.com/questions/2430782/alternative-to-goto-statement-in-java[^]
Java reserves goto but does not define it. However, statement labels do exist, and in a strange twist break and continue were enhanced to accept them. You can only jump out of local blocks, but break operates on any block – not just loops. This makes Java break almost the full equivalent of C# goto.
Java switch operates on (boxed or unboxed) primitives and enums, and since Java SE 7 also on strings. You don’t need to qualify enum case values with the enum type as in C#. Java allows fall-through from one case to the next, just like C/C++. Use the compiler option -Xlint:fallthrough to warn against missing break statements. There is no equivalent to C# goto targeting a case label.
|
|
|
|
|
I would suggest you start from scratch and redesign your program. With all those goto statements it's impossible to figure out the logic.
Veni, vidi, abiit domum
|
|
|
|
|
I am afraid that I can't rewrite the code since it is ported from 8051 assembler to C#. I thought we could do something with
if/else to
goto ?
|
|
|
|
|
Since there is no goto statement in Java, you really do not have much choice.
Veni, vidi, abiit domum
|
|
|
|
|
Hello
i'm looking for a tool that can generate the needed JNI code using only header file in order to call native C/C++ functions from a library (.so file). i'm having hard time to find the documentation i need to understand the tools so i try to ask here
i'm using eclipse on linux
the popular tools are - SWIG, JNIWrapper, Gluegen, JNA, Bridj.
i wish the tool could do the following:
1. Generate JNI code that needed to call C/C++ code? (not another wrape like SWIG does)
2. Generate conversion code in the JNIEXPORT-JNICALL methods so a structs inside structs will also will be converted automatically and not only the external struct. SWIG, for example is passing a pointer only to the external struct.
3. Good performance. Does anywone ever checked those tools?
4. Examples. if you have examples i'd love to receive.
Thanks a lot in advance
Tomer
|
|
|
|
|
|
Thank you Veni
i don't want to write manually the JNI code. i'm looking for a tool that could do that with given header(.h) file.
Do you know if such tool exist and answers my needs?
Thanks
|
|
|
|
|