Painting components over images

I've exteneded a JFrame and overridden it's paint() method because I want to draw an image in the background and then display a component on top of it (label, button, etc). But my component is not displaying in the right place. It only displays in the top left corner, even though I've set the location to something else. I don't think I'm painting it right somehow.

Repainting /clipping area?
&nbsp(87 Views)
One way would be to get the bounds of the rectangle before the move, move the rectangle, repaint both the original and the new area.

Repainting my buttons does not work
&nbsp(102 Views)
there,
I'm trying to make a nice vector game.
The algorithm is quite easy, you start with a numer you choose between 3 an 5.
For example, you choose 3 so the next turn could be 2, 3or 4 (3-1, 3again or 3+1).
I'm writing those numbers in an array and update them.
My Array looks like this for example:
[4|0|0|3|4|5|0|...|0]
than I make the next step with 3 for example:
[4|0|0|3|0|2|3|4|0|0|...|0]
than I make the next step with 4 for example:
[4|0|0|3|0|0|0|4|0|0|3|4|5|0|0|...|0]
But when I try to get it in an gui, I fail.
I make Buttons which are enable when they are not 0
and lable them like the number in the array.
Until here, I have no problems.
But when I press a butten, the buttons will not change.
Here's my Code:
Button class
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Button {
public static void makeStartButton(int von, final int bis) {
// TODO Auto-generated method stub
for (int i=von;i

painting a custom JComponent on a JLayeredPane
&nbsp(68 Views)
EDIT: this is an edited version of the original post since that was probably a bit too much at once... i'll try to keep it a bit simpler now to hopefully get an answer...
dear javaranch community,
i have found this place a valueable source of information then and when and finally signed up to discuss an issue i'm not really sure about.
i have the OpenJDK Runtime Environment (build 1.6.0_0-b11) installed, my OS is Ubuntu-Linux.
the scenario is as follows:
i want to display a number of lines and some filled rectangles on a panel. all of these objects can possibly overlap and the z-order (or stacking order) is important.
i found that when i draw onto a normal JPanel, its not entirely reliable that the last painted object is also displayed on top so i found JLayeredPane might be useful for what i am trying to achieve. since i wan't full control over where my lines and rectangles appear, i use a null-layout (no layout-manager).
it took some time until i found out, that my custom JComponents won't get painted unless i call setSize() with values > 0 before.
further, when i set my custom JComponent to lets say a size of 50x50, it seems i can only use draw commands within the area of (0,0) and (50,50), everything outside this area won't get displayed.
so unless line 29 on SimpleLine is commented in, no lines are visible and since there is no output on the console, paintComponent() is probably never called.
:: can anybody please tell me whats happening here or confirm this behaviour
here is a code example, the controlling class: package custJComponentDrawingSimple;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import java.awt.Color;
public class CJCD {
private static JFrame frm;
private static JLayeredPane lpnl;
protected static int pnlw, pnlh; // the inner dimensions of the panel (without windowborders)
public static void main(String[] args) {
// main window settings
CJCD.frm = new JFrame( "custJComponentDrawingTest" );
CJCD.frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
CJCD.frm.setSize( 400, 400 );
CJCD.frm.setResizable(false);
CJCD.frm.setLayout(null);
CJCD.frm.setVisible( true );
// determine inner size of window
CJCD.pnlw = CJCD.frm.getContentPane().getWidth();
CJCD.pnlh = CJCD.frm.getContentPane().getHeight();
// SimpleLines will be stacked in a layered pane
CJCD.lpnl = new JLayeredPane();
// set panes size to main windows inner size
CJCD.lpnl.setSize( CJCD.pnlw, CJCD.pnlh );
CJCD.lpnl.setOpaque(true);
CJCD.lpnl.setBackground(new Color(31, 31, 31));
CJCD.lpnl.setVisible(true);
CJCD.frm.add(CJCD.lpnl); // add lpnl to frame
// add some test-lines
SimpleLine cSL1, cSL2;
cSL1 = new SimpleLine(1, 10, 10, 360, 360);
CJCD.lpnl.add(cSL1, 1);
// this doesn't help
cSL1.setVisible(true);
cSL1.repaint(); // explicitly request repaint of the component
cSL2 = new SimpleLine(2, 80, 300, 380 , 10);
CJCD.lpnl.add(cSL2, 2);
CJCD.lpnl.repaint(); // explicitly request repaint of the container
}
}

JPanel not repainting
&nbsp(88 Views)
I do not use SWING a lot and I need help figuring this out: Inside JFrame I added JSplitFrame which bottom part contains JPanel. Now I want the panel dynamically populated with checkboxes after a button is pressed. Here is the code inside the panel which generates checkboxes: ... private Box bx; public MyPanel (){//this is the panel constructor extending JPanel super(); initialize(); bx=new Box(BoxLayout.Y_AXIS); add(bx); } ... public void populate(int start){ bx.remove() remove(bx); int sz=20; for (int i=0;i

painting hundreds of Rectangles in JPanel
&nbsp(67 Views)
Hi ranchers! I need some advise on how to make my paintComponent go faster. I'm bulding a InDesign like program. A user can drag rectangles, change border, columns, colors on the rectangle. rectangles have diffrent z-index so you can draw a rectangle over a rectangle. Every time the mouse is moved over the JPanel that acts like a container for the rectangles 4 JSpinners is changed with the x, y , width and height for all selected rectangles. If I have say 50-100 rectangles and drag them around the screen it is very slow. Is there someway of making drawing faster for 50-100 rectangles that is beeing dragged around the screen

JTextArea not painting
&nbsp(63 Views)
I have a JFrame in which i am adding two JTextAreas and a JPanel. In my JPanel i am drawing some graphics using basic Java drawing functions. The first TextArea is to act as an output prompt, and the second acts as the input field where i type commands. the program is a simple text based game. The problem is that the JPanel paints and the graphics inside it show, but both of the textAreas do not paint until i type a command and press enter, or until i highlight text that's already in the compnent. i've already tried calling repaint(), repaintComponent(), and repaintComponents(). none of them seem to work. any help is greatly appreciated.

repainting in shapes other than square
&nbsp(68 Views)
What I want to do is to repaint a square component but only a portion of it say in a circular shape. Something like.... repaint(10,10,new Circle()) ... I think that gets the point across. I do not want to repaint the entire component just some portion of it in a shape other than repaint(0,0,getWidth(),getHeight()) type of deal.

problem painting
&nbsp(74 Views)
I have a JFrame that contains a JPanel (tbox), and a JScrollPane (vPort) which displays the contents of another JPanel (view). The problem is when I draw on 'view' tbox does not repaint and the scrollbars on vPort do not repaint, they show whatever is behind the JFrame. The drawing is done in the JFrame's 'paint' event. I've tried calling the 'repaint()' methods for 'tbox' and 'vPort' at the end of the event and the componets repaint, but then it doesn't show what is drawn in view. When no repaints are called: When 'tbox.repaint()' is called: When 'vPort.repaint()' is called: When both are repainted:

JLabel not repainting automatically
&nbsp(69 Views)
Dear Readers, I have a JFrame with four JPanels in it. In one of the JPanels, I have a JLabel (in a JScrollPane) as the only component in that JPanel that I use for displaying Sprites (little objects). Everything works except for the fact that whenever I minimize this JFrame or maximize it, the Sprites disappear. Only when I move the JScrollPane associated with the JLabel do the Sprites reappear. I know this is a problem with paintComponent() not being called. I tried everything I know but it still dosent work. I tried setting listeners for the JFrame so that when an event occurs I call the method to repaint the Sprites. But it didnt work. Your help would be greatly appreciated. [ March 06, 2007: Message edited by: Shyam Prasad Murarka ]

Laying out components in JPanel(s) and LayoutManager(s)
&nbsp(60 Views)
Hi all, I'm practicing Swing examples with JPanels and LayoutManagers to display the typical windows form. I'm not using any open source class because it's for my SCJD. Problem: Picture this, i have a JFrame with a GridLayout manager of 3 by 3. Each cell contains a JPanel with a GridLayout manager for them also (actually not all of them). When i add components to the cells of the JPanels, they seem to be too far apart and all in the center of the cell, and because some components are bigger in height than other, they seem reall un-aligned and not very professional like. I'm sure there is something to align them in there corresponging cells, but what, and is what i am doing normal

JDesktopPane/JInternalFrame - question re: closing frames and repainting
&nbsp(78 Views)
I have a JSplitPane that contains a JPanel as one of its subcomponents. This JPanel (BorderLayout) contains a toolbar (another JPanel) and a JDesktopPane. The JDesktopPane contains 0-many JInternalFrames all of which are rather simple (i.e., I don't do anything real fancy). The toolbar, among other things, contains a button that when clicked will close all of the JInternalFrames in the JDesktopPane. If I click on an individual frame's close button (X) in the top-right corner, without having written one line of code to handle this, the JSplitPane.paint(Graphics) method is called. If I click on the Close button in my toolbar, my myToolbarPane.actionPerformed() method is called (see below) and the JSplitPane.paint(Graphics) method is NOT called (using either implementation). public void actionPerformed(ActionEvent e) { // Implementation #1 myDesktopPane.remove(); myDesktopPane.repaint(); // Implementation #2 // JInternalFrame[] elementFrames = myDesktopPane.getFrames(); // for (int i = 0; i < elementFrames.length; i++) { // elementFrames[i].doDefaultCloseAction(); // } } The question I am throwing out to is ultimately why is the JSplitPane.paint(Graphics) method NOT called when the Close button is clicked It just so happens that I need a panel in the other part of the JSplitPane to repaint when a JInternalFrame is closed. This is not happening when I click on the Close button. Also know that I tried having the panel that needs to update listen to/observe when the frames are closed and have confirmed it is being notified and it is calling revalidate() and repaint() but nothing happens because it thinks the height and width of the panel is 0.

Reshaping swing components
&nbsp(72 Views)
hi Is it possible to have swing components (buttons, label etc.) in shapes other than a rectangle (i.e circle etc.) If yes, please tell me how to, if no then why not

adding components to JPanel
&nbsp(65 Views)
, I want to know how to make visible components added to a JPanel that happens to be inside a JScrollPane after the JFrame has already been created. My class, that extends JFrame, has the jbInit() method you see below. At the very end of the jbInit() method I set up cardPanel to a GridLayout of 10 rows 1 column. This is so that I can display up to 10 distinct components as I add them dynamically using the displayResults() method. As you can imagine, the new HandStats (which extend JPanel) if they are being added to cardPanel, are not being displayed. I've tried using repaint() methods but they don't do anything. How do I accomplish this Also, how do I remove the HandStats objects from cardPanel so that I can display another new set of them Alan *************************************************
ArrayList players = new ArrayList();
JPanel cardPanel = new JPanel();
private void jbInit() throws Exception
{
this.setSize(new Dimension(600, 300));
this.setTitle("Card Games");
contentPane = (JPanel) this.getContentPane();
...
...
cardPanel.setLayout(new GridLayout(10,1));
scrollpane.getViewport().add(cardPanel);
contentPane.add(scrollpane, BorderLayout.CENTER);
}
private void displayResults()
{
for(int i = 0; i < players.size(); i++)
{
HandStats stats = new HandStats("Player" + (i + 1) + "\nScore: " + ((Player)players.get(i)).getHandScore());
Card[] cards = ((Player)players.get(i)).getHand();
for(int j = 0; j < cards.length; j++)
stats.addComponent(cards[j]);
cardPanel.add(stats);
}
scrollpane.repaint(); //doesn't do anything
this.repaint(); //doesn't do anything either
//enableMenus();
}