java - Implementing clolored free-hand lines with SWING -


Hi!

I want to draw free lines in a JFram. I implemented it in a square that enhances the zypnal, its definition looks like this:

  The public class draw extends JPanel to the MouseMotionListener, MouseListener   

I added a zero method that was added to the square where I have also been added to the geometry drawn from Draw object. My problem is now: I want to be able to choose the line color from a menu:

  jmnu color menu = new jmnu (); Janmietime green color = new gemenuitem ("green"); GreenChoice.addActionListener (this); ColorMenu.add (greenChoice); Jenemitis Red Choice = New Genius ("Red"); ColorMenu.add (redChoice); JMURbar bar = new JMUR (); Bar.add (colorMenu); I do not know where to implement this code! Do I have JFrame together with 2 sections, one for drawing and one for my menu? How do I tell the drawing class to interact with my JMManu, e.g. Can I choose a color from the menu? Here it is where I create my line in class Drwa, with the default color blue: ( 
  public zero paintComponent (Graphics G) {Graphics2D G2D = (Graphics2D) G, If (point1! = Null & amp; amp;; points = 2 = null) {g2d.setPaint (Color.BLUE); g2d.setStroke (New BasicStroke (1.5f)); g2d.draw (line2d);}} < / Code>  

Thank you!

Here is a free hand drawing class which I Swing was used to describe graphics. As you want it

  import java.awt.color; import java.awt.Graphics; import java.awt.point; import java.awt.event.mouseEvent; import java.util.arrayList; import java.util.list; import javax Swing.JFrame; import javax.swing.jpanel; import javax.swing.SwingUtilities; import javax.swing.event.mouseInputAdapter; public class DrawingTest applies Runnable {Private JFrame frame, Private MyDrawPanel drawPanel, from private list & lt; Point & Gt; Numbers, Public DrawingTest () {Issue = New ArrayList & lt; Point & gt; ();} @Override P Ublic void run () {frame = new jframe ("Shading Vertis"); DrawPanel = New MyDrawPanel (this); Mylistor alpha = new milistner (this); DrawPanel.addMouseMotionListener (alpha); DrawPanel.addMouseListener (alpha); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Add frame.getContentPane () (drawPanel) .; Frame.setSize (800, 600); Frame.setVisible (true); } Public JPanel getDrawingPanel () {return drawPanel; } Public listing & lt; Point & gt; GetPoints () {Return points; } Public Zero Setpoint (int x, int y) {points.add (new point (X, Y)); } Public Zero Reset Pin () {points.clear (); } Public static zero main (string [] args) {SwingUtilities.invokeLater (new drawing); } Private class MyListener provides MouseInputAdapter {Private drawingTest drawingTest; Public MyListener (drawingtest drawingtest) {this.drawingTest = drawingTest; } @ Override Public Wide MouseDrag Ged (Mouse Event Event) {drawingTest.setPoint (event.getX (), Event .getY ()); DrawingTest.getDrawingPanel () repaint () .; } @ Override Public Wide Moorlyaged (Mouse Event Event) {drawingTest.resetPoints (); }} Private class expanded MyadraPanel {Personal Static Final Large Serial Version UID = 1L; Private drawing tasting drawing tastes; Public MyDrawPanel (drawing drawingtest) {this.drawingTest = drawingTest; } @ Override Public Wide Paint Comonant (Graphics G) {G. Set color (color. WHITE); G.fillRect (0, 0, this.getWidth), this.getHyight ()); G.setColor (Color.BLUE); (Int i = 1; i & lt; drawingTest.getPoints (.) Size (); i ++) {point p1 = drawing.theget points (). Get (i-1); Point p2 = drawingstate.gate point () Get (i); G.drawline (p1.x, p1.y, p2.x, p2.y); }}}}    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -