zappsweden
Programmer
I have got a Canvas that paints one small image(roughly 30x20) and one big image(1280x1024). The small is a car and the big is a track. Anyway, i create a JFrame and put a a JScrollPane in it and i also put the Canvas in the ScrollPane. The problem is that my JScrollPane is 1280x724 and my Image is just cut-off without any Scrollbars appearing. I can force Scrollbars but there is no point since i can't use them(because my image is now 1280x724 also). When I modify the program so that the JScrollPane is 1280x1024 i have no problem displaying the whole image.
Why doesn't my JScrollPane recognize the size of the Canvas?
below is my code for the 3 classes. if you want to try it you should put an 1280x1024 image and a small image with the same filenames i use, and in the "right" path(in C.
BTW, i just want to say that java runs very fast indeed now with the newer versions of jdk. I manage to get framerates of 50-100 frames at the same time as running a game engine even though i repaint the whole 1280x1024 for every frame!
And I am only using a 350MHz processor. Who cares if c/c++ can be about 30% faster(because it can leave out overhead in array bounds checking and other stuff) because most of the times it doesn't matter much. Java saves ALOT of time for the developer so you can afford to put more time in to the "fun stuff"(that is making the game engine) instead of worrying about solving all the technicalities.
I am not saying that c++ is bad i just got carried away because I felt i have done so much in less time thanks to the great API that follows with java.
i haven't optimized my code much either. the game I am developing is a Formula one management game which probably will be finished in 1-2 years. I have no name for it yet.
public class StartUp {
public static void main(String[] arg) {
MyContainer my = new MyContainer();
my.setVisible(true);
my.go();
}
}
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class MyContainer extends JFrame {
MyCanvas m;
private JScrollPane jScrollPane1;
public MyContainer() {
m=new MyCanvas();
jScrollPane1 = new JScrollPane(m);
//this.setSize(1280, 824);
this.setVisible(true);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setBackground(Color.green);
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
}
public void go() {
//this.setSize(1280, 1024);
for(;
this.m.repaint();
}
}
import java.awt.Canvas;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Graphics;
import java.awt.Color;
public class MyCanvas extends Canvas {
Image car, track;
int xpos;
boolean inc=true;
public MyCanvas() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
track=Toolkit.getDefaultToolkit().createImage("C:\\testtrack.gif"
car=Toolkit.getDefaultToolkit().createImage("C:\\miniferrari.jpg"
}
public void paint(Graphics g) {
g.drawImage(track, 0, 0, this);
g.drawImage(car, xpos, 0, this);
g.dispose();
if(inc==true) {
xpos=xpos+40;
if(xpos==1200) inc=false;
}
else {
xpos=xpos-40;
if(xpos==0) inc=true;
}
}
public void update(Graphics g) {
this.paint(g);
}
private void jbInit() throws Exception {
this.setBackground(Color.cyan);
this.setForeground(Color.red);
}
}
Why doesn't my JScrollPane recognize the size of the Canvas?
below is my code for the 3 classes. if you want to try it you should put an 1280x1024 image and a small image with the same filenames i use, and in the "right" path(in C.
BTW, i just want to say that java runs very fast indeed now with the newer versions of jdk. I manage to get framerates of 50-100 frames at the same time as running a game engine even though i repaint the whole 1280x1024 for every frame!
And I am only using a 350MHz processor. Who cares if c/c++ can be about 30% faster(because it can leave out overhead in array bounds checking and other stuff) because most of the times it doesn't matter much. Java saves ALOT of time for the developer so you can afford to put more time in to the "fun stuff"(that is making the game engine) instead of worrying about solving all the technicalities.
I am not saying that c++ is bad i just got carried away because I felt i have done so much in less time thanks to the great API that follows with java.
i haven't optimized my code much either. the game I am developing is a Formula one management game which probably will be finished in 1-2 years. I have no name for it yet.
public class StartUp {
public static void main(String[] arg) {
MyContainer my = new MyContainer();
my.setVisible(true);
my.go();
}
}
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class MyContainer extends JFrame {
MyCanvas m;
private JScrollPane jScrollPane1;
public MyContainer() {
m=new MyCanvas();
jScrollPane1 = new JScrollPane(m);
//this.setSize(1280, 824);
this.setVisible(true);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setBackground(Color.green);
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
}
public void go() {
//this.setSize(1280, 1024);
for(;
this.m.repaint();
}
}
import java.awt.Canvas;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Graphics;
import java.awt.Color;
public class MyCanvas extends Canvas {
Image car, track;
int xpos;
boolean inc=true;
public MyCanvas() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
track=Toolkit.getDefaultToolkit().createImage("C:\\testtrack.gif"
car=Toolkit.getDefaultToolkit().createImage("C:\\miniferrari.jpg"
}
public void paint(Graphics g) {
g.drawImage(track, 0, 0, this);
g.drawImage(car, xpos, 0, this);
g.dispose();
if(inc==true) {
xpos=xpos+40;
if(xpos==1200) inc=false;
}
else {
xpos=xpos-40;
if(xpos==0) inc=true;
}
}
public void update(Graphics g) {
this.paint(g);
}
private void jbInit() throws Exception {
this.setBackground(Color.cyan);
this.setForeground(Color.red);
}
}