Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

one error in Java code Please help?

Hi Everyone

I need help with this code please. This code is created to import a adjustable graphic in an Applet.

Code: import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class MoveIt extends Applet implements ActionListener

{

// Declare Variables

private Image cup;

private Panel keyPad;

public int top = 15;

public int left = 15;

private Button keysArray[];

public void init()

{

cup = getImage(getDocumentBase(), "cup.gif");

Canvas myCanvas = new Canvas();

// Construct Panel and Buttons

keypad = new Panel();

keys[1] = new Button("Up");

keys[2] = new Button("Left");

keys[3] = new Button("Right");

keys[4] = new Button("Down");

keys[5] = new Button("Center");

// Set Background Color

setBackground(blue);

// Set Layout Manager for Frame

this.setLayout(new BorderLayout());

// Set keypad Layout Manager

this.setLayout(new BorderLayout());

//Add Buttons to the keypad Panels

keypad.add(Up, BorderLayout.NORTH);

keypad.add(Down, BorderLayout.SOUTH);

keypad.add(Left, BorderLayout. EAST);

keypad.add(Right, BorderLayout.WEST);

keypad.add(Center, BorderLayout.Center);

add(myCanvas, BorderLayout.NORTH);

add(keypad, BorderLayout.SOUTH);

// Add ActionListener Statements for each of the Buttons

UpButton.addActionListener(this);

DownButton.addActionListener(this);

LeftButton.addActionListener(this);

RightButton.addActionListener(this);

CenterButton.addActionListener(this);

}

public void paint(Graphics g)

{

g.drawImage(cup, left, top, this);

}

public void actionPerformed(ActionEvent e)

{

String arg = e.getActionCommand();

if(arg == "Up")

top = top -15;

if(arg == "Down")

top = top + 15;

if(arg == "Left")

top = top - 15;

if(arg == "Right")

top = top + 15;

if(arg == "Center")

top = 60;

Left = 125;

}

repaint();

}

Error is: :\Course Technology\59850_Java3e_DF1-4\Chapters 5-8\Chapter06\MoveIt.java:90: invalid method declaration; return type required

repaint();

^

1 error

Thank You :-)

2 Answers

Relevance
  • Will H
    Lv 7
    9 years ago
    Favorite Answer

    Try repaint during action performed.

    public void actionPerformed(ActionEvent e)

    {

    String arg = e.getActionCommand();

    if(arg == "Up")

    top = top -15;

    if(arg == "Down")

    top = top + 15;

    if(arg == "Left")

    top = top - 15;

    if(arg == "Right")

    top = top + 15;

    if(arg == "Center")

    top = 60;

    Left = 125;

    repaint();

    }

    }

  • eckis
    Lv 4
    4 years ago

    import java.applet.*; import java.awt.adventure; import java.awt.pictures; public classification NewClass2 extends Applet implements Runnable { int i; String message; public void init() { setBackground(java.awt.colour.eco-friendly); } public void initiate(){ new Thread(this).initiate(); } public void run() { at the same time as (authentic) { i++; repaint(); try { Thread.sleep(333); } capture (InterruptedException e) { ; } } } public void paint(pictures g) { g.drawString("i = " + i, 10, 20); g.drawString(message, 20, 20); } public boolean keyDown(adventure e, int key) { message = "fee = " + key; go back authentic; } }

Still have questions? Get your answers by asking now.