Results 1 to 2 of 2
- 12-25-2013, 01:23 PM #1
Member
- Join Date
- Dec 2013
- Posts
- 26
- Rep Power
- 0
Drawing to a JPanel-type class, from a different class.
Hello everyone, now to this forum :)
I'm fairly new to 2D graphics in Java.
I learned how to draw simple shapes on a JPanel, using the paintComponent method.
Now I'm trying to do the same thing, except the drawing should be done from an exterior class, to the class that extends JPanel.
I tried something that I believe should work, but it doesn't. Could you explain to me the standard way to do such a thing?
Here is my attempt:
Class Main:
Java Code:package m; import java.util.*; import java.awt.*; import java.awt.Event.*; import javax.swing.*; public class Main extends JFrame { public static void main(String[] args) { Main m = new Main(); } public Main(){ setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("Bla"); setSize(500,500); Surface s = new Surface(); add(s); setVisible(true); } }
Java Code:package m; import java.util.*; import java.awt.*; import java.awt.Event.*; import javax.swing.*; public class Surface extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); DrawRect d = new DrawRect(this); d.draw(); } }
Java Code:package m; import java.util.*; import java.awt.*; import java.awt.Event.*; import javax.swing.*; public class DrawRect { Surface surface; Graphics g; public DrawRect(Surface surface){ this.surface = surface; g = surface.getGraphics(); } public void draw(){ g.fillRect(20,20,100,50); // (this won't work). } }
- 12-25-2013, 02:51 PM #2
Re: Drawing to a JPanel-type class, from a different class.
Also posted at How to draw a rectangle on a JPanel, from a different class?
If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Calculator: Buttons don't show up on Test JFrame class from JPanel class(w/ bg image)
By manibby93 in forum New To JavaReplies: 3Last Post: 12-08-2013, 09:28 PM -
JTextField on JPanel-class within JFrame-class
By floris in forum AWT / SwingReplies: 5Last Post: 06-25-2012, 12:54 PM -
calling a function of a specific type in a java class from another class.....
By safi532 in forum Advanced JavaReplies: 3Last Post: 01-26-2012, 11:20 PM -
How to get a compatible class of a template class? Return type of method is AClass<E>
By SKuypers in forum Advanced JavaReplies: 0Last Post: 12-07-2011, 11:55 AM -
Painting things on a JPanel class from another class?
By Alerhau in forum New To JavaReplies: 2Last Post: 10-17-2011, 08:04 PM
Bookmarks