gpt4 book ai didi

java - 在java中传递图像

转载 作者:行者123 更新时间:2023-12-02 05:55:55 26 4
gpt4 key购买 nike

有人可以帮我解决我遇到的这个错误吗?当我尝试传递图像时,它给了我错误。这是我项目的一部分,我完成了其余部分,但主屏幕有问题。

这是错误:

    Project.java:36: cannot find symbol
symbol : method drawImage(java.awt.Image)
location: class java.awt.Graphics
g.drawImage(img);
^
1 error

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.

这是完整的程序:

import javax.imageio.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Project extends JPanel
implements KeyListener, MouseListener, MouseMotionListener {

char shape = 'r';
int x = 0;
int y = 0;

Image img;

boolean start = false;
boolean help = false;
boolean Player1 = false;
boolean Player2 = false;

public Project() {

img = Toolkit.getDefaultToolkit().getImage("mp.jpg");
setFocusable(true);
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setSize(400, 400);
}

public void paintComponent(Graphics g) {
// BG
Dimension d = getSize();
g.setColor(getBackground());

// IMAGE ************************************* ERROR
g.drawImage(img);

g.setColor(Color.black);
// Help
g.setFont(new Font("default", Font.BOLD, 12));
if (x >= 900 && x <= 950 && y >= 600 && y <= 650 && start == false) {
g.drawString("Press START to", 960, 620);
g.drawString("start the game.", 960, 635);
}

if (x >= 900 && x <= 950 && y >= 600 && y <= 650 && start) {
g.drawString("Choose a Player", 960, 620);
}

else {
g.setColor(Color.red);
g.fillOval(900, 600, 50, 50);
g.setFont(new Font("default", Font.BOLD, 45));
g.setColor(Color.cyan);
g.drawString("?", 915, 640);
}
// Help
g.setColor(Color.black);
g.fillRect(550, 555, 97, 50);
g.setFont(new Font("default", Font.BOLD, 30));
g.setColor(Color.cyan);
g.drawString("Start", 559, 590);
// Start
if (start) {

g.setColor(Color.black);
g.fillRect(400, 550, 400, 55);
g.setFont(new Font("default", Font.BOLD, 30));
g.setColor(Color.yellow);
g.drawString("PLayer 1", 425, 585);
g.drawString("Player 2", 645, 585);
}

}

public void keyTyped(KeyEvent e) {
}

public void keyPressed(KeyEvent e) {

shape = e.getKeyChar();
repaint();
}

public void keyReleased(KeyEvent e) {
}

public void mouseEntered(MouseEvent e) {

if (x >= 800 && x <= 850 && y >= 600 && y <= 650) {
help = true;
}
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {// ****** Players Button *******

// Variables
Player1 = false;
Player2 = false;
// Levels
if (start) {
if (x >= 427 && x <= 555 && y >= 564 && y <= 589) {
Player1 = true;
}

if (x >= 648 && x <= 769 && y >= 564 && y <= 587) {
Player2 = true;

}
}

if (x >= 550 && x <= 650 && y >= 560 && y <= 607) {
start = true;
}
else {
start = false;
}

}

public void mouseDragged(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}

public static void main(String args[]) {

JFrame f = new JFrame("Project");
Project dc = new Project();
f.getContentPane().add(dc);
f.setSize(1200, 775);
f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}

}

最佳答案

看看图形 API:http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html

该类不包含仅获取图像的方法。您可能想要更接近此的东西:

g.drawImage(image, x, y, this);

其中image是要绘制的图像,x和y是位置,这是正在绘制的JPanel,它是一个ImageObserver。

Graphics 类中还有其他drawImage() 方法也采用其他参数。

关于java - 在java中传递图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23086165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com