Results 1 to 11 of 11
  1. #1
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default how to scale correctly ?

    I have small program like this.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    import java.awt.image.*;
    
    import javax.imageio.*;
    import java.awt.geom.*;
    import java.util.*;
    
    public class Test {
    	public static void main(String[] args) {
    		new Test();
    	}
    	
    	Test() {
    		JFrame window = new JFrame();
    		window.setSize(new Dimension(500,500));
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
    		JComponent p = new Draw();
    		
    		window.add(p);		
    		window.setVisible(true);
    	}
    }
    
    class Draw extends JComponent {
    	
    	BufferedImage bgImage=null;
    	Graphics2D g2i;
    	Dimension tool;
    	
    	Draw() {
    		tool = Toolkit.getDefaultToolkit().getScreenSize().getSize();
    		bgImage = new BufferedImage((int)tool.getWidth(), (int)tool.getHeight(), BufferedImage.TYPE_INT_ARGB);
    		g2i = bgImage.createGraphics();		
    		
    		setBackground(Color.white);
    	}
    	
    	public void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D)g;
    		super.paintComponent(g2);
    				
    		drawRec();
    		Scale();
    				
    		if( bgImage !=null ) g2.drawImage(bgImage,null,0,0);
    	}
    	
    	private void drawRec() {
    		g2i.drawRect(50,50,70,50);
    		repaint();
    	}
    	
    	private void Scale() {
    		g2i.scale(2,2);
    		repaint();
    	}
    }
    I don't know what is happening. My purpose is to scale larger image in bufferedImage . I have one rectangle and after scaled i would have one larger rectangle but ... help me with this problem ! thank in advance
    Last edited by h9h; 10-23-2009 at 05:50 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    After 13 posts you should know by now to use the "Code" tags.

    I believe you should be scaling the Graphics object in the paintComponent() method before you paint the image.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please
    1) Edit your post to ad code-tags (please see my signature below), and
    2) Describe in a bit more detail just what isn't working.

    Thanks and best of luck!

  4. #4
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    thank ! i will be carefull next time. Please consider my situation because when i use Graphics2D context it works well, but i use Graphics2D context from BufferedImage it always keep the old rectangle and scale new ones.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by h9h View Post
    thank ! i will be carefull next time.
    There's no need for there to be a next time as you can fix it right now. All you have to do is edit your first post and add code tags. If you want folks to read your code and help you, you're best bet is to make it readable.

    Again, much luck.
    Last edited by Fubarable; 10-23-2009 at 05:32 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    OK, I guess that it's just not that important to him....
    Ok , sorry . it's nice now. You are very ....

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So have you tried camickr's suggestion?
    Java Code:
       public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          super.paintComponent(g2);
    
          // drawRec(g2); // if you want to see what it looks like pre scaling
          scale(g2);
          drawRec(g2);
          //Scale(g2);
          if (bgImage != null) g2.drawImage(bgImage, null, 0, 0);
       }
    
       private void drawRec(Graphics2D g2) {
          g2.drawRect(50, 50, 70, 50);
          //g2i.drawRect(50, 50, 70, 50);
          //repaint();
       }
    
       private void scale(Graphics2D g2) {
          //g2i.scale(2, 2);
          //repaint();
          g2.scale(2, 2);
       }

  9. #9
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    So have you tried camickr's suggestion?
    Java Code:
       public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          super.paintComponent(g2);
    
          // drawRec(g2); // if you want to see what it looks like pre scaling
          scale(g2);
          drawRec(g2);
          //Scale(g2);
          if (bgImage != null) g2.drawImage(bgImage, null, 0, 0);
       }
    
       private void drawRec(Graphics2D g2) {
          g2.drawRect(50, 50, 70, 50);
          //g2i.drawRect(50, 50, 70, 50);
          //repaint();
       }
    
       private void scale(Graphics2D g2) {
          //g2i.scale(2, 2);
          //repaint();
          g2.scale(2, 2);
       }
    I think the camickr's suggestion works but only without using BufferedImage. My program uses BufferedImage so i have to find other ways . Let's continue to discuss this problem ...

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Some remarks:
    Java Code:
    public void paintComponent(Graphics g) {
        ...
        // These method calls inside this method body result
        // in calls to [i]repaint[/i] which result in more
        // calls to this method which results in an endless
        // loop. To demonstrate this to yourself add a println
        // statement in this method.
        drawRec();
        Scale();
        ...
    }
    
    private void drawRec() {
        ...
        // tells java to draw this component (Draw) again
        // which results in a call to [i]paintComponent[/i]
        repaint();
    }
    
    private void Scale() {
        ...
        // tells java to draw this component (Draw) again
        // which results in a call to [i]paintComponent[/i]
        repaint();
    }
    To avoid an endless loop keep your drawing and event code stricly separate.
    The calls to Scale should take place in event code, not in any painting code.

    An example:
    Here's one way to scale a BufferedImage and draw it.
    It shows one way to isolate the event code from the drawing code and how to get them to work together.
    Java Code:
      
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class ScaleTest extends JPanel {
        Dimension size = new Dimension(400, 400);
        Point loc = new Point(50, 50);
        BufferedImage image;
    
        public ScaleTest() {
            int w = 100, h = 100;
            int type = BufferedImage.TYPE_INT_RGB;
            image = new BufferedImage(w, h, type);
            Graphics2D g2 = image.createGraphics();
            g2.setBackground(Color.white);
            g2.clearRect(0,0,w,h);
            g2.setPaint(Color.blue);
            g2.drawRect(0,0,w-1,h-1);
            g2.setPaint(Color.red);
            g2.drawRect(25,25,50,50);
            g2.dispose();
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.drawImage(image, loc.x, loc.y, this);
        }
    
        public Dimension getPreferredSize() {
            return size;
        }
    
        private void scaleUp() {
            double scale = 1.25;
            int w = (int)(scale*image.getWidth());
            int h = (int)(scale*image.getHeight());
            BufferedImage scaled = new BufferedImage(w, h, image.getType());
            Graphics2D g2 = scaled.createGraphics();
            g2.drawImage(image, 0, 0, w, h, this);
            g2.dispose();
            image = scaled;
            size.setSize(w+2*loc.x, h+2*loc.y);
        }
    
        private JPanel getUIPanel() {
            JButton button = new JButton("scale up");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    scaleUp();
                    repaint();
                    revalidate();
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) {
            ScaleTest test = new ScaleTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new JScrollPane(test));
            f.add(test.getUIPanel(), "Last");
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  11. #11
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

Similar Threads

  1. The scale() method
    By IamKira in forum Java 2D
    Replies: 0
    Last Post: 07-15-2009, 03:49 PM
  2. How to use Scale in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:49 PM
  3. Transform Scale
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:53 PM
  4. Scale 2 or more pictures using a JSlider
    By Panchitopro in forum AWT / Swing
    Replies: 4
    Last Post: 05-20-2008, 04:44 PM
  5. Scale 2 or more pictures using a JSlider
    By Panchitopro in forum New To Java
    Replies: 0
    Last Post: 05-05-2008, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •