When i call setSize, it sets the size of the frame, but not the actual canvas. How do I fix this? Code:
[code]
package ballmovement;
import java.applet.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main extends Applet implements Runnable {
private Image dbImage;
private Graphics dbg;
Dimension size = new Dimension(700,700);
public void init() {
size = new Dimension(700,700);
this.setBackground(Color.black);
}
public void start() {
this.setSize(size); //ey?
Thread th = new Thread(this);
th.start();
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
//Error's not here either.
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
}
}
}
public Dimension getSize(){
return this.size;
}
public void update(Graphics g) {
System.out.println(this.getSize().toString());
// initialize buffer
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// clear screen in background
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor(getForeground());
paint(dbg);
// draw image on the screen
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g) {
//hurr hurr nothing important here
}
}
[/code]
Thanks in advance. :)
[editline]20th November 2010[/editline]
when I call setsize, the window's size is set, but not the applet it contains... it also seems that the applet itself is limited to 250*300...
You have to set the size in init();
Also, you can just do [I]this.setSize(700, 700);[/I]
But also, if you still choose to use Dimension, you don't have to initialize it during both its declaration and in init(). You can just leave the initialization in the declaration (just have [I]Dimension size = new Dimension(700,700)[/I]), or you can leave the initialization in init() and just leave its declaration as [I]Dimension size;[/I]
Okay... I put it in init but i get the same exact problem.
[code]
import java.applet.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main extends Applet implements Runnable, MouseListener, MouseMotionListener {
//Main definitions
int Radius = 10;
private Image dbImage;
private Graphics dbg;
Point2D.Double mouse, sprite;
double angle, velocity;
Dimension size = new Dimension(700,700);
public void init() {
this.setSize(size);
this.setBackground(Color.black);
this.setPreferredSize(size);
mouse = new Point2D.Double(0,0);
sprite = new Point2D.Double(0,0);
angle = 0.0;
velocity = 0.5;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void start() {
Thread th = new Thread(this);
th.start();
}
protected double calcAngle(Point2D.Double p1, Point2D.Double p2) {
return Math.atan2(p2.y - p1.y, p2.x - p1.x);
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
angle = calcAngle(sprite, mouse);
// make the circle "snap" to the mouse location if it's close enough
if (Math.round(sprite.x) == Math.round(mouse.x)) {
sprite.x = mouse.x;
}
if (Math.round(sprite.y) == Math.round(mouse.y)) {
sprite.y = mouse.y;
}
if (sprite.x != mouse.x) {
sprite.x += Math.cos(angle) * velocity;
}
if (sprite.y != mouse.y) {
sprite.y += Math.sin(angle) * velocity;
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
}
}
}
public void mouseClicked(MouseEvent e) {
this.mouse.x = e.getX();
this.mouse.y = e.getY();
}
//public Dimension getSize(){
// return this.size;
//}
public void update(Graphics g) {
System.out.println(this.getSize().toString());
// initialize buffer
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// clear screen in background
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor(getForeground());
paint(dbg);
// draw image on the screen
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillOval((int) this.sprite.x - Radius, (int) this.sprite.y - Radius, 2 * Radius, 2 * Radius);
g.setColor(Color.red);
g.drawLine((int) this.sprite.x, (int) this.sprite.y, (int) this.mouse.x, (int) this.mouse.y);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
this.mouseClicked(e);
}
public void mouseMoved(MouseEvent e) {
}
}
[/code]
There's the full code for inspection.
I'm quite new to java so I don't know if this will help at all, but don't you need to draw everything to a JPanel, set its size and then use paintComponent(Graphics g){//Draw stuff here} to draw to it?
Or maybe you need to set the content pane's size (if applets even have a content pane, I've not used them)?
Sorry, you need to Log In to post a reply to this thread.