Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: WORKING BUT SOME FEATURES NOT

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question WORKING BUT SOME FEATURES NOT

    hello this is my first post i have a project to do but i have problems with the code
    the programm work without any error but i dont understand why some features doesnt work plz help

    so i have to make a programma that get an image and resize alling left right center have optrion button and can reset this is the code plz some help me.So if someone know the problem plz explain to me too im getting crazy when im running the programm i can reset it resize the image but i cannot aling left right center

    this is the code

    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import javax.swing.Icon.*;
    import java.lang.Object;
    import java.awt.image.BufferedImage;
    import java.awt.Image;
    import java.awt.BorderLayout;
    import java.awt.Graphics2D;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
     
    public class Image_Application extends JFrame
    {
        private MyPanel p1;
        private JPanel p2,p3;
        private JTextField t1, t2;
     
        private JButton alignL, alignC, alignR,resize;
     
        public Image_Application()
        {
            super("Image Application");
     
            p1=new MyPanel();
     
            Container c=getContentPane();
     
            c.add(p1, BorderLayout.CENTER);
     
            alignL = new JButton("Align Left");
            alignC = new JButton("Align Center");
            alignR = new JButton("Align Right");
            resize= new JButton("Resize");
     
            JPanel north = new JPanel();
            north.add(alignL);
            north.add(alignC);
            north.add(alignR);
            c.add(north, BorderLayout.NORTH);
     
            p2=new JPanel();
     
           // JPanel width = new JPanel("Width: ");
            t1=new JTextField(10);
            t1.setText(String.valueOf((int)p1.getImageIconDimension().getWidth()));
     
           // JPanel length = new JPanel("Heigth: ");
            t2=new JTextField(10);
            t2.setText(String.valueOf((int)p1.getImageIconDimension().getHeight()));
     
            p2.setLayout(new FlowLayout());
            p2.add(resize);
     
            c.add(p2, BorderLayout.SOUTH);
     
            p3=new JPanel();
     
            p3.setLayout(new GridLayout(2,1)); 
            p3.add(t1); 
            p3.add(t2);
     
            c.add(p3, BorderLayout.EAST);
     
             resize.addActionListener( new ActionListener()
             {
                public void actionPerformed(ActionEvent e)
                {
                  p1.scale(Integer.parseInt(t1.getText()),Integer.parseInt(t2.getText()));
                }
             });
     
     
    alignL.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent a)
            {
                p1.setLayout(new FlowLayout(FlowLayout.LEFT));
                p1.validate();
            }
    });
     
        alignR.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                p1.setLayout(new FlowLayout(FlowLayout.RIGHT));
                p1.validate();
            }
    });
     
     
    alignC.addActionListener( new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(p1, BorderLayout.CENTER);
            p1.validate();
        }
    });
     
            JMenuBar mb=new JMenuBar();
            JMenu m=new JMenu("Options");
            JMenuItem mi1=new JMenuItem("Resize");
            mi1.addActionListener(new ActionListener() {
     
            public void actionPerformed(ActionEvent e){
     
                p1.scale(Integer.parseInt(t1.getText()),Integer.parseInt(t2.getText()));
            }
        });
     
     
        JMenuItem mi2=new JMenuItem("Reset");
        mi2.addActionListener(new ActionListener()
        {
     
            public void actionPerformed(ActionEvent e)
            {p1.setOriginalSize();}    
        }
     
        );
     
        m.add(mi1); 
        m.add(mi2); 
        mb.add(m); 
        setJMenuBar(mb);
        setSize(400,300); 
        setVisible(true);
     }
     
     
    public static void main(String args[])
      {
        Image_Application a=new Image_Application();
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    } 
     
    class MyPanel extends JPanel 
    {
        private ImageIcon ic;
     
        public MyPanel()
        { setOriginalSize();}
     
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            ic.paintIcon(this,g,0,0);
        }
     
        public void scale(int w, int h)
        {
            ic=new ImageIcon(ic.getImage().getScaledInstance(w,h,Image.SCALE_FAST));
            repaint();
        }
     
        public void setOriginalSize()
        {
            ic=new ImageIcon("tulips.jpg");
            repaint();
        }
     
        public Dimension getImageIconDimension()
        {
            return new Dimension(ic.getIconWidth(), ic.getIconHeight());    }
    }
    Last edited by kariolos; May 25th, 2011 at 09:31 AM. Reason: forgot


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Please use code tags when posting your code. See BB Code List - Java Forums

    Use the # icon above the text input box

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    You create a panel p1, and set the LEFT and RIGHT alignment actions to align its contents, but you never put anything into it. I don't know what you're trying to do with the CENTER alignment action, but it's wrong.

    If you add something (e.g. a button or a label) to p1, you'll see the left and right buttons work as expected. You should be able to work the rest out yourself.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Quote Originally Posted by dlorde View Post
    You create a panel p1, and set the LEFT and RIGHT alignment actions to align its contents, but you never put anything into it. I don't know what you're trying to do with the CENTER alignment action, but it's wrong.

    If you add something (e.g. a button or a label) to p1, you'll see the left and right buttons work as expected. You should be able to work the rest out yourself.
    can you help how can i do it cause when i run the programma the buttons are as supposed to be plz if have the time correct my code plzzzz i want it for tomorrow morning and i havent any idea how

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Your code shows that you know how to add a component to a container.
    Create a new component, say a label with some text and add it to the container.

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Quote Originally Posted by Norm View Post
    Your code shows that you know how to add a component to a container.
    Create a new component, say a label with some text and add it to the container.
    but my p1 is the the panel look did you run the programm?
    p1 has the panel where the foto is and it should go right left or center when i press one off the buttons but something iswrong and my head is stuck i dont think so that i have to put another component but if can change it like you say and its better i ll try it but plz i dont what t make one change and all my programma then is wrong

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Look at the paintIcon method. It always puts the image at 0,0

  8. #8
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Quote Originally Posted by Norm View Post
    Look at the paintIcon method. It always puts the image at 0,0
    man srry but i have to go to sleep is there ant chance that you can help me do not give me hint plzz i looked it again and again i want something ready to understand and the 0,0 is beacuse when i resize the icon i want to be to 0,0 so plz im begging you can you tell me what should i change in the code??plzzzz

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Here is a hint. If you want the image somewhere besides the upper left corner, change the x,y location where it is being drawn.
    Further hints: add a Point to the MyPanel class and have the listeners change its x,y values for left, center and right.
    Last hint: call repaint

  10. #10
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Quote Originally Posted by Norm View Post
    Here is a hint. If you want the image somewhere besides the upper left corner, change the x,y location where it is being drawn.
    Further hints: add a Point to the MyPanel class and have the listeners change its x,y values for left, center and right.
    Last hint: call repaint
    so you cant just give me the code?im getting crazy here i dont know what to change the x,y, values dont have anything to do cause i want my picture change side when i press the button but its something wrong whith the button and the FLowLayout.Left but i donttttttttttttttttttttttttttt knwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww ?????!?!?!?!!?!?!?!!?!?!?!??!?!!?!?!?!?!!?!!?!?!!? |

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    If the code is for a homework assignment, I definitely can't give any student code.

  12. #12
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    yes but its almost ready i cant find any solution the hint you gave m seems to me fine and im considering giving up java

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Sorry, not everyone can do everything.

  14. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: WORKING BUT SOME FEATURES NOT

    so you cant just give me the code?
    Sorry, but it doesn't work like that. In fact, doing so is against the policy of the forums and anything considered as such is subject to moderation. We can help with leading you through the assignment, but not cheat. Norm has given you some valuable advice, I'd suggest studying that advice and working with your code the best you can.

  15. #15
    Junior Member
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    Quote Originally Posted by copeg View Post
    Sorry, but it doesn't work like that. In fact, doing so is against the policy of the forums and anything considered as such is subject to moderation. We can help with leading you through the assignment, but not cheat. Norm has given you some valuable advice, I'd suggest studying that advice and working with your code the best you can.
    yes but i want that code for tomorrow and im in a hurry i knw about the policy but i didnt want to cheat or something like that i followed his hints but everything i did end up in errors and i just doint have the time im not so googd programmer so i made the whole programm and i wantsed a fix nothing more i didnt giuve you my exercise and told you to solve i just dont have the time and its a same not to have it finished until 9 o clock.anyway thnx for the help

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    everything i did end up in errors
    When you have errors, post them and someone will help you fix them.

  17. #17
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: WORKING BUT SOME FEATURES NOT

    @kariolos - I told you already how to progress with this: put the components you want to be aligned into panel p1. You also need to change the center alignment action to be more like the left and right alignment.

    If you wrote this code in the first place, you should have no trouble fixing it with my suggestion. If you've left it too late, that's a lesson learned for the future.

Similar Threads

  1. [SOLVED] Working on Win 7 and not on XP
    By shaumux in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2011, 05:36 PM
  2. new features in bug tracking tools
    By ssiklib in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 02:34 AM
  3. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  4. CVS features in Eclipse
    By bbr201 in forum Java IDEs
    Replies: 5
    Last Post: August 11th, 2010, 06:12 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM