Results 1 to 3 of 3
Thread: swing mixed with awt graphics
- 08-17-2009, 09:50 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
swing mixed with awt graphics
HI all,
First i am limited in placing code Ill do my best to explain what is missing. Thanks
I am mixing the graphic interface similar to Goole maps, currently I am working on a pan zoom feature that zooms in on multiple map images. I do not know what the graphics are at this time I have been using gif and jpg. I have placed my graphics on the JFrame layer
and used LayeredPanes to paste the JSlider.Vertical bar. This is where i get stumped... with out the main method except for testing, how would i put the JSlider on a floating pane where it is not stuck on the frame layer I messed my LayeredPane up some how.
this is code I have been working with to get functionality.
Thanks in advance for clues, tips, and direction...Java Code:public class MapScale extends JPanel { BufferedImage image; double scale = 1.0; public MapScale(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); double x = (getWidth() - scale*image.getWidth())/2; double y = (getHeight() - scale*image.getHeight())/2; AffineTransform at = AffineTransform.getTranslateInstance(x,y); at.scale(scale, scale); g2.drawRenderedImage(image, at); } public Dimension getPreferredSize() { int w = (int)(scale*image.getWidth()); int h = (int)(scale*image.getHeight()); return new Dimension(w, h); } private JSlider getSlider() { int min = 1, max = 41, inc = 5; final JSlider slider = new JSlider(JSlider.VERTICAL,min, max, 20); slider.setMajorTickSpacing(5); slider.setMinorTickSpacing(1); slider.setPaintTicks(true); slider.setSnapToTicks(true); slider.setLabelTable(getLabelTable(min, max, inc)); slider.setPaintLabels(true); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int value = slider.getValue(); System.out.println(value + "this is the value of the value slider "); // I want a more mag in the zoom scale = (value+4)/5.0; revalidate(); repaint(); } }); return slider; } private Hashtable getLabelTable(int min, int max, int inc) { Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>(); for(int j = min; j <= max; j += inc) { String s = String.format("%.2f", (j+4)/20.0); System.out.println(s +" this is the format"); table.put(Integer.valueOf(j), new JLabel(s)); } return table; } public static void main(String[] args) throws IOException { String path = "images/WBD_world-map.jpg"; BufferedImage image = ImageIO.read(new File(path)); MapScale test = new MapScale(image); test.add(test.getSlider());// this adds to the map JLayeredPane lPane = new JLayeredPane(); lPane.setPreferredSize(new Dimension(100,300)); lPane.setLocation(10, 10); lPane.setBorder(BorderFactory.createEmptyBorder()); Container cp = new Container(); cp.add(test.getSlider()); lPane.add(cp); lPane.setVisible(true); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(test)); // f.getContentPane().add(test.getSlider()); // f.getContentPane().add(test.getSlider(), "Last"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } }
- 08-31-2009, 04:41 AM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
without the main method
how would i put the JSlider on a floating pane
If I understand the situation, your application will be a single Component that displays a map in a JScrollPane. If so, you may be able to solve the problem by having the JScrollPane be the single Component.
where it is not stuck on the frame layer
This suggests that the Component will have two parts, the JSlider and the JScrollPane. You can combine these into a single JPanel. Set its LayoutManager to a BorderLayout and insert the map in BorderLayout.CENTER and the slider in BorderLayout.LEADING, or the like.
Here's an (untested) sketch:
The value to insert as the Component is the value returned by createMapViewer.Java Code:Component createMapViewer () { MapScale test = new MapScale(image); JScrollPane scroll = new JScrollPane(test); JSlider slider = ...; JPanel viewer = new JPanel(); viewer.setLayout(new BorderLayout()); viewer.add(slider, BorderLayout.LEADING); viewer.add(scroll, BorderLayout.CENTER; return viewer; }
}
- 08-31-2009, 08:01 AM #3
Similar Threads
-
Help me with graphics
By 7oclock in forum New To JavaReplies: 12Last Post: 04-04-2009, 11:20 PM -
Help with 2d graphics please
By xbox_nutter in forum New To JavaReplies: 0Last Post: 04-02-2009, 11:48 AM -
Mixed language project?
By toadaly in forum NetBeansReplies: 5Last Post: 03-11-2009, 06:29 AM -
SWT Graphics Example
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:28 PM -
Graphics
By feniger in forum New To JavaReplies: 1Last Post: 12-29-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks