Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I getting a { error?
    primarykey
    data
    text
    <p>I am getting java:105: error: illegal character: \29 }</p> <p>I am writing a program where the user can click a right/left/up/down button and move a "ball" on a screen.</p> <p>I can't figure out what I am doing wrong. Can someone please help me with this?</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Lab2a extends JFrame { Lab2a(){ setTitle("Lab 1b - Application #2"); Lab2Panel p = new Lab2Panel(); add(p); } public static void main(String[] args){ Lab2 frame = new Lab2(); frame.setTitle("Lab2 Application # 1"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); frame.setVisible(true); } } class Lab2Panel extends JPanel{ Lab2Button canvas = new Lab2Button(); JPanel panel = new JPanel(); Lab2Panel () { setLayout(new BorderLayout()); JButton leftButton = new JButton("left"); JButton rightButton = new JButton("right"); JButton upButton = new JButton("up"); JButton downButton = new JButton("down"); panel.add(leftButton); panel.add(rightButton); panel.add(upButton); panel.add(downButton); this.add(canvas, BorderLayout.CENTER); this.add(panel, BorderLayout.SOUTH); leftButton.addActionListener(new LeftListener(canvas)); rightButton.addActionListener(new RightListener(canvas)); upButton.addActionListener(new UpListener(canvas)); downButton.addActionListener(new DownListener(canvas)); } } class Lab2Button extends JPanel { int radius = 5; int x = -1; int y = -1; protected void paintComponent(Graphics g){ if (x&lt;0 || y&lt;0) { x = getWidth() / 2 - radius; y = getHeight() / 2 - radius; } super.paintComponent(g); g.drawOval(x,y, 2 * radius, 2 * radius); } public void moveLeft(){ x -= 5; this.repaint(); } public void moveRight(){ x += 5; this.repaint(); } public void moveUp(){ y += 5; this.repaint(); } public void moveDown(){ y -= 5; this.repaint(); } } class LeftListener implements ActionListener{ private Lab2Button canvas; LeftListener(Lab2Button canvas) { this.canvas = canvas; } public void actionPerformed(ActionEvent e){ canvas.moveLeft(); } } </code></pre> <p>Sorry about that 105 is the line above this one.</p> <pre><code>class RightListener implements ActionListener{ private Lab2Button canvas; RightListener(Lab2Button canvas) { this.canvas = canvas; } public void actionPerformed(ActionEvent e){ canvas.moveRight(); } } class UpListener implements ActionListener{ private Lab2Button canvas; UpListener(Lab2Button canvas) { this.canvas = canvas; } public void actionPerformed(ActionEvent e){ canvas.moveUp(); } } class DownListener implements ActionListener{ private Lab2Button canvas; DownListener(Lab2Button canvas) { this.canvas = canvas; } public void actionPerformed(ActionEvent e){ canvas.moveDown(); } } </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