Results 1 to 18 of 18
Thread: Drawing Graph and Add to JPanel
- 09-10-2009, 10:34 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Drawing Graph and Add to JPanel
Hi. I am coding for a simple Graphic Calculator.
As you can see in the picture (img143.imageshack.us/img143/3800/jpanel.png), a graph is supposed to be shown in that orange box (mainPanel).
Since I had coded a Draw.class, I created an object and add to mainPanelHowever, nothing is shown in mainPanel. What did I get wrong?Java Code:Draw newGraph=new Draw() mainPanel.add(newGraph)
Thank you.
- 09-10-2009, 10:41 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Hard to tell without seeing your code.
- 09-10-2009, 10:49 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Thanks for reply.
My codes for Draw.classThe picture shows how I coded for Panel:Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package DrawPanel; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.JFrame; /** * * @author NguyenDuyLong */ public class Draw extends JPanel{ public double x1,x2; public int xCenter; public int yCenter; public Draw(){ super(); setVisible(true); setBackground(Color.blue); setForeground(Color.red); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); xCenter=(int)1024/2; yCenter=700; //xCenter=(int)Size.getWidth()/2; //yCenter=(int)Size.getHeight()/2; g.drawLine(0,yCenter,xCenter*2,yCenter); g.drawLine(xCenter,0,xCenter,yCenter*2); //g.drawOval(500,200,200,500); } }
img98.imageshack.us/img98/8112/panel.png
- 09-10-2009, 10:56 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If I add your panel to a frame I do see something but only after maximizing frame.
You should consider using sizes relative to the the size of the frame in your drawing.
- 09-10-2009, 11:00 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
I have been aware of that. However my main point is that the Panel does not seem to work. Even if I change the value of xCenter and yCenter, Panel does not show anything.
- 09-10-2009, 11:02 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Perhaps show us how you are displaying it then. Are you adding it to a frame?
I certainly see the blue background and the x-y axis.
- 09-10-2009, 11:26 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Please check this link:
img196.imageshack.us/gal.php?g=95376863.png
I try both mainPanel and mainFrame but they show nothing.
Please tell me what I get wrong.
- 09-10-2009, 11:33 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Where is your code where you test the panel? That is what I asked for above.
example: here is my test for your panel
Now post your code which you say shows nothing.Java Code:import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import java.awt.Color; import java.awt.Graphics; class Draw extends JPanel { public double x1, x2; public int xCenter; public int yCenter; public Draw() { super(); setVisible(true); setBackground(Color.blue); setForeground(Color.red); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); xCenter = 1024 / 2; yCenter = 700; // xCenter=(int)Size.getWidth()/2; // yCenter=(int)Size.getHeight()/2; g.drawLine(0, yCenter, xCenter * 2, yCenter); g.drawLine(xCenter, 0, xCenter, yCenter * 2); // g.drawOval(500,200,200,500); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initGUI(); } }); } public static void initGUI() { JFrame f = new JFrame("Frame "); f.setSize(300, 300); f.add(new Draw()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
- 09-10-2009, 11:44 AM #9
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Ooops. I think I misunderstand your point. What I mean is that the codes
do not show anything in the NetBean project that I am trying to do.Java Code:Draw Graph=new Draw() Panel.add(Graph)
- 09-10-2009, 11:49 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Yeah but those panels don't display on their own you know.
- 09-10-2009, 12:00 PM #11
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
I add mainPanel.setVisible(true) and mainFrame.setVisible(true) as well, but they still do not work. Otherwise, I do not know what to do.
- 09-10-2009, 12:19 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Since it seems you are never going to post your code, read the code I posted above and compare it to yours then.
P.S If you are new to Java you should close Netbeans, get Textpad and go through Sun's tutorial testing the codes on Textpad.
- 09-10-2009, 12:32 PM #13
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
I think that I did not make my point clear.
The following is my code to check the "Draw.class"It works. I agree with you.Java Code:package desktopapplication2; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.SwingUtilities; /** * * @author NguyenDuyLong */ public class Draw extends JPanel{ public double x1,x2; public int xCenter; public int yCenter; public Draw(){ super(); setVisible(true); setBackground(Color.blue); setForeground(Color.red); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); xCenter=(int)200; yCenter=300; //xCenter=(int)Size.getWidth()/2; //yCenter=(int)Size.getHeight()/2; g.drawLine(0,yCenter,xCenter*2,yCenter); g.drawLine(xCenter,0,xCenter,yCenter*2); //g.drawOval(500,200,200,500); } public static void main(String[] args) { JFrame f = new JFrame("Frame "); f.setSize(300, 300); f.add(new Draw()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
I think I should make my point again: The Draw.class works fine. What troubles me is that when I try to plot such graph on a graphPanel in my application, it does not work. I am not interested in drawing one graph but many graphs. It is kind of Graphic Calculator which user can put in equations, click the button "Graph!" and gets the result on the graphPanel. "Draw.java" runs well but when I create a Draw object and add into graphPanel and hope for a similar result, it does not. So my application would not work.
I hope that you would understand me now. If not, I have nothing else to say. It seems to me that I somehow make you angry. Sorry for that.
- 09-10-2009, 12:39 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Is there any possibility in us seeing the full runnable code where the Draw object is added onto another panel and then does not work?
- 09-10-2009, 04:09 PM #15
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
Basically, I just add a new JPanel newPanel to mainPanel(created by NetBean automatically). In newPanel, I choose "Customize Code" and include:
The three java files are here:Java Code:Draw newGraph= new Draw(); newPanel.add(newGraph); newPanel.setVisible(true);
mediafire.com/download.php?hj3zjgy3kmz]DesktopApplication1.java
mediafire.com/download.php?v1ozdzvatjz]DesktopApplication1AboutBox.java
mediafire.com/download.php?mmnogzmhywd
- 09-10-2009, 04:22 PM #16
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You are going to need frames to display the frames on.
I suggest you read the Swing tutorial.
- 09-22-2009, 12:30 PM #17
Member
- Join Date
- Aug 2009
- Posts
- 11
- Rep Power
- 0
-
gancio, this advice of yours here while creative, is completely irrelevant to the original poster's problem.
Last edited by Fubarable; 09-22-2009 at 01:35 PM.
Similar Threads
-
Looking for help on drawing stuff in a jPanel
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 08-28-2009, 06:00 PM -
drawing in JPanel ,beyond the co-ordinates
By anandnarayan891 in forum AWT / SwingReplies: 4Last Post: 04-05-2009, 03:42 AM -
drawing to a JPanel
By diggitydoggz in forum New To JavaReplies: 11Last Post: 03-09-2009, 07:42 AM -
Drawing points on a JPanel
By josephdcoleman in forum New To JavaReplies: 6Last Post: 02-25-2009, 03:47 PM -
how to draw x-y graph in Jpanel.--not in APPLET.
By vincent2001@gmail.com in forum New To JavaReplies: 2Last Post: 08-24-2008, 05:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks