Results 1 to 1 of 1
- 05-20-2010, 08:18 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 9
- Rep Power
- 0
Draw Shape later of Press Button, other implementation., where is mistake?
Draw Shape later of Press Button, other implementation.
Cordial Greetings:
Although this question has the same purpose as another question for me, it changes the way of implementing the objective.
First declare a class:
PHP Code:class Object2D { Color mc; int ObType; static int mLINE = 1; static int mRECT = 2; int X0,Y0, X1,Y1; float ms; Graphics mg; Object2D(Graphics g, Color c, int t, int x0, int x1, int y0, int y1, float s){ mg = g; mc = c; ms = s; ObType = t; X0 = x0; Y0 = y0; X1 = x1; Y1 = y1; } void ViewObject2D(){ Graphics2D g2d = (Graphics2D)mg; g2d.setColor(mc); g2d.setStroke(new BasicStroke(ms)); g2d.setPaintMode(); if (ObType==mLINE){ g2d.draw(new Line2D.Double(X0, X1, Y0, Y1)); } if (ObType==mRECT){ g2d.draw(new Rectangle2D.Double(X0, Y0, X1, Y1)); } } }
Now I detail how I use the class:
build an array of class type called ObjectVector
construct a method that fills the array with two objects
Create a method that displays the contents of the vector
PHP Code:public class Grapher extends javax.swing.JFrame { /** Creates new form Grapher */ Vector<Object2D> ObjectVector = new Vector<Object2D>(); Graphics g1d; ... public void FillVector(Graphics g){ //Create two objects a line type and a rectangular type ObjectVector.addElement(new Objeto2D(g,new Color(0,0,255),Objeto2D.mLINE,100,100,200,200,1.0f)); ObjectVector.addElement(new Objeto2D(g,new Color(0,255,0),Objeto2D.mRECT,150,150,200,200,1.5f)); } public void DrawVector(){ //Draw the contents of the vector if(!ObjectVector.isEmpty()){ for (int i = 0; i<ObjectVector.size();i++){ ObjectVector.elementAt(i).ViewObject2D(); } } } ...
Let me explain ..
The paint method gets an object of type Graphics
Initializes a variable of type Graphics called g1d... this with the purpose of later use
fills the vector with two objects FillVector(g); and then draw the vector DrawVector();...
PHP Code:public void paint(Graphics g) { this.g1d = g; super.paint(this.g1d); FillVector(g); DrawVector(); } private void jbDrawActionPerformed(java.awt.event.ActionEvent evt) { FillVector(this.g1d); repaint(); }
Question:
Is there any way that after pressing the jbDraw button is drawn really as Needed and not when calling paint()?;
That is, paint only serve to initialize the graphics object and draw the objects contained in the vector ObjectVector.
Is it possible to do so requires either using another method?PHP Code:public void paint(Graphics g) { this.g1d = g; super.paint(this.g1d); DrawVector(); }
Thanks ...
Similar Threads
-
Button click for drawing geometry shape
By nnur in forum AWT / SwingReplies: 5Last Post: 05-15-2010, 07:48 PM -
Draw shapes by pressing a button
By SWEngineer in forum AWT / SwingReplies: 16Last Post: 05-14-2010, 05:26 AM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 12:48 PM -
simulate form type=button press with java application
By redmoonzer01 in forum NetworkingReplies: 1Last Post: 03-29-2008, 06:28 AM -
Press any key to continue/press enter
By JT4NK3D in forum New To JavaReplies: 1Last Post: 11-17-2007, 09:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks