求解~java窗体显示图片和画图问题
packageframe;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importj...
package frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class FiveChessFrame extends JFrame implements MouseListener, Runnable{
// 取得屏幕的宽度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// 取得屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
public FiveChessFrame(){
this.setTitle("五子棋");
this.setSize(500, 500);
this.setLocation((width - 500)/2 , (height - 500)/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
String imagePath = System.getProperty("user.dir")+"/bin/background.jpg" ;
File f = new File("bin/background.jpg");
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println(f.getAbsolutePath());
BufferedImage bgImage = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
this.setVisible(true);
this.repaint();
}
public void paint(Graphics gg){
BufferedImage bi =new BufferedImage(500 , 500 ,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
//g.drawImage(img, x, y, width, height, observer)
g.setColor(Color.black);
g.setFont(new Font("宋体" , 0 ,14));
g.drawString("黑方时间:" , 30 , 470);
g.setFont(new Font("黑体" , Font.BOLD , 14));
g.drawString("白方时间" , 260 , 470);
for(int i = 0 ; i < 10 ; i++){
g.drawLine(20 , 80 + 40 * i , 380 , 80 + 40 * i);
g.drawLine(20 + 40 * i , 80 , 20 + 40 * i , 440);
}
gg.drawImage(bi , 0 , 0 ,this);
}
}
//主类文件
import frame.FiveChessFrame;
public class LoginDefault {
public static void main(String[] args) {
FiveChessFrame fcf = new FiveChessFrame();
}
}
为什么只显示了paint()函数的画图效果,却没有背景图片?求解~
呃,public class FiveChessFrame extends JFrame implements MouseListener, Runnable多了Runnable,忘记删了。 展开
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class FiveChessFrame extends JFrame implements MouseListener, Runnable{
// 取得屏幕的宽度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// 取得屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
public FiveChessFrame(){
this.setTitle("五子棋");
this.setSize(500, 500);
this.setLocation((width - 500)/2 , (height - 500)/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
String imagePath = System.getProperty("user.dir")+"/bin/background.jpg" ;
File f = new File("bin/background.jpg");
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println(f.getAbsolutePath());
BufferedImage bgImage = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
this.setVisible(true);
this.repaint();
}
public void paint(Graphics gg){
BufferedImage bi =new BufferedImage(500 , 500 ,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
//g.drawImage(img, x, y, width, height, observer)
g.setColor(Color.black);
g.setFont(new Font("宋体" , 0 ,14));
g.drawString("黑方时间:" , 30 , 470);
g.setFont(new Font("黑体" , Font.BOLD , 14));
g.drawString("白方时间" , 260 , 470);
for(int i = 0 ; i < 10 ; i++){
g.drawLine(20 , 80 + 40 * i , 380 , 80 + 40 * i);
g.drawLine(20 + 40 * i , 80 , 20 + 40 * i , 440);
}
gg.drawImage(bi , 0 , 0 ,this);
}
}
//主类文件
import frame.FiveChessFrame;
public class LoginDefault {
public static void main(String[] args) {
FiveChessFrame fcf = new FiveChessFrame();
}
}
为什么只显示了paint()函数的画图效果,却没有背景图片?求解~
呃,public class FiveChessFrame extends JFrame implements MouseListener, Runnable多了Runnable,忘记删了。 展开
2个回答
展开全部
BufferedImage bi = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(bgImage, bgImage.getWidth(), bgImage.getHeight(),null);
bgImage 就是你的背景图片, 把 bgImage 变量放到构造函数外面
更多追问追答
追问
怎么在构造函数外面把图片赋给bgimage?
OnlyGetBackground bg = new OnlyGetBackground();
class OnlyGetBackground{
public OnlyGetBackground(){
try {
File f = new File("bin/background.jpg");
bgImage = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我在FiveChessFrame类里又弄了一个类,在调试的时候,bgImage已经读取到了,为什么还是只显示线条而已?
追答
把
File f = new File("bin/background.jpg");
BufferedImage bgImage = ImageIO.read(f);
把这2行代码放到
public FiveChessFrame() 的上面
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询