Results 1 to 4 of 4
Thread: BorderLayout problem
- 05-26-2010, 09:45 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
BorderLayout problem
Hello
Im making a program where i have to make something with lights and borderlayout.
I have 3 classes.
1: Light
2: Partylights
3: Frame, the main-class
I want to make a arraylist in de partylights class, where the arraylist will be filled with objects comin out of the Light class. The problem is that there wont draw something in the layout. I placed my code of the class under.
I want to make a line of lights who can blink and have 4 different colors. How do i fill this arrayList and how can i accomplish that the light will be drawn?
Light:
PartyLights:Java Code:package Lights; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Light extends JPanel implements ActionListener { Color kleur; boolean aan = true; //If light is on boolean knipperend = false; //if light is blinkin Timer blinkinLight; int PosY, PosX, size; public Lampje(int PosX, int PosY, int size, Color kleur) { this.kleur = kleur; this.PosX = PosX; this.PosY = PosY; this.size = size; Timer blinkinLight = new Timer(200, null); blinkinLight.start(); } public void paintComponent(Graphics g) { if(aan == true) { super.paintComponent(g); g.setColor(kleur); g.fillOval(PosX, PosY, grootte, grootte); } if(aan == false) { super.paintComponent(g); g.setColor(Color.BLACK); g.fillOval(PosX, PosY, grootte, grootte); } repaint(); }
Java Code:package Lights; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Partylights extends JPanel implements KeyListener { ArrayList<Light> lights; Light light; public Partylights() { setRequestFocusEnabled(true); lights = new ArrayList<Light>(); } public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.BLACK); repaint(); }
-
Some problems here:
You'll need an ActionListener in place of that null. An anonymous inner class should work well here.Java Code:Timer blinkinLight = new Timer(200, [b][color="red"]null[/color][/b]); // ???? null
You never want to call repaint in a paintComponent method as it doesn't make sense and carries a risk for circular logic. Myself, I'd call repaint in the Timer's ActionListener that you'll need to write. Get rid of if (xxx == true) or if (yyy == false). Instead use if (xxx) or if (!yyy), and in your case, the second if can be replace by an else.Java Code:public void paintComponent(Graphics g) { if(aan == true) // replace with if (aan) { super.paintComponent(g); g.setColor(kleur); g.fillOval(PosX, PosY, grootte, grootte); } if(aan == false) // just an else will do here { super.paintComponent(g); g.setColor(Color.BLACK); g.fillOval(PosX, PosY, grootte, grootte); } repaint(); // ??? you never want repaint called in a paintComponent method. }
don't call setBackground in a paintComponent but rather do this in the class's constructor. Again, get rid of repaint() in the paintComponent method.Java Code:public class Partylights extends JPanel implements KeyListener { ArrayList<Light> lights; Light light; public Partylights() { setRequestFocusEnabled(true); lights = new ArrayList<Light>(); } public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.BLACK); // don't call this in paintComponent repaint(); // ??? again, a bad idea }
For your last question, you fill the ArrayList by creating Light objects and adding them to the ArrayList (though not sure why you need an ArrayList to be honest). You'll also need to add the created Light objects to your GUI, probably the Partylights panel, somewhere. How you do this will depend on much that you haven't told us such as just what is this GUI supposed to look like.
Much luck!
- 05-27-2010, 10:03 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
Well, thanks for the reply. Some of these fails were made because of the hurry.
I had some sort of idea how to fill the ArrayList for PartyLights.
PartyLights class:
Im running in some problems here. First I dont know what to do with the filling of the ArrayList. I want to use 4 colors, I think that is best to fill an ArrayList with these colors and then get those colors from that ArrayList. But I dont know how to repeat an ArrayList so that these colors will be used over and over again. Then again, i also run in another problem.Java Code:package Lights; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class PartyLights extends JPanel implements KeyListener { ArrayList<Light> lights; Light light; ArrayList<Color> colorLight; public PartyLights() { setRequestFocusEnabled(true); lights = new ArrayList<Light>(); setBackground(Color.BLACK); for(int i = 0; i<????; i++) { lampjes.add(new Lampje(i*20, 3, 20, ????)) } } public void paintComponent(Graphics g) { super.paintComponent(g); for(int i = 0; i< lights.size(); i++) { lights.get(i); } }
I want to use a borderLayout and use 3 instances of PartyLights and then get those 3 instances; west, north and east. But how do I draw those Lights that in all of these instances get these lights. I posted a screen to make things more clear.

The PartyLights are as you can see, in the north, east and west....
Here is the class Light again...
Light:
Java Code:package Lights; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Light extends JPanel implements ActionListener { Color color; boolean aan = true; //if light is on boolean knipperend = false; //If light is blinkin Timer knipperLicht; int PosY, PosX, grootte; public Lampje(int PosX, int PosY, int grootte, Color kleur) { this.kleur = kleur; this.PosX = PosX; this.PosY = PosY; this.grootte = grootte; Timer knipperLicht = new Timer(200, this); knipperLicht.start(); } public void paintComponent(Graphics g) { if(aan) { super.paintComponent(g); g.setColor(kleur); g.fillOval(PosX, PosY, grootte, grootte); } else { super.paintComponent(g); g.setColor(Color.BLACK); g.fillOval(PosX, PosY, grootte, grootte); } }Last edited by TGH; 05-27-2010 at 03:49 PM.
-
The mod operator can help you here. Inside of whatever loop you're using to create your lights, mod the index you're using by the size of the List. e.g.,
Java Code:String[] strings = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; List<String> stringList = Arrays.asList(strings); for (int i = 0; i < 20; i++) { System.out.println(stringList.get(i % stringList.size())); }
Not sure what you're having trouble with. You could put your lights into JPanels that use GridLayout(1, 0) or GridLayout(0, 1), depending on the orientation of the lights in the JPanels. I'd use the size of the image and the size of the lights to determine how many to place in any given JPanel. Experiment with this and you'll see what I mean.I want to use a borderLayout and use 3 instances of PartyLights and then get those 3 instances; west, north and east. But how do I draw those Lights that in all of these instances get these lights. I posted a screen to make things more clear.
Also, another way to get a blinking light effect is to create BufferedImages of the lights (colored circles) by using a BufferedImage's Graphics object to draw the circles (don't forget to dispose of the Graphics object after use!), creating ImageIcons with the Images, and then creating JLabels that have their icons swapped inside of the Swing Timer.
Similar Threads
-
BorderLayout
By oneself in forum New To JavaReplies: 3Last Post: 08-06-2009, 10:59 PM -
[SOLVED] Possible to have two BoxLayout in same area of BorderLayout?
By mainy in forum AWT / SwingReplies: 4Last Post: 02-16-2009, 09:52 PM -
BorderLayout Demo
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:51 PM -
Help with BorderLayout
By lenny in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 07:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks