Results 1 to 4 of 4
Thread: extending JPanel issue
- 04-27-2011, 12:44 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
extending JPanel issue
Hello,
I'm new to java and I'm having bit time trouble starting with Graphics class...
I have to do some custom graphics and other 2D figures. I decided to extend a basic component and just add the drawing features "on top", but hit a wall...
Okay so I use NetBeans7.0 and so far I
1) Created following class GraphicsPanel.
2) Added new JPanel to my applicaton's gui.Java Code:package gui; import javax.swing.*; // For JPanel, etc. import java.awt.*; // For Graphics, etc. public class GraphicsPanel extends JPanel { private Graphics g1; @Override public void paintComponent(Graphics g) { g1 = g; clear(g); g.drawLine (10, 0, 10, 100); g.drawLine (250, 100, 10, 100); } public void AddLine() { g1.drawLine (100, 100, 10, 100); super.paintComponent(g1); System.out.println("AddLine()"); } // super.paintComponent clears off screen pixmap, // since we're using double buffering by default. protected void clear(Graphics g) { super.paintComponent(g); } }
Since NetBeans doesn't allow modifications to variable declaration code, I used default
but changed the initialization code to :Java Code:private javax.swing.JPanel TestGraphic;
3) So now when I run the application I can see the result fromJava Code:TestGraphic = new GraphicsPanel();
g.drawLine (10, 0, 10, 100); and g.drawLine (250, 100, 10, 100);
in public void paintComponent(Graphics g)
Picture - ImageShack® - Online Photo and Video Hosting
4) Now I'd like to have a method that enables me to add the actual content to this graphic using public void AddLine(), so I created the jButton with following code
The method is called and System.out.println("AddLine()") is executed(I see result in console), but nothing happens in the graphical interface... I guess I shouldn't use g1 this way, but still can't figure out what should i do.Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { GraphicsPanel myTestGraphic = (GraphicsPanel)TestGraphic; myTestGraphic.AddLine(); }
I've already spent half day on this and checked couple books and web tutorials, but still can't figure out what I'm missing... thank you..
Regards,
Pavel
- 04-27-2011, 02:19 PM #2
No, you absolutely should not be using Graphics like that.
What you want to be doing is keeping a List of lines. In paintComponent(), just paint all the lines in the List. In the addLine() method, add a line to that List, then call repaint().
My best advice to you is to ditch the GUI builder altogether.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-28-2011, 07:10 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Hello Kevin,
Thank you for the advice it was priceless!!
I solved the problem by doing following changes:
1) Defined line, because I couldn't find a predefined one.
2) Modified my GraphicsPanelJava Code:public class Line { public int[] xy = new int[4]; public Line() { xy[0] = 0; //x1 xy[1] = 0; //y1 xy[2] = 0; //x2 xy[3] = 0; //y2 } public Line(int X1,int Y1,int X2,int Y2) { this.xy[0] = X1; this.xy[1] = Y1; this.xy[2] = X2; this.xy[3] = Y2; } }
3) Changed my JButton a littleJava Code:package gui; import javax.swing.*; // For JPanel, etc. import java.awt.*; // For Graphics, etc. public class GraphicsPanel extends JPanel { int[] ln = new int[4]; java.util.LinkedList<core.Line> ll = new java.util.LinkedList<core.Line>(); public GraphicsPanel(){ ll.add(new core.Line(10, 0, 10, 100)); ll.add(new core.Line(10, 100, 400, 100)); } @Override public void paintComponent(Graphics g) { // Clear off-screen bitmap super.paintComponent(g); // Cast Graphics to Graphics2D Graphics2D g2d = (Graphics2D)g; //print blue frame g2d.setPaint(Color.BLUE); for(int i = 0; i< 2; i++) { ln = ll.get(i).xy; g2d.drawLine(ln[0], ln[1], ln[2], ln[3]); } // print red lines with the data g2d.setPaint(Color.RED); for(int i = 2; i< ll.size(); i++) { ln = ll.get(i).xy; g2d.drawLine(ln[0], ln[1], ln[2], ln[3]); } } public void AddLine(core.Line l) { ll.add(l); this.repaint(); } public void AddLine(int x1, int y1, int x2, int y2) { ll.add(new core.Line(x1, y1, x2, y2)); this.repaint(); } }
Now I haveJava Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { GraphicsPanel myTestGraphic = (GraphicsPanel)TestGraphic; myTestGraphic.AddLine(new core.Line(10, 100, 200, 50)); myTestGraphic.AddLine(140, 100, 340, 50); }
initial state - ImageShack® - Online Photo and Video Hosting
after jButton1 is clicked ImageShack® - Online Photo and Video Hosting
About dropping the GUI Builder... I already got warned that it's better to avoid it, but for a java-noob (especially coming from MS Visual Studio) doing GUI with text editor is a big cultural shock, I guess I'll need some time to agree to do that.. :D
Once again thanks for your response!
If I made big mistake again please do not hesitate to correct me:rolleyes:
Regards,
PavelLast edited by p.pavlov; 04-28-2011 at 07:20 PM. Reason: corrections
- 04-29-2011, 01:27 PM #4
Much better.
The only thing I'd like to point out is standard naming conventions- variables and methods should always start with a lower-case letter, classes start with a capital letter. It sounds picky, but it makes your code much easier to read.
Also, there already is a Line class in the standard API. Actually there are 3! Line2D (which is abstract), Line2D.Double, and Line2D.Float. You might use Line2D.Double instead of a custom Line class.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Adding jscrollpane when extending jpanel
By phil128 in forum AWT / SwingReplies: 4Last Post: 01-18-2011, 11:30 PM -
Extending Classes and What is Necessary
By GavinCash in forum New To JavaReplies: 10Last Post: 10-11-2010, 07:07 AM -
JPanel/JScrollPane issue
By Moncleared in forum New To JavaReplies: 1Last Post: 09-23-2010, 04:02 AM -
Possible to do JPanel drawing without extending?
By SM2010 in forum AWT / SwingReplies: 7Last Post: 07-02-2010, 11:30 PM -
Implementing and Extending together
By eva in forum New To JavaReplies: 2Last Post: 12-24-2007, 09:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks