class or interface error

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
shohreh
Posts: 4
Joined: Sun Apr 23, 2006 9:43 pm
Contact:

class or interface error

Post by shohreh »

How do I correct this error
circle.java:56: 'class' or 'interface' expected
{
^
1 error
import java.util.StringTokenizer;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;

public class Circle extends Applet
{
public Circle()
{ circle = new Ellipse2D.Double(0,0, CIRCLE_WIDTH, CIRCLE_HEIGHT);

MouseClickListener listener = new MouseClickListener();
addMouseListener(listener);

// Color bg = new Color(0.0F, 0.0F, 0.0F);
// setBackground(bg);




}



public void paint(Graphics g)
{ Graphics2D g2 = (Graphics2D)g;
Color pink = new Color(1.0F, 0.7F, 0.7F);
g2.setColor(Color.pink);
g2.fill(circle);
g2.draw(circle);
g2.setFont(font);
g2.setColor(Color.black);
g2.drawString(message, 50, 100);
//g2.fill(circle);
String message = "Just a test";
int Font_size = 14;
Font font = new Font("Helvetica", Font.BOLD, Font_size);
FontRenderContext context = g2.getFontRenderContext();
TextLayout layout = new TextLayout (message, font, context);
float xMessageWidth = layout.getAdvance();
float yMessageHeight =layout.getAscent() + layout.getDescent();
//getWidth() - xMessageWidth
float xLeft = 0.5F * (getWidth() - xMessageWidth);
float yTop = 0.5F * (getHeight() - yMessageHeight);
float yBase = yTop + layout.getAscent();
g2.drawString(message, xLeft, yBase);
}
}

{
private Ellipse2D.Double circle;
private static final double CIRCLE_WIDTH = 400;
private static final double CIRCLE_HEIGHT = 200;
//private I commented out private changed to public
private MouseClickListener extends MouseAdapter
{

{
}

public void mouseClicked(MouseEvent event)
{ int mouseX = event.getX();
int mouseY = event.getY();

circle.setFrame(mouseX - CIRCLE_WIDTH/2, mouseY - CIRCLE_HEIGHT/2, CIRCLE_WIDTH, CIRCLE_HEIGHT);

repaint();
}

}
}
User avatar
speiestaaim
Posts: 2
Joined: Fri Jun 16, 2006 10:45 am
Location: Pune, India

Post by speiestaaim »

Your source code is not correct. Not a problem with textpad.
<SpaceTime/>
BenjaminB
Posts: 10
Joined: Tue Sep 12, 2006 3:09 pm

Re: class or interface error

Post by BenjaminB »

shohreh wrote: (...)
layout.getDescent();
//getWidth() - xMessageWidth
float xLeft = 0.5F * (getWidth() - xMessageWidth);
float yTop = 0.5F * (getHeight() - yMessageHeight);
float yBase = yTop + layout.getAscent();
g2.drawString(message, xLeft, yBase);
}
}
There are too many brackets. The last "}" closes your class. That's why the compiler assumes the end of the file or another class.

And the following code is REALLY messy.
shohreh wrote:
{
private Ellipse2D.Double circle;
private static final double CIRCLE_WIDTH = 400;
private static final double CIRCLE_HEIGHT = 200;
//private I commented out private changed to public
private MouseClickListener extends MouseAdapter
{

{
}

public void mouseClicked(MouseEvent event)
{ int mouseX = event.getX();
int mouseY = event.getY();

circle.setFrame(mouseX - CIRCLE_WIDTH/2, mouseY - CIRCLE_HEIGHT/2, CIRCLE_WIDTH, CIRCLE_HEIGHT);

repaint();
}

}
}
Do you have any idea about how to code Java? :D You wrote a method(?) without telling the compiler that you did so. (you forgot 'public void anything(anything)' or similar) And what is that supposed to do:
{

{
}
?
Post Reply