java - Repainting Graphics Issue -
I'm trying to write a small side-scroller game, but ran into a problem with redrawing The key listener works only when it presses the direction key, increases the x and y value and decreases. Just repeating () does not seem to be repetitive.
Public class SideScroller implements the JPNL KeyListener {Private random random = new random (); Private Ent RR, G, B; Private int X = 0, Y = 0; Public Zero Key is pressed (key ID) {if (e.getKeyCode () == E.VK_UP) {y - = 5; } Else if (e.getKeyCode () == e.VK_DOWN) {y + = 5; } And if (e.getKeyCode () == EVKLLFT} {x - = 5; } And if (e.getKeyCode () == E.VK_RHHT) {x + = 5; } Color (); } Public Zero Key Related (Key Event E) {} Public Zero Key Type (KeyEyeE) {} Public Zero Color (Graphics GG) {gg.setColor (New Color (R, G, B)); Gg.fillRect (x, y, 50, 50); } Public static zero main (string [] Args) {SideScroller SS = New SideScroller (); JFrame f = new JFrame (); F.add (new sidecroller ()); F.setSize (500, 500); F.setLocationRelativeTo (zero); F.setResizable (wrong); F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); F.setVisible (true); F.addKeyListener (new sidecroller ()); -
You must be overridden paintComponent
and < Code> super.paintComponent , not color . Protected Zero PaintCompany (Graphics GG) {super.paintComponent (gg); Gg.setColor (new color (R, G, B)); Gg.fillRect (x, y, 50, 50); -
In addition, I recommend using the KeyListener instead, -
In addition, you should run the Swipe application on the Event Dispatch Thread, you can wrap the code in Main in SwingUtilities.invokeLater (...) . / P> Here are all the fixes mentioned
import java.awt.Color; Import java.awt.Graphics; Import java.awt.event.ActionEvent; Import java.util.Random; Import javax.swing.AbstractAction; Import javax.swing.ActionMap; Import javax.swing.InputMap; Import javax.swing.JComponent; Import javax.swing.JFrame; Import javax.swing.JPanel; Import javax.swing.KeyStroke; Import javax.swing.SwingUtilities; Public square SideScroller Zipal {Private random random = new random () Private intRR, G, B; Private int X = 0, Y = 0; Public SideScroller () {InputMap im = getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap am = getActionMap (); Im.put (KeyStroke.getKeyStroke ("UP"), "Operation"); Am.put ("Activity", New Abstract Action () {Executed Public Zero Action (ActionEvent E) {y - = 5; repaint ();}}); Im.put (KeyStroke.getKeyStroke ("Down"), "DownEction"); Am.put ("downaction", new abstract action () {public zero action action (e)} {y + = 5; repaint ();}}); Im.put (KeyStroke.getKeyStroke ("LEFT"), "left"); Am.put ("leftaction", new abstract action () {executed with public zero verb (actionEvent e) {x - = 5; repaint ();}}); Im.put (KeyStroke.getKeyStroke ("RIGHT"), "Authorization"); Am.put ("rightaction", new abstract action () {public zero functioning (action event e) {x + = 5; repaint ();}}); } Protected Zero PaintCompany (Graphics GG) {SuperPoint Component (GG); Gg.setColor (new color (R, G, B)); Gg.fillRect (x, y, 50, 50); } Public static zero main (string [] Args) {SwingUtilities.invokeLater (New Runnabal) (Public Zero Run) {JFrame f = new JFrame (); F.add (new sidecroller ()); F.setSize (500, 500); F.setLocationRelativeTo (null); F.setResizable (wrong); F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); F.setVisible (true);}}); }}
Comments
Post a Comment