Results 1 to 7 of 7
Thread: Help with paintComponent!
- 12-27-2010, 12:06 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Help with paintComponent!
Hiya,
I've got a rather large program, a catalogue system. It all works fine, except I have a frame that shows a list of order states. ANyway i want to display graphics on that frame for loading, but I cannot seem to get any of it working. I've used graphics on classes on there own so I understand how the graphics work, but for some reason I cannot get it working in this frame. I have a panel created called paintPanel so far. Anyway pointers on how to draw inside this panel would be greatly appreciated
Here are my class codes
Class DisplayGUI
Java Code:package Clients; import java.awt.*; import java.util.List; import java.util.Map; import java.util.Collection; import javax.swing.*; import Middle.*; import java.util.Set; import java.util.HashMap; class DisplayGUI extends JFrame { private enum State { process, checked } private static final int H = 600; // Height of window pixels private static final int W = 600; // Width of window pixels private OrderProcessing theOrder = null; private JLabel waiting = new JLabel(); private JLabel beingProcessed = new JLabel(); private JLabel waitingForCollection = new JLabel(); private JTextArea theWaiting = new JTextArea(); private JTextArea theBeingProcessed = new JTextArea(); private JTextArea theWaitingForCollection = new JTextArea(); private JPanel paintPanel = new JPanel(); public DisplayGUI( RootPaneContainer rpc, MiddleFactory mf ) { try // { theOrder = mf.makeOrderProcessing(); // Process order } catch ( Exception e ) { System.out.println("Exception: " + e.getMessage() ); } Container cp = rpc.getContentPane(); // Content Pane Container rootWindow = (Container) rpc; // Root Window cp.setLayout(null); // No layout manager rootWindow.setSize( W, H ); // Size of Window Font f = new Font("Monospaced",Font.PLAIN,12); // Font f is waiting.setBounds( 12, 35 , 170, 20 ); // Message area waiting.setText( "Waiting" ); cp.add(BorderLayout.EAST, waiting ); theWaiting.setBounds( 10, 65 , 170, 150 ); // Message area cp.add(BorderLayout.EAST, theWaiting ); // Add to canvas beingProcessed.setBounds( 202, 35 , 170, 20 ); // Message area beingProcessed.setText( "Being Processed" ); cp.add(beingProcessed ); paintPanel.setBounds( 10, 240 , 170, 150 ); // Message area # paintPanel.setBackground(Color.white); cp.add(paintPanel ); theBeingProcessed.setBounds( 200, 65 , 170, 150 ); // Message area cp.add( theBeingProcessed ); // Add to canvas waitingForCollection.setBounds( 392, 35 , 170, 20 ); // Message area waitingForCollection.setText( "Waiting For Collection" ); cp.add(waitingForCollection ); theWaitingForCollection.setBounds( 390, 65 , 170, 150 ); // Message area cp.add( theWaitingForCollection ); // Add to canvas rootWindow.setVisible( true ); // Make visible backgroundDisplay bd = new backgroundDisplay(); bd.start(); } class backgroundDisplay extends Thread { public void run() { while ( true ) { try { Processing.Order order = new Processing.Order(); Map res = theOrder.getOrderState(); System.out.println(res); { StringBuffer sb = new StringBuffer(); Object waitingList = res.get("Waiting"); String waitingListString = waitingList.toString(); int n = waitingListString.length(); for(int i=0; i < n; i++) { if(!((waitingListString.charAt(i) == '[')||(waitingListString.charAt(i) == ']'))) { sb.append(waitingListString.charAt(i)); } else { } } String finalWaiting = sb.toString(); theWaiting.setText( finalWaiting ); StringBuffer sc = new StringBuffer(); Object beingPickedList = res.get("BeingPicked"); String beingPickedString = beingPickedList.toString(); int a = waitingListString.length(); for(int i=0; i < a; i++) { if(!((beingPickedString.charAt(i) == '[')||(beingPickedString.charAt(i) == ']'))) { sc.append(beingPickedString.charAt(i)); } else { } } String finalProcessed = sc.toString(); theBeingProcessed.setText( finalProcessed ); Object toBeCollectedList = res.get("ToBeCollected"); String beingCollectedString = toBeCollectedList.toString(); theWaitingForCollection.setText( beingCollectedString ); } sleep(5000); // delay } catch ( Exception err ) { } } } } }
Java Code:package Clients; import javax.swing.*; import java.awt.*; import Middle.*; /** * The Display Client. * @author Michael Alexander Smith * @version 2.0 */ public class DisplayClient { public static void main (String args[]) { String stockURL = args.length < 1 // URL of stock RW ? Names.STOCK_RW // default location : args[0]; // supplied location String orderURL = args.length < 2 // URL of order ? Names.ORDER // default location : args[1]; // supplied location RemoteMiddleFactory mrf = new RemoteMiddleFactory(); mrf.setStockRWInfo( stockURL ); mrf.setOrderInfo ( orderURL ); // displayGUI(mrf); // Create GUI } public static void displayGUI(MiddleFactory mf) { JFrame window = new JFrame(); window.setTitle( "Display Client (RMI)"); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); new DisplayGUI( window, mf ); } }
Thanks
-
You state you need help with paintComponent, yet you're not showing your paintComponent code, and that confuses me. Shouldn't your paintPanel override this method?
- 12-27-2010, 12:27 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
I have this :
public void paintComponent(Graphics paintFrame) {
int width = getWidth();
int height = getHeight();
paintFrame.setColor(Color.black);
paintFrame.drawOval(0, 0, width, height);
}
which I have placed :
Java Code:package Clients; import java.awt.*; import java.util.List; import java.util.Map; import java.util.Collection; import javax.swing.*; import Middle.*; import java.util.Set; import java.util.HashMap; class DisplayGUI extends JFrame { private enum State { process, checked } private static final int H = 600; // Height of window pixels private static final int W = 600; // Width of window pixels private OrderProcessing theOrder = null; private JLabel waiting = new JLabel(); private JLabel beingProcessed = new JLabel(); private JLabel waitingForCollection = new JLabel(); private JTextArea theWaiting = new JTextArea(); private JTextArea theBeingProcessed = new JTextArea(); private JTextArea theWaitingForCollection = new JTextArea(); private JPanel paintFrame = new JPanel(); [B][COLOR="Red"]//HERE//[/COLOR][/B] public void paintComponent(Graphics paintFrame) { int width = getWidth(); int height = getHeight(); paintFrame.setColor(Color.black); paintFrame.drawOval(0, 0, width, height); } public DisplayGUI( RootPaneContainer rpc, MiddleFactory mf ) { try // { theOrder = mf.makeOrderProcessing(); // Process order } catch ( Exception e ) { System.out.println("Exception: " + e.getMessage() ); } Container cp = rpc.getContentPane(); // Content Pane Container rootWindow = (Container) rpc; // Root Window cp.setLayout(null); // No layout manager rootWindow.setSize( W, H ); // Size of Window Font f = new Font("Monospaced",Font.PLAIN,12); // Font f is waiting.setBounds( 12, 35 , 170, 20 ); // Message area waiting.setText( "Waiting" ); cp.add(BorderLayout.EAST, waiting ); theWaiting.setBounds( 10, 65 , 170, 150 ); // Message area cp.add(BorderLayout.EAST, theWaiting ); // Add to canvas beingProcessed.setBounds( 202, 35 , 170, 20 ); // Message area beingProcessed.setText( "Being Processed" ); cp.add(beingProcessed ); paintFrame.setBounds( 10, 240 , 170, 150 ); // Message area # paintFrame.setBackground(Color.white); cp.add(paintFrame ); theBeingProcessed.setBounds( 200, 65 , 170, 150 ); // Message area cp.add( theBeingProcessed ); // Add to canvas waitingForCollection.setBounds( 392, 35 , 170, 20 ); // Message area waitingForCollection.setText( "Waiting For Collection" ); cp.add(waitingForCollection ); theWaitingForCollection.setBounds( 390, 65 , 170, 150 ); // Message area cp.add( theWaitingForCollection ); // Add to canvas rootWindow.setVisible( true ); // Make visible backgroundDisplay bd = new backgroundDisplay(); bd.start(); } class backgroundDisplay extends Thread { public void run() { while ( true ) { try { Processing.Order order = new Processing.Order(); Map res = theOrder.getOrderState(); System.out.println(res); { StringBuffer sb = new StringBuffer(); Object waitingList = res.get("Waiting"); String waitingListString = waitingList.toString(); int n = waitingListString.length(); for(int i=0; i < n; i++) { if(!((waitingListString.charAt(i) == '[')||(waitingListString.charAt(i) == ']'))) { sb.append(waitingListString.charAt(i)); } else { } } String finalWaiting = sb.toString(); theWaiting.setText( finalWaiting ); StringBuffer sc = new StringBuffer(); Object beingPickedList = res.get("BeingPicked"); String beingPickedString = beingPickedList.toString(); int a = waitingListString.length(); for(int i=0; i < a; i++) { if(!((beingPickedString.charAt(i) == '[')||(beingPickedString.charAt(i) == ']'))) { sc.append(beingPickedString.charAt(i)); } else { } } String finalProcessed = sc.toString(); theBeingProcessed.setText( finalProcessed ); Object toBeCollectedList = res.get("ToBeCollected"); String beingCollectedString = toBeCollectedList.toString(); theWaitingForCollection.setText( beingCollectedString ); } sleep(5000); // delay } catch ( Exception err ) { } } } } }
- 12-27-2010, 12:38 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
A JFrame isn't a JComponent so it can never override a paintComponent( ... ) method and it will never be called. A JFrame has a content pane that is painted in the area of the JFrame. The content pane is a JComponent (a JPanel perhaps?). Read the API documentation of the JFrame class.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-27-2010, 02:31 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Thnaks for the reply, I have declared and added a JPanel called paintFrame (sorry for the confusing name!) to my content pane.
taken from my code :
Java Code:private JPanel paintFrame = new JPanel(); paintFrame.setBounds( 10, 240 , 170, 150 ); paintFrame.setBackground(Color.white); cp.add(paintFrame );
Java Code:public void paintComponent(Graphics paintFrame) { int width = getWidth(); int height = getHeight(); paintFrame.setColor(Color.black); paintFrame.drawOval(0, 0, width, height); }
-
Please read the tutorials on painting in Swing as need to unlearn some assumptions. You must extend JPanel or other JComponent to be able to override and use its paintComponent method. Again, I strongly urge you to study the tutorials.
Edit: an example of extending a JPanel and overriding its paintComponent is as Jos is showing below.Last edited by Fubarable; 12-27-2010 at 03:02 PM.
- 12-27-2010, 02:59 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
You can't just define a paintComponent( ... ) method anywhere and expect it to be called when an unrelated JPanel needs to be repainted. You have to extend the JPanel class and override that method in that class so it'll be called whenever the object needs to be repainted, so your paintPanel has to be of this class:
Java Code:public class PaintPanel extends JPanel { ... public void paintComponent(Graphics g) { // do your graphics here } }
Java Code:PaintPanel paintPanel= new PaintPanel();
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
JPanel PaintComponent
By capiono in forum AWT / SwingReplies: 5Last Post: 10-31-2010, 03:36 AM -
Working around paintcomponent
By sahhhm in forum New To JavaReplies: 2Last Post: 05-16-2008, 03:43 AM -
Problem going outside paintComponent
By Thez in forum Java 2DReplies: 9Last Post: 12-08-2007, 05:59 PM -
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 11:36 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 04:43 AM
Bookmarks