|
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
|
|
|
|
|
The javah command generates the basic .h files, which is about all you can do automatically. I don't know what other code you imagine can be generated for you.
Veni, vidi, abiit domum
|
|
|
|
|
The javah command generates only the signatures of the methods but it doesn't generates conversion of the parameters i want to pass. Until now i managed to work only with 1 out of many tools exist - SWIG: http://en.wikipedia.org/wiki/SWIG[^]
SWIG command generates part of the implementation file (.c file) and converts variables to JNI types. For example it generates code of conversion from int to jint. One disadvantage of SWIG is that it doesn't convert internal struct if this one is inside external struct
|
|
|
|
|
I have data in MySQL database i retrieve them to draw Graphics2D shapes, the data Stored in database as Medium-Text,
each 6 points(coordinates) in single line, i spilt spaces,new line and i parsed String[] to double[], i created 4 classes, -1st class for retrieving data from database.
-2nd class for pathEntry extends JPanel i declared 6 double variables to use them in paintComponent() method in curveTo() method(to draw curves).
-3rd class for Paths extends JPanel where i used ArrayList<pathentry> and called moveTo() method and loop on paintComponent() method in pathEntry class
-4th class i used JFrame to draw all Shapes are called from the last 3 classes to draw Shapes.
i stuck in the shapes did not display on the JFrame ?
1st Class:
public class selectStmt {
public Paths db = null;
.....
String values = null;
int noPrint = 0;
while (rs.next()) {
values = rs.getString(1);
String[] valueNewLine = values.split("\\r\\n");
double[] Points = new double[valueNewLine.length];
for(int i=0;i<valueNewLine.length;i++){
Points = StringtoDoubleArray (valueNewLine [i]);
db.add(valueNewLine[i]);
db.MoveToX= Points [Points.length - 2];
db.MoveToY = Points [Points.length - 1];
noPrint++;
if(noPrint % 6 == 0) {
System.out.println();
noPrint = 0;
}}
...
public double[] StringtoDoubleArray (String str){
double[] result = new double[6];
String[] temp = str.split("\\s");
for(int i=0;i<temp.length;i++){
result [i] = Double.parseDouble(temp[i]);
}
return result;
}
2nd Class:
public class Paths extends JPanel{
public double MoveToX=0;
public double MoveToY=0;
ArrayList <pathEntry> p=new ArrayList<pathEntry>();
<a href="/Members/override">@Override</a>
public void paintComponent(Graphics g) {
super.paintComponent(g);
update(g);
}
public void update(Graphics g){
Graphics2D g2d = (Graphics2D) g;
GeneralPath path=new GeneralPath();
path.moveTo(MoveToX,MoveToY);
for ( int i = 0 ; i < p.size() ; i++ ) {
p.get(i).paintComponent(g2d);
}
}
public void paint_dump() {
System.out.println(MoveToX+" "+MoveToY);
for ( int i = 0 ; i < p.size() ; i++ ) {
System.out.println(p.get(i));
}
}
public void add (String pathstr) {
pathEntry pe=new pathEntry();
String[] temp = pathstr.split("\\s");
pe.cx1 = Double.parseDouble(temp[0]);
pe.cy1 = Double.parseDouble(temp[1]);
pe.cx2 = Double.parseDouble(temp[2]);
pe.cy2 = Double.parseDouble(temp[3]);
pe.x2 = Double.parseDouble(temp[4]);
pe.y2 = Double.parseDouble(temp[5]);
this.p.add(pe);
}
}
3rd Class:
public class pathEntry extends JPanel{
public double cx1 = 0;
public double cy1=0;
public double cx2=0;
public double cy2=0;
public double x2=0;
public double y2=0;
<a href="/Members/override">@Override</a>
public void paintComponent(Graphics g) {
super.paintComponent(g);
update(g);
}
public void update(Graphics g){
Graphics2D g2d = (Graphics2D) g;
GeneralPath path=new GeneralPath();
path.curveTo( cx1 , cy1 , cx2 , cy2 , x2 , y2 );
g2d.draw(path);
g2d.fill(path);
}
}
4th Class :
public class Main extends JFrame {
public selectStmt s;
public static void main(String[] args){
Main m=new Main();
}
public Main(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1024, 768);
setTitle("Drawing Shapes");
setVisible(true);
s = new selectStmt();
add(s.db);
s.db.paint_dump();
}
}
modified 31-Dec-13 5:38am.
|
|
|
|
|
In your 3rd class you create a Path without an initial MoveTo .
Veni, vidi, abiit domum
|
|
|
|
|
i solved it with other way in 3nd class
public void paint (Graphics g, GeneralPath path) {
path.curveTo( cx1 , cy1 , cx2 , cy2 , x2 , y2 )};
and in the 2rd class i replaced with g2d.draw(path); instead in of 3rd class
Thanks anyways
|
|
|
|
|