Java图形编程绘制图像错误
您好,我在画框上绘制图像时出错。我不确定这里出了什么问题 我在这里得到以下错误Java图形编程绘制图像错误,java,swing,user-interface,compiler-errors,scope,Java,Swing,User Interface,Compiler Errors,Scope,您好,我在画框上绘制图像时出错。我不确定这里出了什么问题 我在这里得到以下错误 Java: 77: cannot find symbol symbol: variable image location: class DrawComponent g.drawImage(image, 0, 0, null); class DrawComponent extends JComponent { public void paintComponent(Graphics g) {
Java: 77: cannot find symbol
symbol: variable image
location: class DrawComponent
g.drawImage(image, 0, 0, null);
class DrawComponent extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// draw a circle with the same center
double centerX = 250;
double centerY = 180;
double radius = 20;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
g2.setPaint(Color.RED);
g2.fill(circle);
g2.draw(circle);
String filename = "SydneyOperaHouse.jpeg";
try{
Image image = ImageIO.read(new File(filename));
}catch(IOException ex){
// Handle Exeption
}
g.drawImage(image, 0, 0, null);
}
}
任何帮助都非常有用:)您只需在try块中声明图像变量。。。它在外面是看不见的
try{
Image image = ImageIO.read(new File(filename));
}catch(IOException ex){
// Handle Exeption
}
g.drawImage(image, 0, 0, null);
变量image
的输入错误。请注意,您正在try
-块中声明变量。变量不存在于try
-块的{…}
之外
在try
-块之外声明变量:
Image image = null;
try {
image = ImageIO.read(new File(filename));
} catch(IOException ex) {
// Handle Exeption
}
if (image != null) {
g.drawImage(image, 0, 0, null);
}
顺便说一下,您不应该在paintComponent
方法中进行I/O操作。最好将图像加载到其他地方(例如,当应用程序启动时),将其存储在成员变量中,并在paintComponent
方法中使用
当您在
paintComponent
方法中加载图像时,它将在每次需要绘制组件时加载图像。这将使您的应用程序速度变慢。如果出现异常情况,您希望看到什么
String filename = "SydneyOperaHouse.jpeg";
try{
Image image = ImageIO.read(new File(filename));
}catch(IOException ex){
// Handle Exeption
}
g.drawImage(image, 0, 0, null);
您应该声明/initalise并在try{}
块中绘制
图像
属性交给(或加载到)构造函数,并作为绘制方法可见的类属性存储。切勿尝试使用此方法加载图像(或执行其他可能长时间运行的任务)李>
JComponent
是一个ImageObserver
sog.drawImage(image,0,0,null)代码>应为
g.drawImage(图像,0,0,this)代码>
+所有分数为1。也许您应该补充一点,解决点1也解决了他的范围问题
import java.awt.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
public class DrawComponent extends JComponent {
private Image image;
DrawComponent(Image image) {
this.image = image;
Dimension d = new Dimension(image.getWidth(this),image.getHeight(this));
this.setPreferredSize(d);
}
public void paintComponent(Graphics g) {
// always call super first, to get borders etc.
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// paint the BG
g.drawImage(image, 0, 0, this);
// draw a circle with the same center
double centerX = 250;
double centerY = 180;
double radius = 20;
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
g2.setPaint(Color.RED);
g2.fill(circle);
g2.draw(circle);
}
public static void main(String[] args) throws Exception {
String s = "http://pscode.org/media/citymorn1.jpg";
final Image image = ImageIO.read(new URL(s));
Runnable r = new Runnable() {
@Override
public void run() {
JComponent gui = new DrawComponent(image);
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}