Так что моя проблема в том, что я рисую много изображений, чтобы видеть, и я не могу заставить отображаться панели JScrollPane, чтобы я мог просматривать все изображения.
Мой код такой.*
Вам понадобится изображение 40 на 40 или получите ошибку.Имя файла: Wall.gif, DefualtTileBackup.gif, Character.gif, metal3.jpg и DefualtTileBackup.g
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.net.*;
public class DrawImage extends JPanel {
private Image image;
Toolkit tk = Toolkit.getDefaultToolkit();
private char[][] fieldMap = {
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
{'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
{'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
};
public static void main(String[] args) {
JFrame frame = new JFrame("DrawImage");
frame.setSize(600,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(new DrawImage()));
}
public DrawImage() {
super();
setVisible(true);
}
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) { }
return url;
}
public void paint(Graphics g) {
//create an instance of Graphics2D
Graphics2D g2d = (Graphics2D) g;
//fill the background with black
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, getSize().width, getSize().height);
//draw the image
for(int K = 0; K < this.fieldMap.length ; K = K+1){
for(int J = 0; J < this.fieldMap[K].length; J = J + 1){
g2d.drawImage(imagePlacer(this.fieldMap[K][J]),(J*40),(K*40), this );
} // loop Y end
}// loop X end
}// paint end
public Image imagePlacer(char tiles){
try {
switch(tiles){
case('$'):
System.out.println("Wall, $");
image = tk.getImage(getURL("Wall.gif"));
break;
case('F'):
System.out.println("Floor, #");
image = tk.getImage(getURL("DefualtTileBackup.gif"));
break;
case('@'):
System.out.println("Character, @");
image = tk.getImage(getURL("Character.gif"));
break;
case('M'):
System.out.println("Monster, !");
image = tk.getImage(getURL("metal3.jpg"));
break;
default:
System.out.println("defualt,?");
image = tk.getImage(getURL("DefualtTileBackup.gif"));
break;
// end default
}// end switch
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}//end method
}// end class
Программа работает правильно, ноЯ не получаю полосы прокрутки.