LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2003, 11:04 AM   #1
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Rep: Reputation: 30
Java advice needed


I'm doing a project where an overhead view of a schematic can be zoomed in on, depending where the user clicks.

I'm doing this to learn more about making interactive GUIs, and to add a new aspect to a console app I want to develop. My question is: should I use Java2D /3D or what?

The schematic can be drawn in 2D, but as it has to be zoomed in on, does this introduce a third axis that requires 3D? I have never done any graphics programming in java, just the basic gui stuff.

I want this to be fairly fancy, eg a room on the schematic that currnetly has the mouse over it, will illuminate slightly.

Any insight is appreciated.
 
Old 10-08-2003, 11:51 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Never used graphics much myself but I know from when learning that Image class allows you to scale up or down an image. There is also another class CropImageFilter that creates a new image cut/cropped from an area in an existing image. So I suppose you could get where the mouse clicked crop an area of the image based on whatever criteria then scale the new image up to whatever size you want which would give the effect of zooming in. There is image filtering available to in the 2D api which I suppose could create a highlight effect but seeing as I can barely draw a matchstick man I haven't looked at all at any of it.

Thats a suggestion anyway, probably better ones...but the ball is rolling.
 
Old 10-08-2003, 03:33 PM   #3
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Just for the hell of it I did a test myself. This takes a picture as a command line argument, loads it resizing it to the size of the frame

left click - zooms into the centre of the picture unless more than likely my arithmetic is wrong

right click - zooms back out to original size

x1,y1,x2,y2 refer to area of picture to be zoomed into (could be any part of the picture)
and the other drawImage() variables resize/scale the picture relative to the current size of the frame.

Overall shouldn't be too hard to accomplish what you want,

If you want to, try it out to see what I'm meaning

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

public class ImageTest extends JFrame
{
  private Image myImage;
  private Insets insets;
  private int x1,y1,x2,y2;
  private boolean zoomed;

  public ImageTest(String im)
  {
     loadImage(im);


    this.setSize(600,600);

    insets=getInsets();

     zoomed=false;

     x1=0;
     y1=0;
     x2=myImage.getWidth(this); //get the actual width of image 
     y2=myImage.getHeight(this); // get the actual height of image




   ComponentListener listen=new ComponentAdapter()    // listener for window resizing
                     {
                       public void componentResized(ComponentEvent e)
                         {
                               repaint();

                           }

                      };


   MouseListener mouseListen=new MouseAdapter()    // mouse clicked listener
                      {
                       public void mouseClicked(MouseEvent me)
                         {

                        if(( (me.getModifiers() & InputEvent.BUTTON1_MASK)!=0) & (! zoomed )) //left button on three button mouse 
                          {
                             zoomIn();

                          }

                          if(( (me.getModifiers() & InputEvent.BUTTON3_MASK)!=0) & (zoomed )) //right button on three button mouse
                           {
                             zoomOut();

                           }
                        }

                      };



     this.addComponentListener(listen); //add listener for window resize event
     this.addMouseListener(mouseListen); //add listener for mouse event     

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.show();

   }

  private void zoomIn()
  {

   x1=myImage.getWidth(this)/2;
   x1/=2;
   y1=myImage.getHeight(this)/2;
   y1/=2;

   x2=(myImage.getWidth(this)/2)+x1;
   y2=(myImage.getHeight(this)/2)+y1;

   zoomed=true;

   repaint();
  }


  private void zoomOut()
  {
    x1=y1=0;
    x2=myImage.getWidth(this);
    y2=myImage.getHeight(this);

    zoomed=false;

    repaint();
  }

  private void loadImage(String im)
  {
    ImageIcon iconImage=new ImageIcon(im);
    myImage=iconImage.getImage();
   }




  public void paint(Graphics g)
  {
    super.paint(g);

    g.drawImage(myImage, insets.left, insets.top, this.getWidth(), this.getHeight(),x1 ,y1, x2, y2,this);

   }                               //x1,y1,x2,y2  coordinates representing an  area in  the picture relative to the picture not the frame holding it                               
         
                                  // this.getWidth() this.getHeight for scaling the picture relative to the frame



public static void main (String args[])
{
  ImageTest test=new ImageTest(args[0]);

}

}
 
Old 10-08-2003, 06:57 PM   #4
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
Looking_Lost, you make that look deceptively easy. Thats great, I can just use an image of the schematic. It will have to be hi res of course, and the monitor sizes may vary, so I'll have to take that into consideration.

I could supply images for say 10 common resolutions, and on startup, the program will choose the correct one.

If you can reccomend a good book for graphics programming (I already have Deitel and Deitels Java Books, as well as Sun Microsystems 'Core Java'), I'd appreciate that too. Knowing where to look is half the battle as they say.

Thanks again, that was an eye opener!
 
Old 10-08-2003, 07:29 PM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I've been messing about further with it and can at least get a rectangle to appear over specificed areas of the picture when the mouse goes over it, the toughest bit I'm finding is the good old maths, i.e the ratio to the actual size of the area of the picture comapre with the mouse co-ords and when the windows is resized and how it affects it all using only one picture.

I've a horrible feeling I'm trying to reinvent the wheel here and there is a premade class somewhere for imagemapping..but until I find it brute force will prevail

Only book I've got is Mastering Java 2 John Zukowski which while not comprehensive is usually good for pointing you in the right direction.

Maybe somebody will come along who is a bit more in the know about graphics and give you perhaps better pointers.
 
Old 10-08-2003, 07:58 PM   #6
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Original Poster
Rep: Reputation: 30
The algoritm side of things is actually my stronger point. We did all console apps including dsp apps and ASCII games! But I think that even though the idea of Java is that everything is technically done for you, it isn't always worth the search and research, when you can implement it yourself.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
advice needed bshearer *BSD 2 04-18-2005 12:19 PM
Partioning Advice Needed pembo13 Linux - General 3 11-06-2003 05:48 PM
advice needed ezra143 Linux - Laptop and Netbook 1 08-13-2003 02:44 PM
Upgrading advice needed jester_69 Linux - General 2 09-19-2002 08:39 AM
Major advice needed TimRPH Linux - Newbie 9 07-02-2001 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration