2个回答
展开全部
String []mapimg={"source/font.png","source/ks.jpg","source/1.jpg","source/2.png",
"source/3.jpg","source/youknow.jpg","fight/js.jpg","fight/js1.jpg"};
//获得工具包
Toolkit tool=Toolkit.getDefaultToolkit()
//开始图片
Image startimg;
//背景图片
Image bjimg,fontimg;
//画开始界面
public void drawstart(Graphics g){
startimg=tool.getImage(mapimg[1]);
g.drawImage(startimg, 0, 0, frame.getWidth(), frame.getHeight(), frame);
fontimg=tool.getImage(mapimg[0]);
g.drawImage(fontimg, 20, 30, frame);
};
这是我以前做的一个很简单的游戏中的几行代码,对你的问题,你要手动选择图片,那做个文件选择器,把图片路径作为方法参数传进去就ok了
"source/3.jpg","source/youknow.jpg","fight/js.jpg","fight/js1.jpg"};
//获得工具包
Toolkit tool=Toolkit.getDefaultToolkit()
//开始图片
Image startimg;
//背景图片
Image bjimg,fontimg;
//画开始界面
public void drawstart(Graphics g){
startimg=tool.getImage(mapimg[1]);
g.drawImage(startimg, 0, 0, frame.getWidth(), frame.getHeight(), frame);
fontimg=tool.getImage(mapimg[0]);
g.drawImage(fontimg, 20, 30, frame);
};
这是我以前做的一个很简单的游戏中的几行代码,对你的问题,你要手动选择图片,那做个文件选择器,把图片路径作为方法参数传进去就ok了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自定义界央的背景图片?
---------------------------------------------------------------------------
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class FrameTest extends JFrame implements ActionListener {
private ImagePanel panel = new ImagePanel();
private JFileChooser chooser = new JFileChooser();
public FrameTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JButton btnNewButton = new JButton("Click");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(12, 10, 93, 23);
panel.add(btnNewButton);
setVisible(true);
}
public static void main(String[] args) {
new FrameTest();
}
public void actionPerformed(ActionEvent actionevent) {
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
panel.setPath(chooser.getSelectedFile().getPath());
panel.repaint();
}
}
}
class ImagePanel extends JPanel {
private String path = null;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (path != null) {
ImageIcon icon = new ImageIcon(path);
g.drawImage(icon.getImage(), 0, 0, 400, 300, null);
}
}
}
---------------------------------------------------------------------------
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class FrameTest extends JFrame implements ActionListener {
private ImagePanel panel = new ImagePanel();
private JFileChooser chooser = new JFileChooser();
public FrameTest() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JButton btnNewButton = new JButton("Click");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(12, 10, 93, 23);
panel.add(btnNewButton);
setVisible(true);
}
public static void main(String[] args) {
new FrameTest();
}
public void actionPerformed(ActionEvent actionevent) {
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
panel.setPath(chooser.getSelectedFile().getPath());
panel.repaint();
}
}
}
class ImagePanel extends JPanel {
private String path = null;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (path != null) {
ImageIcon icon = new ImageIcon(path);
g.drawImage(icon.getImage(), 0, 0, 400, 300, null);
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询