Results 1 to 3 of 3
Thread: drawing in java
- 11-07-2011, 03:32 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 21
- Rep Power
- 0
drawing in java
I'm was trying to make a class that will accept a graphics object and some parameters, apply methods on graphics object, and then return it back to the source to be 'published'.
Is it possible?
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; public class Drawing { public static void main(String[] args) { int x=30; int y=80; Graphics2D g1=null; Graphics2D g2=null; Lineing lineing = new Lineing(); lineing.setObject(g1); g2 = lineing.painter(x,y); BufferedImage bimage = g2.getDeviceConfiguration().createCompatibleImage(400, 400, Transparency.OPAQUE); ImagePanel contentPane = new ImagePanel(bimage); JFrame f = new JFrame("Linea"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(contentPane); f.setBounds(200, 200, 500, 500); f.setVisible(true); f.setResizable(false); } }
Java Code:import java.awt.*; public class Lineing { Graphics2D panel = null; public void setObject(Graphics2D g){ panel = g; } public Graphics2D painter(int a, int b){ panel.setColor(new Color(50, 50,200)); panel.setStroke(new BasicStroke(14F)); panel.drawLine(a,a,b,b); return panel; } }
- 11-07-2011, 03:57 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: drawing in java
I feel like it may be easier to just allow the class to draw itself. Then override the paintComponent of where you want to paint and call the method.
Java Code:public class Ball{ private int x, y; public void drawBall(Graphics2D g){ g.fillOval(...); } } public class OverriddenJPanel extends JPanel{ private Ball b; @Override public void paintComponent(Graphics g){ Graphics2D g2d = (Graphics2D) g; b.drawBall(g2d); } }
To make this more general you can allow the class to not override any other class and have it's own method to draw everything. Then you can simply allow the class to be drawn on any class with an overridden paintComponent method.
- 11-07-2011, 06:39 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 21
- Rep Power
- 0
Re: drawing in java
Well,the idea was to have a class that will prepare some graphic output and then return back that object without actually displaying it .
Problem is that I'm not yet sure where do I need the output.
There is gonna be lots of graphs and calculations.
Is it possible to have a set of Graphics objects (g1, g2, g3...) that could be sent to that class, which draws upon 'em according to one set of parameters, and returns them back as (G1, G2, G3)
so that I can decide later how to output them?
In fact I need at least 2 classes like that because I need the outcomes of calculations presented in more different ways.
The other class is supposed to take same (g1, g2, g3) with second set of parameters, and to give back objects (G1' G2' G3')....etc.
Similar Threads
-
Java GUI drawing a line
By seanfmglobal in forum New To JavaReplies: 26Last Post: 04-26-2011, 05:52 AM -
Drawing coordinates in Java GUI
By javausr in forum Java 2DReplies: 5Last Post: 12-29-2010, 10:49 AM -
Drawing strings in Java
By LyraM in forum Java 2DReplies: 4Last Post: 05-11-2010, 11:15 PM -
drawing shapes in java help
By alphajoseph in forum Java 2DReplies: 2Last Post: 09-29-2009, 06:35 PM -
Java Drawing PUZZLE
By Cyorxamp in forum AWT / SwingReplies: 3Last Post: 06-09-2008, 10:35 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks