我正在玩图形类。这是我在某个位置绘制图像的代码:
public class PixelManager extends JPanel {
private ArrayList<Pixel> pixels = new ArrayList<Pixel>();
public PixelManager() { }
public void addPixel(int posX, int posY, BufferedImage texture) {
pixels.add(new Pixel(posX, posY, texture));
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Pixel pixel : pixels) {
g2d.drawImage(pixel.getTexture(), pixel.getX(), pixel.getY(), 20, 20, null);
}
g2d.dispose();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400,400);
}
}
说明:我将此类用作像素或纹理管理器。在 ArrayList 中,我保存了一些像素,其中包括位置和纹理。在 de paintComponent Method 中,我正在遍历 ArrayList 并尝试绘制它们。
像素等级:
public class Pixel extends JPanel{
private int _posX, _posY;
private static int _width = 20;
private BufferedImage _texture;
public Pixel(int posX, int posY, BufferedImage texture) {
_posX = posX;
_posY = posY;
_texture = texture;
}
//Getter und Setter
public int getPosY() {
return _posY;
}
public int getPosX() {
return _posX;
}
public void setPosY(int posY) {
_posY = posY;
}
public void setPosX(int posX) {
_posX = posX;
}
public BufferedImage getTexture() {
return _texture;
}
public void setTexture(BufferedImage texture) {
_texture = texture;
}
}
和 JFrame 扩展类:
public class Zeichnen extends JFrame {
static PixelManager pman;
public Zeichnen()
{
setTitle("Tutorial");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
Zeichnen fenster = new Zeichnen();
pman = new PixelManager();
fenster.add(pman);
try {
pman.addPixel(100, 100, ImageIO.read(new File("C:\Users\Cronax3\Documents\grass.jpg")));
pman.addPixel(200, 200, ImageIO.read(new File("C:\\Users\\Cronax3\\Documents\\blue.png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
所以这就是问题所在。这两个图像将被绘制到面板/框架,但始终绘制到位置 x=0,y=0。我调试了所有内容,直到这一行:
g2d.drawImage(pixel.getTexture(), pixel.getX(), pixel.getY(), 20, 20, null);
像素提供的值是正确的。drawImage 将接收正确的位置和正确的纹理/图像。
因为我是图形新手,我想请你们帮助我解决这个问题。
提前非常感谢!
格莱兹克罗纳克斯3
所以,你的基本问题归结为...
public static class Pixel extends JPanel {
为什么Pixel
需要从JPanel
扩展?
如果你删除extends JPanel
,它将触发编译错误,说"找不到符号getX
">等,但这不是你定义的方法,getPosX
。
getX
是JPanel
的属性,但由于我认为没有必要以这种方式使用JPanel
,因此根本不需要它。 事实上,你可以Pixel
画自己,但这是另一天的讨论
简短的回答:
使用g2d.drawImage(pixel.getTexture(), pixel.getPosX(), pixel.getPosY(), 20, 20, null);
而不是g2d.drawImage(pixel.getTexture(), pixel.getX(), pixel.getY(), 20, 20, null);
(使用Pixel
中分配给posX
和posY
的值(
长而更好的答案:实现MadProgrammer和Andrew Thompson的建议(见评论(:
//see :https://stackoverflow.com/help/mcve
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Zeichnen extends JFrame {
PixelManager pman;
public Zeichnen(){
setTitle("Tutorial");
//setSize(400, 400); //not needed. see pack()
pman = new PixelManager();//add pixle manager to frame
add(pman);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack(); //Causes this Window to be sized to fit the preferred size of content
}
//add this method to allow adding pixels
void addPixel(int posX, int posY, BufferedImage texture) {
pman.addPixel(posX, posY, texture);
}
public static void main(String[] args)
{
Zeichnen fenster = new Zeichnen();
try {
//to make it mcve use links as suggested by Andrew Thompson
fenster.addPixel(100, 100, ImageIO.read(new URL("https://i.stack.imgur.com/gYxHm.png")));
fenster.addPixel(200, 200, ImageIO.read(new URL("https://i.stack.imgur.com/5v2TX.png")));
} catch (Exception e) { e.printStackTrace(); }
}
}
//as posinted out by MadProgrammer this class is just a data structure to hold
//pixel information. It does not need to extend JPanel
class Pixel {
private int _posX, _posY;
private BufferedImage _texture;
public Pixel(int posX, int posY, BufferedImage texture) {
_posX = posX;
_posY = posY;
_texture = texture;
}
//Getter und Setter
public int getPosY() { return _posY; }
public int getPosX() { return _posX; }
public void setPosY(int posY) { _posY = posY; }
public void setPosX(int posX) { _posX = posX; }
public BufferedImage getTexture() { return _texture; }
public void setTexture(BufferedImage texture) { _texture = texture; }
}
class PixelManager extends JPanel {
private ArrayList<Pixel> pixels = new ArrayList<>();
PixelManager() { }
void addPixel(int posX, int posY, BufferedImage texture) {
pixels.add(new Pixel(posX, posY, texture));
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Pixel pixel : pixels) {
g2d.drawImage(pixel.getTexture(), pixel.getPosX(), pixel.getPosY(), 20, 20, null);
}
g2d.dispose();
}
@Override
public Dimension getPreferredSize() { return new Dimension(400,400); }
}
谢谢大家的回答。是的,这是我的一个愚蠢的错误。Pixel并不打算扩展JPanel。添加此行是为了测试一些东西,我忘了删除它-.- 正因为如此,我没有注意到 getX(( 也是错误的。Totaly 搞砸了,因为我没有负责清理我的测试 Pixel 类。
谢谢你们!