The moose likes Swing / AWT / SWT and the fly likes a curious problem about canvas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "a curious problem about canvas " Watch "a curious problem about canvas " New topic

a curious problem about canvas

alan fisher

Joined: Sep 27, 2003
Posts: 5
I add two canvas into a panel, as for these tow canvas, I set different background,but when the program runs,it shows only one canvas,why?
The test source code as follows:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class Test
public static void main(String[] args)
JFrame frame = new MainFrame();

class MainFrame extends JFrame {
public MainFrame()
Container contentPane = getContentPane();
JPanel jPanel2 = new JPanel();

jPanel2.setLayout(new BorderLayout());
Canvas nodeCanvas=new Canvas();
Canvas topoCanvas=new Canvas();
nodeCanvas.setBackground(new Color(255,0,255));
topoCanvas.setForeground(new Color(0,255,255));
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Swing likes the setPreferredSize method. Also, in Swing we use either JComponent or JPanel instead of Canvas. It is recommended not to mix heavyweight AWT components with lightweight Swing components.
I agree. Here's the link: http://aspose.com/file-tools
subject: a curious problem about canvas
Similar Threads
paintComponent() problem
Using GradientPaint and Swing Components
Why does this crash
setting background image in applet
is it possible to design your own background 4 jbuttons