Need Urgent Java Coding help

amila325

Well-known member
  • Jul 11, 2006
    9,188
    33
    48
    rochel1977 said:
    then amila can u add that label and send it to me the full code? thx bro i am realy stuck here. what adjustment i do all go wrong. pls machan.
    dude you need to change the color green to a image r8??
     

    rochel1977

    Well-known member
  • May 15, 2006
    11,466
    2,673
    113
    AKL, NZ and Kandy
    amila325 said:
    hihi thanx man....

    rochel there is another way u will hv to override da paint method in JPanel class inside a class of your own...

    Code:
    class MyPanel extends JPanel {
    
     private Image img;
    
     public MyPanel(Image img) {
       this.img = img;
       //do what ever u need to do in the jpanel layouts and everything
     }
    
      public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);
      }
    }
    after this you can use this class as a customized JPanel

    appa machan I didn't understand what you said. all these are complicated machan. can u pls include the code and get the out put that i need amila?? thx
     

    amila325

    Well-known member
  • Jul 11, 2006
    9,188
    33
    48
    rochel1977 said:
    machan what i want is when i click the last row of radio buttons it should display the text with the pictures as well.
    ya im on that this picture wont come up ne :P :P
     

    amila325

    Well-known member
  • Jul 11, 2006
    9,188
    33
    48
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class QuoteGUI extends JFrame {
    
        public static void main(String arg[]) {
            final Image fstImg = new ImageIcon("C:/bold.jpg").getImage();
            final Image secImg = new ImageIcon("iamges/italic.jpg").getImage();
            final Image thrImg = new ImageIcon("C:/italic.jpg").getImage();
            final JLabel quote = new JLabel("Malaysian Boleh!");
            final MyPanel pquote = new MyPanel(fstImg);
            JPanel pradio = new JPanel();
    
            
    
            
            quote.setFont(new Font("Helvetica", Font.PLAIN, 24));
    
    
            final ButtonGroup group = new ButtonGroup();
    
            JRadioButton mahatir = new JRadioButton("Dr.Mahatir", true);
            JRadioButton paklah = new JRadioButton("Pak Lah", false);
            JRadioButton pckang = new JRadioButton("Phua Chu Kang", false);
    
    
    
            //pquote.setBackground(Color.green);
            pradio.setBackground(Color.yellow);
    
            group.add(mahatir);
            group.add(paklah);
            group.add(pckang);
    
            pradio.add(mahatir);
            pradio.add(paklah);
            pradio.add(pckang);
    
            JCheckBox bold = new JCheckBox("BOLD");
            JCheckBox italic = new JCheckBox("ITALIC");
    
    
    
            class VoteItemListener implements ItemListener {
    
                public void itemStateChanged(ItemEvent ex) {
                    String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand();
                    boolean selected = (ex.getStateChange() == ItemEvent.SELECTED);
                    if (selected) {
                        if (item.equals("Dr.Mahatir")) {
                            quote.setText("Malaysian Boleh!");
                            pquote.setImage(fstImg);
                        } else if (item.equals("Pak Lah")) {
                            quote.setText("Work with me: )");
                            pquote.setImage(secImg);
                        } else if (item.equals("Phua Chu Kang")) {
                            quote.setText("Don't play-play!");
                            pquote.setImage(thrImg);
                        }
                    }
                }
            }
    
            ItemListener itemListener = new ItemListener() {
    
                public void itemStateChanged(ItemEvent itemEvent) {
                    AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
                    int state = itemEvent.getStateChange();
                    if (state == ItemEvent.SELECTED) {
                        quote.setFont(quote.getFont().deriveFont(quote.getFont().getStyle() + Font.ITALIC));
                    } else {
                        quote.setFont(quote.getFont().deriveFont(Font.PLAIN));
                    }
                }
            };
            ItemListener itemListenerBold = new ItemListener() {
    
                public void itemStateChanged(ItemEvent itemEvent) {
                    AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
                    int state = itemEvent.getStateChange();
                    if (state == ItemEvent.SELECTED) {
                        quote.setFont(quote.getFont().deriveFont(quote.getFont().getStyle() + Font.BOLD));
                    } else {
                        quote.setFont(quote.getFont().deriveFont(Font.PLAIN));
                    }
                }
            };
            bold.addItemListener(itemListenerBold);
            italic.addItemListener(itemListener);
    
            pquote.add(quote);
            pquote.add(bold);
            pquote.add(italic);
    
    
            ItemListener al = new VoteItemListener();
            mahatir.addItemListener(al);
            paklah.addItemListener(al);
            pckang.addItemListener(al);
    
            JFrame frame = new JFrame("Quote");
            frame.setLayout(new BorderLayout());
    
    
    
            frame.getContentPane().add(pquote, BorderLayout.CENTER);
            frame.add(pradio, BorderLayout.PAGE_END);
            frame.setSize(500, 170);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        }
    }
    
    class MyPanel extends JPanel {
    
        private Image img;
        private Color c = Color.GREEN;;
    
        public MyPanel(Image img) {
            this.img = img;
        //do what ever u need to do in the jpanel layouts and everything
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                g.drawImage(img, 0, 0, null);
            } else {
                setBackground(c);
            }
        }
    
        public void setImage(Image img){
            this.img = img;
            repaint();
        }
    }

    changed to ur requirement :P :P...
     
    Last edited:

    amila325

    Well-known member
  • Jul 11, 2006
    9,188
    33
    48
    rochel1977 said:
    machan pics are with me. hehe. u just add a pic from ur pc and see machan. :P
    LOL im not that bad :P
    thing is it doesnt work with relative path :( :(.... i dont knw why... it worked with absolute path.
    Well this is not exactly what u need i think...
    but u'll b able to do it from here without a prob nw, I think :)
     

    rochel1977

    Well-known member
  • May 15, 2006
    11,466
    2,673
    113
    AKL, NZ and Kandy
    amila325 said:
    LOL im not that bad :P
    thing is it doesnt work with relative path :( :(.... i dont knw why... it worked with absolute path.
    Well this is not exactly what u need i think...
    but u'll b able to do it from here without a prob nw, I think :)

    oh is it? any way i will give it a try and see machan. thx alot for ur help. btw do u know how to center an icon image??

    i got this code where the pic is in the left hand side. i need it to be in the center

    Code:
    JPanel panel = new JPanel();	
    ImageIcon icon = new ImageIcon("logo.gif");	
    JLabel l1 = new JLabel();
    l1.setIcon(icon);
    panel.add(l1);
     

    amila325

    Well-known member
  • Jul 11, 2006
    9,188
    33
    48
    rochel1977 said:
    oh is it? any way i will give it a try and see machan. thx alot for ur help. btw do u know how to center an icon image??

    i got this code where the pic is in the left hand side. i need it to be in the center

    Code:
    JPanel panel = new JPanel();	
    ImageIcon icon = new ImageIcon("logo.gif");	
    JLabel l1 = new JLabel();
    l1.setIcon(icon);
    panel.add(l1);
    Code:
    JPanel panel = new JPanel();	
    ImageIcon icon = new ImageIcon("logo.gif");	
    JLabel l1 = new JLabel("",icon,JLabel.CENTER);
    panel.add(l1);
    Well this will work... i think :)
     

    rochel1977

    Well-known member
  • May 15, 2006
    11,466
    2,673
    113
    AKL, NZ and Kandy
    amila325 said:
    Code:
    JPanel panel = new JPanel();	
    ImageIcon icon = new ImageIcon("logo.gif");	
    JLabel l1 = new JLabel("",icon,JLabel.CENTER);
    panel.add(l1);
    Well this will work... i think :)

    no macho. its not working. mekata flowlayout or borderlayout dalada handa one?
     

    amila325

    Well-known member
  • Jul 11, 2006
    9,188
    33
    48
    rochel1977 said:
    no macho. its not working. mekata flowlayout or borderlayout dalada handa one?
    it's working for me :S :S and i used this method also
    Code:
    import java.awt.BorderLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Amila
     */
    public class LableClass extends JFrame{
        public static void main(String[] args) {
            JFrame frame = new JFrame("Quote");
            frame.setLayout(new BorderLayout());
            JPanel panel = new JPanel();
            ImageIcon icon = new ImageIcon("C:/bold.jpg");
            JLabel l1 = new JLabel();
            l1.setHorizontalAlignment(JLabel.CENTER);
            l1.setIcon(icon);
            panel.add(l1);
            frame.add(panel,BorderLayout.PAGE_START);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
     

    rochel1977

    Well-known member
  • May 15, 2006
    11,466
    2,673
    113
    AKL, NZ and Kandy
    amila325 said:
    it's working for me :S :S and i used this method also
    Code:
    import java.awt.BorderLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Amila
     */
    public class LableClass extends JFrame{
        public static void main(String[] args) {
            JFrame frame = new JFrame("Quote");
            frame.setLayout(new BorderLayout());
            JPanel panel = new JPanel();
            ImageIcon icon = new ImageIcon("C:/bold.jpg");
            JLabel l1 = new JLabel();
            l1.setHorizontalAlignment(JLabel.CENTER);
            l1.setIcon(icon);
            panel.add(l1);
            frame.add(panel,BorderLayout.PAGE_START);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }

    ah ok elaz. :P