java - draw method animation to another class -
There is a bit thicker with Java, but someone can tell me how to take the draw method from the list of objects, from where There is more 1 box, and it has to be put into action, so I redesign to The basic idea is ... when Then The following is a very basic concept. .. X and
y to
and move them Can I modify the getter?
box class:
public zero draw (graphics g) {g.setColor (Color.BLUE); Gifilact (getX) - (width / 2)), (getY) - (height / 2)), getWidth (), getHeight ()); }
actionPerform method: (this is in another class)
PublicExchangeEvent (e) {timer. Start (); }
Actions are called ...
paintComponent method within your components, repeat the list and color them
Import java.awt.borderLayout; Import java.awt.color; Import java.awt.Dimension; Import java.awt.EventQueue; Import java.awt.Graphics; Import java.awt.Graphics2D; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import java.util.ArrayList; Import java.util.list; Import javax.swing.JComponent; Import javax.swing.JFrame; Import javax.swing.JPanel; Import javax.swing.Timer; Import javax.swing.UIManager; Import javax.swing.UnsupportedLookAndFeelException; Public class flyingbox {public static zero main (string [] args) {new flying box (); } Public Interface Drawable {Public Zero Update (JComponent Comp); Public Zero Draw (Graphics G); } Public square box applies Drawable {Private int x; Private int y; Private int width = 10; Private intensity height = 10; Private Int x Delta; Private int yDelta; Public box (int x, int y) {this.x = x; this. Y = y; XDelta = random (); YDelta = Random (); } @ Override Public Recording Update (JCMPnet Comp) {x + = xDelta; Y + = yDelta; If (x <0) {x = 0; XDelta * = -1; } And if (x + width> comp.getWidth ()) {x = comp.getWidth () - width; XDelta * = -1; } If (Y end lie; 0) {y = 0; YDelta * = -1; } And if (y + height> comp.getHyight ()) {y = comp.getHeight () - height; YDelta * = -1; }} @ Override Public Empty Draw (Graphics G) {g.setColor (Color.BLUE); G.fillRect (X, Y, width, height); } Secure Sur Random () {int value = 0; Do {value = -2 + (int) (Math.random () * 4); } While (value == 0); return value; }} Public Flying Box () {EventQueue.invokeLater {@Override Public Zero () {{UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ())}} Catch (ClassNotFoundException | InstantiationException | Invalid Access Exception | unsupported view and before fxx expansion) {} Jeffre Frame = New Gefram ("Test"); Frame.Set default close operation (JFRM.EXIT_ON_CLOSE), frame.selectable (new border layout ()); Frame.ed (new testpan ()); Frame.pack (); Frame.Setlineschillate (faucet); Frames.Seviable (true);}}); } Public Square TestPane JPanel {Private listing & lt; Drawable & gt; Drauble; {Int x = (int) (Math.Rendem) for public testpane () (droble = new arrelistist <; (25); (int index = 0; index; * 190); Int y = (int) (Math.random (* * 190); Drawables.add (new box (x, y)); timer timer = new timer (40, new exclamation engineer) ({override public void Action Criteria (Draft D: Drabs) {d.update (TestPane.this)}} repaint ();}}); timer.start ();} @ Override public dimension getPreferredSize () { New Dimension Back (200, 200);} @ Override Protected Zero Do Tkanpanent (Graphics g) to {Suprpintkanponent (g); (Drayniy D: Drobl) {Dkdraw (G);}}}} On
Comments
Post a Comment