[英]Drawing imageS in java using AWT
EDIT: First question solved, see Roberto Attias 's answer and maybe read the comment . 编辑:第一个问题已解决,请参见Roberto Attias的回答,并可能阅读评论。 Still there's the second issue.
仍然有第二个问题。
I have to do a small 2D game in Java and I want to stick to AWT as much as possible, ( so no SWING nor Java2D unless absolutely necessary). 我必须用Java编写一个小型2D游戏,并且我想尽可能地坚持使用AWT(因此除非绝对必要,否则不要使用SWING或Java2D)。
I have a window and I can draw an image on it but there's two issues. 我有一个窗口,可以在其上绘制图像,但是有两个问题。 First I can't draw more than one Image.
首先,我不能绘制多个图像。 In fact with some of my test when in debug I can see that my program will draw my two images only to delete them and re-draw the first image.
实际上,在调试时通过一些测试,我可以看到我的程序将绘制两个图像只是为了删除它们并重新绘制第一个图像。 Second, that first image which is re-drawn is not at the coordinate it should ( its slightly on the left and on below )
第二,重新绘制的第一张图片不在它应该的坐标上(稍微在左侧和下方)
For now I have something like that: 现在我有这样的事情:
public class AwtManager {
private Frame frame;
private Panel panel;
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
// here I tried to setBounds to solve my second issue, it didn't work
//panel.setBounds(0, 0, 800, 600);
frame.add(panel);
frame.setVisible(true);
}
This part open my windows and works quite nicely but my second issue seems to be caused by the borders of my frame / panel so there might be some changement to do here. 这部分打开了我的窗户,并且工作得很好,但是我的第二个问题似乎是由框架/面板的边框引起的,因此这里可能需要做一些更改。
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i);
//And this is where I'm lost
}
// or here maybe
frame.setVisible(true);
}
}
In this second part I tried every combination of Component.paint(g), Component.update(g), Component.repaint(), Component.setVisible(true) I could think of. 在第二部分中,我尝试了我能想到的Component.paint(g),Component.update(g),Component.repaint(),Component.setVisible(true)的每种组合。
My object ImageComponent is simply this: 我的对象ImageComponent就是这样:
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
This function getPreferredSize() disturbs me like hell, it should return the preferred size of the ImageComponent but apparently I have to put the size of my frame/ panel otherwise it won't work. 这个函数getPreferredSize()像打扰我一样困扰我,它应该返回ImageComponent的首选大小,但是显然我必须放置框架/面板的大小,否则它将无法工作。
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
And finally my paint, update, repaint: 最后是我的绘画,更新和重绘:
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
I highly doubt that those three look like what they should but I find the documents on the subject very hard to understand. 我非常怀疑这三个文件看起来应该是什么,但是我发现关于该主题的文件很难理解。
So pleas, could you help me with those issues, and by the way if you know how Component.setVisible(boolean) works i would like to know because in debug mod, this function made me loose some hair. 因此,请帮我解决那些问题,顺便说一句,如果您知道Component.setVisible(boolean)的工作方式,我想知道,因为在调试mod中,此功能使我有些松懈。
EDIT: 编辑:
Here's a screenshot of my window knowing that I asked for two red square (there are Images not Rectangle), one a 0,0, the other at 200, 200. 这是我的窗口的屏幕截图,知道我要了两个红色正方形(有不是矩形的图像),一个是0,0,另一个是200、200。
EDIT 2: Here's a fully runnable code (i think): 编辑2:这是一个完全可运行的代码(我认为):
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class AwtManager {
private Frame frame;
private Panel panel;
public static void main(String args[]) {
new AwtManager();
ArrayList<ImageComponent> images = new ArrayList<>();
images.add(new ImageComponent("myimage.jpg", 0, 0));
images.add(new ImageComponent("myimage.jpg", 200, 200));
showMytwoImagesFFS(images);
}
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
frame.add(panel);
frame.setVisible(true);
}
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i));
}
frame.setVisible(true);
}
}
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
Just a quick thought, but do you know the visibility of each of your images in that ArrayList? 只是一个简单的想法,但是您知道每个图像在ArrayList中的可见性吗? Ie
即
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
images.get(i).setVisible(true);//is the visibility actually true?
panel.add(images.get(i);
}
// or here maybe
frame.setVisible(true);
}
Also, do you have screenshots of how your program looks at the moment? 另外,您是否有有关程序当前外观的屏幕截图? With gui problems, I find they're easier to solve if I can see what's going on.
对于gui问题,如果我能看到正在发生的事情,我发现它们更容易解决。
This is your code, slightly modified to run. 这是您的代码,稍作修改即可运行。 In the future, please post fully runnable examples.
将来,请发布完全可运行的示例。
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class AwtManager {
private Frame frame;
private Panel panel;
public static void main(String args[]) {
new AwtManager();
}
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
// here I tried to setBounds to solve my second issue, it didn't work
//panel.setBounds(0, 0, 800, 600);
frame.add(panel);
ArrayList<ImageComponent> images = new ArrayList<>();
images.add(new ImageComponent("myimage.jpg", 0, 0));
showMytwoImagesFFS(images);
frame.setVisible(true);
}
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i));
}
}
}
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.