Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make a rectangle move across the screen with key bindings?
    primarykey
    data
    text
    <p>The game I'm trying to create is snake and so far I've figured out how to use <code>paint(Graphics g)</code> a bit of <code>JPanel</code>, mouse listener and now I'm trying to create a rectangle that will move across the screen and use key bindings or key listener, but I have no idea how I should go about this.</p> <p>Here's my code so far, it has 2 parts. The first part is called <code>snake2</code> because if I don't know what I'm doing I make the same program with different things. <code>Snake</code> used frame, but <code>Snake2</code> uses <code>JPanel</code> (looks better…)</p> <pre><code> import java.awt.*; //required for MouseListener import java.awt.event.*; //requied for Graohics import java.applet.*; import javax.swing.*; public class Snake2 extends JPanel { private Rectangle sampleObject; public Snake2() { addMouseListener(new MouseListener()); } /* create background */ public void paint (Graphics g) { Font angel = new Font("Angelic War", Font.BOLD, 60); Font ith = new Font("Ithornît", Font.BOLD, 78); setBackground(Color.darkGray); g.setColor(Color.darkGray); g.fillRect(0,0,700,850); g.setColor(Color.gray); g.fillRect(50,150,600,650); g.setColor(Color.white); g.drawRect(50,150,600,650); g.drawString("Quit",52,116); g.drawRect(50,100,30,20); //g.setFont(angel); //g.drawString("SNAKE",300,70); g.setFont(ith); g.drawString("SNAKE",280,90); } public void sprite (int x, int y, Graphics g){ g.setColor(Color.white); g.fillRect(300,200,10,10); } public void start (int x, int y, Graphics g){ g.setColor(Color.white); g.drawString("START GAME",300,425); } } /* Tracks where mouse is clicked */ class MouseListener extends MouseAdapter { public void mouseReleased(MouseEvent me) { if (me.getX() &gt;= 50 &amp;&amp; me.getX() &lt;= 80 &amp;&amp; me.getY() &gt;= 100 &amp;&amp; me.getY() &lt;= 120) { System.exit(0); } String str="Mouse Released at "+me.getX()+","+me.getY(); System.out.println(str); } } </code></pre> <p>And the second part is:</p> <pre><code> import javax.swing.JFrame; import java.awt.Dimension; public class SnakeDisplay { public static void main ( String [ ] arguments ) { JFrame frame = new JFrame ( "Snake" ); Snake2 panel = new Snake2 ( ); frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); frame.add ( panel ); frame.setContentPane ( panel ); frame.setPreferredSize ( new Dimension ( 700, 850 ) ); //frame.setLocationRelativeTo ( null ); frame.setVisible ( true ); frame.pack ( ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload