Avatar billede fredand Forsker
10. oktober 2003 - 11:18 Der er 6 kommentarer og
2 løsninger

Is everything possible with Java?

Hello!

I use to say that everything is possible with Java but perhaps it is not?

I saw this simple but fly app and wondering if it is possible to do the same with Java but now I guess that it is not possible. The simple app is a calipers for pixels that actally always is on top of everything even above the appbar at the bottom of your desktop in win 2000.
You can download it at:
http://www.ksurf.net/~bermania/prog/dessin/prdessin034.html

My attempt to doing something like that is (with a lot of help of people at eksperten) by copying the desktop-image as a background but perhaps they not use that strategy in calipers.exe.

In this app thay also got the app to be on top always. Perhaps that is impossible for us Java developers.

Hope for a nice and giving conversation.

Best regards
Fredrik

My attempt with a fictiv transparent window:

package glasswindow;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class GlassWindowTest extends JWindow implements MouseListener, MouseMotionListener
{
    private Image screen;
    private Image backBuffer;
    private Graphics backGraphics;

    private Dimension preferredSize = new Dimension(250,200);
    private Dimension screenSize;
    private Rectangle screenRectangle;
    private int xdiff = 0;
    private int ydiff = 0;
    private static Robot robot;
    private Dimension windowSize;
    private int newX;
    private int newY;
    private boolean first = false;
    private java.awt.Point mouseP;
    private double  oldPosX, oldPosY, mouseFirstX, mouseFirstY, mouseNowX, mouseNowY;

    public GlassWindowTest()
    {
        super();

        JPanel content = (JPanel) getContentPane();
        content.setOpaque(false);
        setSize( getPreferredSize() );

        windowSize = getSize();
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        screenRectangle = new Rectangle();
        screenRectangle.setSize( screenSize );
        Rectangle bounds = getBounds();
        newX = (screenSize.width - bounds.width) / 2;
        newY = (screenSize.height - bounds.height) / 2;

        try
        {
            robot = new Robot();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        addMouseListener( this );
        addMouseMotionListener( this );

        copyScreen();

        setSize(250, 200);
        centerFrameOnScreen();
        show();
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        if(backBuffer == null)
        {
            createBackGraphics();
        }

        if( screen != null )
        {
            backGraphics.drawImage( screen, 0, 0, windowSize.width, windowSize.height, newX, newY, newX+windowSize.width, newY+windowSize.height, null);
        }

        backGraphics.setColor(Color.red);
        backGraphics.fillOval(50, 50, 50,50);

        g.drawImage(backBuffer, 0, 0, null);
    }

    private void createBackGraphics()
      {
        backBuffer = createImage(250, 200);
        backGraphics = backBuffer.getGraphics();
    }

    public void centerFrameOnScreen()
    {
        setBounds( newX, newY, 250, 200);
      }

      public void copyScreen()
      {
        screen = robot.createScreenCapture( screenRectangle );
    }

      public Dimension getMinimumSize()
      {
        return this.preferredSize;
    }

    public Dimension getPreferredSize()
    {
        return this.preferredSize;
    }

      public void mouseClicked(MouseEvent e)
      {
    }

      public void mouseReleased(MouseEvent e)
      {
    }

      public void mouseEntered(MouseEvent e)
      {
    }

      public void mouseExited(MouseEvent e)
      {
    }

    public void mousePressed(MouseEvent e)
    {
        first = true;
    }

    public void mouseDragged(MouseEvent e)
    {
        mouseP = e.getPoint();

        if(first)
        {
            mouseFirstX = mouseP.getX();
            mouseFirstY = mouseP.getY();
        }

        mouseNowX = mouseP.getX();
        mouseNowY = mouseP.getY();

        oldPosX = this.getX();
        oldPosY = this.getY();

        newX = (int)((oldPosX + mouseNowX) - mouseFirstX);
        newY = (int)((oldPosY + mouseNowY) - mouseFirstY);

        setLocation( newX, newY );
        repaint();
        first = false;
    }

    public void mouseMoved(MouseEvent e)
    {
    }

    public static void main(String[] args)
    {
        GlassWindowTest glassWindow = new GlassWindowTest();
    }


}
Avatar billede arne_v Ekspert
10. oktober 2003 - 11:48 #1
No everything is not possible with Java.

Many things is possible with Java.

But the fact that Java is platform independent obviusly
limit its capability to do very platform specific things.
Avatar billede fredand Forsker
10. oktober 2003 - 12:10 #2
Hello!

Do you guess that the calipers.exe is indeed a very platform specific thing then or do think that it is possible to do the same with Java?

Best regards

Fredrik

PS
Make an "Svar" instead of a "Kommentar" so you can get your share of the points
Avatar billede magoo20000 Nybegynder
10. oktober 2003 - 17:28 #3
I'm pretty sure you can make something like that in Java. E.g. IntelliJ is made in Java - and I think it looks pretty fine!

Feel free to be inspired by this code:

import java.applet.Applet;
import java.awt.*;

public class Ruler extends Applet {

    int yOffset;
    TextField tField;
    double time;
    double ts;
    boolean running;
    Dimension area;
    Image fgImage;
    Image bgImage;
    Image gImage;
    Graphics fg;
    Graphics bg;
    Graphics g;
    Graphics g0;
    int x0;
    int y0;
    Checkbox cShow;
    boolean showInfo;
    Color bgColor;
    String rts;
    String STR[] = {
        "Reset", "Show", "mm"
    };
    boolean rightClick;
    int xs;
    private int Xb[] = {
        60, 60, 90, 100, 100, 620, 620, 100, 100, 60
    };
    private int Yb[] = {
        60, 180, 246, 251, 140, 140, 60, 60, 60, 60
    };
    private int Xf[] = {
        0, 0, 10, 50, 270, 270, 0
    };
    private int Yf[] = {
        0, 110, 105, 40, 40, 0, 0
    };
    int xc;
    int xx;
    int chy;
    FontMetrics fm;
    int x;
    int y;
    int dx;
    String str;
    String valStr;


    public Ruler() {
        yOffset = 40;
        time = 0.0D;
        ts = 0.0D;
        running = false;
        showInfo = false;
        bgColor = new Color(200, 223, 208);
        rightClick = false;
        xc = 10;
        xx = 0;
    }


    public void init() {
        for (int i = 0; i < STR.length; i++)
            if ((rts = getParameter(STR[i])) != null)
                STR[i] = new String(rts);

        setBackground(bgColor);
        Panel panel = new Panel();
        panel.add(tField = new TextField("0.0", 3));
        panel.add(new Label(STR[2]));
        panel.add(new Button(STR[0]));
        panel.add(cShow = new Checkbox(STR[1]));
        add("North", panel);
        area = size();
        area.height -= yOffset;
        x0 = 100;
        y0 = 140;
        reset(true);
    }

    public boolean action(Event event, Object obj) {
        if (event.target instanceof Button) {
            String s = (String) obj;
            if (s.equals(STR[0]))
                reset(true);
        } else if (event.target == cShow) {
            showInfo = cShow.getState();
            repaint();
        } else if (event.target == tField) {
            String s1 = ((String) obj).trim();
            g0.drawImage(gImage, 0, yOffset, this);
            if (s1.equals(valStr)) {
                str = "Great! you have the right answer!";
                g0.setColor(Color.blue);
            } else {
                g0.setColor(Color.red);
                float f = (float) (Double.valueOf(s1).doubleValue() - Double.valueOf(valStr).doubleValue());
                if ((double) f == 0.0D)
                    str = "Check number of digits.";
                else
                    str = "Difference is " + String.valueOf(f) + " mm";
                g0.drawString(str, (area.width - fm.stringWidth(str)) / 2, 2 * chy + yOffset);
                str = "Wrong answer! Try it again!";
            }
            g0.drawString(str, (area.width - fm.stringWidth(str)) / 2, chy + yOffset);
        }
        return true;
    }

    public void reset(boolean flag) {
        clear();
        if (flag)
            tField.setText(" 0.0");
        valStr = "0.0";
        repaint();
    }

    public boolean mouseDown(Event event, int i, int j) {
        if ((j -= yOffset) < 0)
            return false;
        if (event.modifiers == 4)
            rightClick = true;
        xs = i;
        return true;
    }

    public boolean mouseDrag(Event event, int i, int j) {
        if ((j -= yOffset) < 0)
            return false;
        xx += i - xs;
        xs = i;
        if (xx < 0) {
            xx = 0;
            tField.setText("0.0");
        } else {
            float f = (float) ((double) xx / 10D);
            valStr = String.valueOf(f);
            if (valStr.indexOf(".") == -1)
                str += ".0";
            if (showInfo)
                tField.setText(valStr);
            else
                tField.setText("");
        }
        repaint();
        return true;
    }

    public boolean mouseUp(Event event, int i, int j) {
        if ((j -= yOffset) < 0)
            return false;
        if (rightClick)
            rightClick = false;
        else
            running = !running;
        return true;
    }

    void clear() {
        if (bg == null) {
            gImage = createImage(area.width, area.height);
            g = gImage.getGraphics();
            bgImage = createImage(area.width, area.height);
            bg = bgImage.getGraphics();
            fgImage = createImage(271, 130);
            fg = fgImage.getGraphics();
            fg.setColor(Color.blue);
            g0 = getGraphics();
        }
        bg.clearRect(0, 0, area.width, area.height);
        bg.setColor(Color.blue);
        bg.drawPolygon(Xb, Yb, 10);
        int l = y0 - 10;
        for (int i = x0 + xc; i < 620; i += 10)
            bg.drawLine(i, y0, i, l);

        l -= 5;
        for (int j = x0 + xc; j < 620; j += 50)
            bg.drawLine(j, y0, j, l);

        l -= 5;
        fm = bg.getFontMetrics();
        chy = fm.getHeight();
        int k = x0 + xc;
        for (int i1 = 0; k < 620; i1++) {
            bg.drawLine(k, y0, k, l);
            str = String.valueOf(i1 * 10);
            bg.drawString(str, k - fm.stringWidth(str) / 2, l);
            k += 100;
        }

        fg.drawPolygon(Xf, Yf, 7);
        l = 10;
        int k1 = fm.getHeight();
        k = xc;
        for (int j1 = 0; k < 201; j1++) {
            fg.drawLine(k, 0, k, l);
            str = String.valueOf(j1);
            fg.drawString(str, k - fm.stringWidth(str) / 2, l + k1);
            k += 19;
        }

        fg.drawString("0.1mm", 220, l + k1);
        xx = 0;
        tField.setText("  0.0");
    }

    public void paint(Graphics g1) {
        update(g1);
    }

    public void update(Graphics g1) {
        g.drawImage(bgImage, 0, 0, this);
        g.drawImage(fgImage, x0 + xx, y0 + 1, this);
        dx = xx - (xx / 10) * 10;
        if (showInfo) {
            g.setColor(Color.red);
            g.drawLine(x = x0 + xc + (xx / 10) * 10, y = y0 - 20, x, y - 20);
            g.drawLine(x - 3, y - 5, x, y);
            g.drawLine(x + 3, y - 5, x, y);
            str = String.valueOf(xx / 10) + ".0 mm";
            g.drawString(str, x - fm.stringWidth(str) / 2, y - 25);
            x += dx * 20;
            g.drawLine(x, y = y0 + 10, x, y + 20);
            g.drawLine(x - 3, y + 5, x, y);
            g.drawLine(x + 3, y + 5, x, y);
            y += 20;
            str = "0." + String.valueOf(dx) + " mm";
            g.drawString(str, x - fm.stringWidth(str) / 2, y + chy + 15);
            x = x0 + xc + xx;
            g.drawLine(x, y0, x, y = y0 - 20);
            g.drawLine(x - 3, y + 5, x, y);
            g.drawLine(x + 3, y + 5, x, y);
            float f = (float) ((double) xx / 10D);
            str = String.valueOf(f);
            if (str.indexOf(".") == -1)
                str += ".0";
            str += " mm";
        } else {
            str = "?";
        }
        g.setColor(Color.black);
        g.drawString(str, x - xx / 2 - fm.stringWidth(str) / 2, Yb[3] + chy + 10);
        g.drawLine(x = Xb[3], y = Yb[3] + 5, x + xx, y);
        if (xx > 10) {
            g.drawLine(x + 5, y - 3, x, y);
            g.drawLine(x + 5, y + 3, x, y);
            x += xx;
            g.drawLine(x - 5, y - 3, x, y);
            g.drawLine(x - 5, y + 3, x, y);
        }
        g1.drawImage(gImage, 0, yOffset, this);
    }


}
Avatar billede arne_v Ekspert
10. oktober 2003 - 21:55 #4
I can not even read the page you link to.

I speak many languages including Fortran, Pascal, C, C++, Java, VB and
C#, But not french.

:-)

To me the description you gave me sounded as something very Windows
specific.
Avatar billede arne_v Ekspert
10. oktober 2003 - 21:56 #5
I can put in an answer, but maybe you should check up on Magoo's
answer - he is much better than me in such matters.
Avatar billede fredand Forsker
12. oktober 2003 - 18:03 #6
Hello Mates!

I have been looking around and I guess that to do something exactly like the calipers.exe I have to use the native libarys. I got the ide from the company that develops jniwrapper-package.

They have developed a own native lib (*.dll) and a Java package that they use.
You can take a look at the cool things at http://www.jniwrapper.com/index.jsp, enter the Online Demo in the menu. They actually got a window with a irregular shape.

They load a native lib with:
Library _kernel = new Library("kernel32");

Then they can access a function from the natives lib:
Function beep = _kernel.getFunction("Beep");

And invoke it:
UInt32 high = new UInt32(1000);
UInt32 durHigh = new UInt32(200);
beep.invoke(null, high, durHigh);

Pretty nice I must say.

So I start to look how to do this on my own, but of course I got stucked.
It looks like that we easy can load a native lib with:
Runtime runtime = Runtime.getRuntime();
runtime.load("C:/WINNT/system32/KERNEL32.DLL");

But how do we get hand on and use a function from it like the "Beep"?

I have understand that we need to do something like below, but I also guess that we need some C-code that maps to this?
public native static int beep();

Greatfull for any comments.

Fredrik
Avatar billede arne_v Ekspert
12. oktober 2003 - 18:09 #7
You need to:
  - write the Java stub
  - generate a .h file from that
  - write a .c/.cpp file that implements the methods in the .h file
    and call the Win32 API

Reason:
  JNI naming convention and Win32 API naming convention are not compatible
Avatar billede fredand Forsker
15. oktober 2003 - 14:45 #8
Hello mates!

I guess that I have to study some more, but now I know what to study!
Thanks
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester