Results 1 to 7 of 7
  1. #1
    fourpixel is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Unhappy Please help!JButtons

    hi guys i need help ..i need to put a JButton in my project....

    this must be the output of the program



    when you click the Day button the Moon will hide and the background will turn to white

    same as the Moon when you click it the the sun will hide and the background will turn to black...

    this is the code for the main class

    import javax.swing.JFrame;
    public class Graphics {
    public static void main(String[] args) {
    JFrame fr = new JFrame ("Drawing");
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr.setSize(400,400);
    fr.setVisible(true);

    Graphics2 sw = new Graphics2();
    fr.add(sw);

    }

    }

    and this is for the subclass

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.awt.Polygon;
    public class Graphics2 extends JPanel{
    public void paintComponent (Graphics g){
    super.paintComponent (g);
    this.setBackground (Color.BLACK);

    g.setColor (Color.BLUE);
    g.fillRect (30, 100, 90, 200);
    g.setColor(Color.WHITE);
    g.drawRect (29,98,90,200);

    g.setColor (Color.BLUE);
    g.fillRect (260, 100, 90, 200);
    g.setColor(Color.WHITE);
    g.drawRect(259, 99, 90, 200);

    g.setColor (Color.BLUE);
    g.fillRect (130, 210, 120, 90);
    g.setColor(Color.WHITE);
    g.drawRect(129, 209, 120, 90);

    g.setColor (Color.CYAN);
    g.fillRect (54, 110, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(53, 109, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (54, 170, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(53, 169, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (54, 230, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(53, 229, 40, 40);

    g.setColor (Color.RED);
    g.fillRect (165, 230, 50, 70);
    g.setColor(Color.WHITE);
    g.drawRect(164, 229, 50, 70);

    g.setColor (Color.CYAN);
    g.fillRect (285, 109, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(284, 108, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (285, 169, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(284, 168, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (285, 229, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(284, 228, 40, 40);

    g.setColor(Color.BLACK);
    g.fillOval (68, 110, 10,10);

    g.setColor(Color.BLACK);
    g.fillOval (68, 170, 10,10);

    g.setColor(Color.BLACK);
    g.fillOval (68, 230, 10,10);

    g.setColor(Color.BLACK);
    g.fillOval (180, 229, 15,15);

    g.setColor(Color.BLACK);
    g.fillOval (300, 109, 10, 10);

    g.setColor(Color.BLACK);
    g.fillOval (300, 169, 10, 10);

    g.setColor(Color.BLACK);
    g.fillOval (300, 229, 10, 10);

    g.setColor(Color.WHITE);
    g.fillOval (70, 30, 50, 50);

    g.setColor(Color.BLACK);
    g.fillOval (90, 25, 50, 50);

    g.setColor(Color.YELLOW);
    g.fillOval (250, 30, 50, 50);

    g.setColor(Color.YELLOW);
    g.drawLine(275, 100, 275, 10);

    g.setColor(Color.YELLOW);
    g.drawLine(325, 50, 220, 50);

    g.setColor(Color.YELLOW);
    g.drawLine(325, 90, 220, 20);

    g.setColor(Color.YELLOW);
    g.drawLine(325, 20, 220, 90);



    Polygon p = new Polygon();
    p.addPoint(185, 155);
    p.addPoint (270, 210);
    p.addPoint(115,210);
    g.setColor(Color.GRAY);
    g.fillPolygon(p);


    }

    }

    please help me ...i need to submit that tomorrow....
    Last edited by fourpixel; 09-29-2010 at 05:38 AM.

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

    Default

    What have you tried so far to solve this? Can we see your most recent code attempt? And what is the main thing that has you stumped? Also, when posting code, if you use code tags, your code will be more readable and more folks will read it. You can find out how by clicking on the link in my signature below.

    Much luck and welcome!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    when you click the Day button
    For anything to happen when you click on a button, you need to add a listener to the button that will be called when the button is clicked.

    Also comments in the code would help anyone reading it understand what each bit of the code does. The paintComponent method does a lot of drawing without describing what is being drawn at each step.

    There are too many hardcoded numbers used in the draw methods that will make your code un-maintainable. IE you won't easily be able to move the objects to another location without a lot of trail and error.

  4. #4
    fourpixel is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What have you tried so far to solve this? Can we see your most recent code attempt? And what is the main thing that has you stumped? Also, when posting code, if you use code tags, your code will be more readable and more folks will read it. You can find out how by clicking on the link in my signature below.

    Much luck and welcome!
    i haven't tried anything so far...i dont know what will i do next........

    Quote Originally Posted by Norm View Post
    For anything to happen when you click on a button, you need to add a listener to the button that will be called when the button is clicked.

    Also comments in the code would help anyone reading it understand what each bit of the code does. The paintComponent method does a lot of drawing without describing what is being drawn at each step.

    There are too many hardcoded numbers used in the draw methods that will make your code un-maintainable. IE you won't easily be able to move the objects to another location without a lot of trail and error.
    guys can you make me the code for the "Day" button i really don't know what will i do next ....then i'll study the codes for the "Night" button and the other buttons...btw im using eclipse helios...guys please help me :(

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    There are many sample pieces of code on this forum that use buttons. Search for ActionListener and you should get all you want.

  6. #6
    fourpixel is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    guys im done putting 1 JButton

    heres the code

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.awt.Polygon;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Event;

    public class Graphics2 extends JPanel{
    private JButton b1;
    private DayHandler dh;
    public Graphics2 (){
    add(b1 = new JButton ("Day"));
    dh = new DayHandler();
    b1.addActionListener(dh);

    }
    public void paintComponent (Graphics g){



    super.paintComponent (g);
    this.setBackground (Color.BLACK);


    g.setColor (Color.BLUE);
    g.fillRect (30, 100, 90, 200);
    g.setColor(Color.WHITE);
    g.drawRect (29,98,90,200);

    g.setColor (Color.BLUE);
    g.fillRect (260, 100, 90, 200);
    g.setColor(Color.WHITE);
    g.drawRect(259, 99, 90, 200);

    g.setColor (Color.BLUE);
    g.fillRect (130, 210, 120, 90);
    g.setColor(Color.WHITE);
    g.drawRect(129, 209, 120, 90);

    g.setColor (Color.CYAN);
    g.fillRect (54, 110, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(53, 109, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (54, 170, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(53, 169, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (54, 230, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(53, 229, 40, 40);

    g.setColor (Color.RED);
    g.fillRect (165, 230, 50, 70);
    g.setColor(Color.WHITE);
    g.drawRect(164, 229, 50, 70);

    g.setColor (Color.CYAN);
    g.fillRect (285, 109, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(284, 108, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (285, 169, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(284, 168, 40, 40);

    g.setColor (Color.CYAN);
    g.fillRect (285, 229, 40, 40);
    g.setColor(Color.WHITE);
    g.drawRect(284, 228, 40, 40);

    g.setColor(Color.BLACK);
    g.fillOval (68, 110, 10,10);

    g.setColor(Color.BLACK);
    g.fillOval (68, 170, 10,10);

    g.setColor(Color.BLACK);
    g.fillOval (68, 230, 10,10);

    g.setColor(Color.BLACK);
    g.fillOval (180, 229, 15,15);

    g.setColor(Color.BLACK);
    g.fillOval (300, 109, 10, 10);

    g.setColor(Color.BLACK);
    g.fillOval (300, 169, 10, 10);

    g.setColor(Color.BLACK);
    g.fillOval (300, 229, 10, 10);

    g.setColor(Color.WHITE);
    g.fillOval (70, 30, 50, 50);

    g.setColor(Color.BLACK);
    g.fillOval (90, 25, 50, 50);

    g.setColor(Color.YELLOW);
    g.fillOval (250, 30, 50, 50);

    g.setColor(Color.YELLOW);
    g.drawLine(275, 100, 275, 10);

    g.setColor(Color.YELLOW);
    g.drawLine(325, 50, 220, 50);

    g.setColor(Color.YELLOW);
    g.drawLine(325, 90, 220, 20);

    g.setColor(Color.YELLOW);
    g.drawLine(325, 20, 220, 90);

    g.setColor(Color.WHITE);
    g.drawString("Jasper John S. Cecilio", 230, 330);


    Polygon p = new Polygon();
    p.addPoint(185, 155);
    p.addPoint (270, 210);
    p.addPoint(115,210);
    g.setColor(Color.GRAY);
    g.fillPolygon(p);


    }
    public class DayHandler implements ActionListener {
    public void actionPerformed(ActionEvent e){



    }
    }

    }

    now my problem is what will i put in the ActionListener....
    to hide the moon and turn white the background
    can someone help me please :(

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    what will i put in the ActionListener.
    Your action listener will decide what is to be drawn, set some flags to tell the paintComponent method what to do and call repaint. The JVM will call the paintComponent method which will look at the instructions from the action listener and draw what the instructions say to draw. One way to pass instructions is by setting a boolean true:
    define a boolean drawMoon at the class level and set it false when the paintComponent method is NOT to draw the moon.

Similar Threads

  1. Help; Missing JButtons
    By Cyprusice in forum New To Java
    Replies: 3
    Last Post: 01-10-2010, 11:50 PM
  2. JButtons and If statements
    By calculuslord in forum New To Java
    Replies: 6
    Last Post: 10-20-2009, 10:52 PM
  3. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  4. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 02:49 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM

Posting Permissions

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