
java问题图片覆盖子窗口且刷新时候不闪烁 100
packagecom.plants.vs.zombies.game;importcom.plants.vs.zombies.Frame.PlayFrame;publicc...
package com.plants.vs.zombies.game;
import com.plants.vs.zombies.Frame.PlayFrame;
public class Game {
//游戏窗口的引用
private PlayFrame playFrame;
public Game(){
this.setPlayFrame(new PlayFrame());
}
public static void main(String[] args) {
new Game();
}
public PlayFrame getPlayFrame() {
return playFrame;
}
public void setPlayFrame(PlayFrame playFrame) {
this.playFrame = playFrame;
}
}
package com.plants.vs.zombies.Frame;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
// 游戏窗口的面板
public class PlayPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int locationX = 200, locationY = -130;
private Image img;
static JInternalFrame[] frameSon;
JDesktopPane jp = new JDesktopPane();
SonPanel sonPanel;
int index = 0;
public PlayPanel(ImageIcon image) {
frameSon = new JInternalFrame[100];
img = image.getImage();
setLayout(new BorderLayout());
add(jp, BorderLayout.CENTER);
}
// 重画
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
if (frameSon[99] != null) {
// PlayPanel.frameSon[99].setVisible(false);
// PlayPanel.frameSon[99].setVisible(true);
}
}
// 添加子窗口
public void addInternalWindow() {
if (index < 100) {
frameSon[index] = new JInternalFrame("选择战士");
frameSon[index].setSize(335, 100);
frameSon[index].setLocation(locationX, locationY);
frameSon[index].setResizable(false);
frameSon[index].add(new SonPanel(), BorderLayout.CENTER);
jp.add(frameSon[index], index);
frameSon[index].setVisible(true);
if (index >= 1) {
frameSon[index - 1].setVisible(false);
}
++index;
locationY++;
}
if (index == 99 && frameSon[99] != null) {
frameSon[99].remove(99);
}
}
// 删除子窗口
public void deleteInternalWindow() {
if (index > 0) {
--index;
frameSon[index].setVisible(false);
jp.remove(frameSon[index]);
}
}
public static JInternalFrame[] getFrameSon() {
return frameSon;
}
public static void setFrameSon(JInternalFrame[] frameSon) {
PlayPanel.frameSon = frameSon;
}
// 线程刷新
private class MyThread implements Runnable {
public void run() {
while (true) {
repaint();
addInternalWindow();
try {
Thread.sleep(20);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void start() {
new Thread(new MyThread()).start();
}
private class SonPanel extends JPanel {
public void paintComponent(Graphics g) {
for (int x = 50; x < 350; x += 55) {
for (int y = 0; y < 100; y += 5) {
g.draw3DRect(x, y, 5, 5, true);
}
}
}
}
}
在PlayFrame中加入addFocusListener(new FocusListener(){
public void focusGained(FocusEvent f) {
pp.focusGained(f);
}
@Override
public void focusLost(FocusEvent f) {
pp.focusLost(f);
}
});
在PlayJPanel中加入public void focusGained(FocusEvent f) {
judge = true;
}
// 控制失去焦点
public void focusLost(FocusEvent f) {
judge = true;
}
在paint中加载图片修改为if(judge == true){
g.drawImage(img, 0, 0, this);
judge = false;
}
加载图片不覆盖子窗口解决了 为什么 我猛拉一下主窗口后图片会消失~~ 望帮忙解决 我会再追加50分 展开
import com.plants.vs.zombies.Frame.PlayFrame;
public class Game {
//游戏窗口的引用
private PlayFrame playFrame;
public Game(){
this.setPlayFrame(new PlayFrame());
}
public static void main(String[] args) {
new Game();
}
public PlayFrame getPlayFrame() {
return playFrame;
}
public void setPlayFrame(PlayFrame playFrame) {
this.playFrame = playFrame;
}
}
package com.plants.vs.zombies.Frame;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
// 游戏窗口的面板
public class PlayPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int locationX = 200, locationY = -130;
private Image img;
static JInternalFrame[] frameSon;
JDesktopPane jp = new JDesktopPane();
SonPanel sonPanel;
int index = 0;
public PlayPanel(ImageIcon image) {
frameSon = new JInternalFrame[100];
img = image.getImage();
setLayout(new BorderLayout());
add(jp, BorderLayout.CENTER);
}
// 重画
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
if (frameSon[99] != null) {
// PlayPanel.frameSon[99].setVisible(false);
// PlayPanel.frameSon[99].setVisible(true);
}
}
// 添加子窗口
public void addInternalWindow() {
if (index < 100) {
frameSon[index] = new JInternalFrame("选择战士");
frameSon[index].setSize(335, 100);
frameSon[index].setLocation(locationX, locationY);
frameSon[index].setResizable(false);
frameSon[index].add(new SonPanel(), BorderLayout.CENTER);
jp.add(frameSon[index], index);
frameSon[index].setVisible(true);
if (index >= 1) {
frameSon[index - 1].setVisible(false);
}
++index;
locationY++;
}
if (index == 99 && frameSon[99] != null) {
frameSon[99].remove(99);
}
}
// 删除子窗口
public void deleteInternalWindow() {
if (index > 0) {
--index;
frameSon[index].setVisible(false);
jp.remove(frameSon[index]);
}
}
public static JInternalFrame[] getFrameSon() {
return frameSon;
}
public static void setFrameSon(JInternalFrame[] frameSon) {
PlayPanel.frameSon = frameSon;
}
// 线程刷新
private class MyThread implements Runnable {
public void run() {
while (true) {
repaint();
addInternalWindow();
try {
Thread.sleep(20);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void start() {
new Thread(new MyThread()).start();
}
private class SonPanel extends JPanel {
public void paintComponent(Graphics g) {
for (int x = 50; x < 350; x += 55) {
for (int y = 0; y < 100; y += 5) {
g.draw3DRect(x, y, 5, 5, true);
}
}
}
}
}
在PlayFrame中加入addFocusListener(new FocusListener(){
public void focusGained(FocusEvent f) {
pp.focusGained(f);
}
@Override
public void focusLost(FocusEvent f) {
pp.focusLost(f);
}
});
在PlayJPanel中加入public void focusGained(FocusEvent f) {
judge = true;
}
// 控制失去焦点
public void focusLost(FocusEvent f) {
judge = true;
}
在paint中加载图片修改为if(judge == true){
g.drawImage(img, 0, 0, this);
judge = false;
}
加载图片不覆盖子窗口解决了 为什么 我猛拉一下主窗口后图片会消失~~ 望帮忙解决 我会再追加50分 展开
展开全部
先把你要画在屏幕上的东西画到一个BufferedImage里,BufferedImage的大小的屏幕绘图区大小一样.在BufferedImage里画好了之后,调用repaint();在重载的paint函数中里把这个BufferedImage用g画出来就可以了.
这做2D游戏用这个方法,一般都不闪.
这做2D游戏用这个方法,一般都不闪.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询