Results 1 to 7 of 7
Thread: Please help!JButtons
- 09-28-2010, 07:02 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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.
-
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!
- 09-28-2010, 01:48 PM #3
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.when you click the Day button
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.
- 09-28-2010, 01:58 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
i haven't tried anything so far...i dont know what will i do next........
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 :(
- 09-28-2010, 02:02 PM #5
There are many sample pieces of code on this forum that use buttons. Search for ActionListener and you should get all you want.
- 09-29-2010, 05:01 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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 :(
- 09-29-2010, 01:48 PM #7
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:what will i put in the ActionListener.
define a boolean drawMoon at the class level and set it false when the paintComponent method is NOT to draw the moon.
Similar Threads
-
Help; Missing JButtons
By Cyprusice in forum New To JavaReplies: 3Last Post: 01-10-2010, 11:50 PM -
JButtons and If statements
By calculuslord in forum New To JavaReplies: 6Last Post: 10-20-2009, 10:52 PM -
Help with JButtons...
By ashton in forum New To JavaReplies: 8Last Post: 01-26-2009, 09:38 AM -
JButtons
By jadaleus in forum Advanced JavaReplies: 4Last Post: 10-17-2008, 02:49 AM -
JButtons
By fgasimzade in forum SWT / JFaceReplies: 1Last Post: 12-25-2007, 05:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks