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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-08-2003, 11:04 AM
|
#1
|
Member
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344
Rep:
|
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.
|
|
|
10-08-2003, 11:51 AM
|
#2
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
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.
|
|
|
10-08-2003, 03:33 PM
|
#3
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
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]);
}
}
|
|
|
10-08-2003, 06:57 PM
|
#4
|
Member
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344
Original Poster
Rep:
|
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!
|
|
|
10-08-2003, 07:29 PM
|
#5
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
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.
|
|
|
10-08-2003, 07:58 PM
|
#6
|
Member
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 11:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|