关于动画的问题 我在一个Panel上画了背景 再加了两个Panel 用不同的线程在子Panel上显示动画,而动画每显示一祯都要擦除上一祯的,有什么办法在擦除的时候不把背景给擦掉了啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 背景画到JFrame中,动画在JPanel中,将JPanel设置透明,加入到JFrame里也可以使用层,看你喜欢了 弱弱的问下 怎样将JPanel 设透明啊 用哪个方法? 还有怎么样才能在JFrame中画图呢 好象只能在JPanel上画吧? 给你写个例子:import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import javax.swing.JFrame;import javax.swing.JPanel;public class Test extends JFrame { private JPanel framePane = null; // 画背景图片的JPanel private MovePane move = null; // 画动画的类,继承:JPanel,接口:Runnable public Test() { super("Test"); move = new MovePane(); framePane = new JPanel() { public void paintComponent(Graphics g) { g.setColor(new Color(100, 155, 100)); g.fillRect(0, 0, 300, 200); g.setColor(new Color(80, 135, 80)); g.setFont(new Font("黑体", 1, 25)); g.drawString("画背景图片", 80, 30); } }; framePane.setLayout(new BorderLayout()); // 设置为JFrame所用的BorderLayout this.setContentPane(framePane); // 用重写了paintComponent(Graphics g)方法的JPanel替换JFrame本身的Container framePane.add(move); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 200); this.setVisible(true); } public static void main(String[] arg) { new Test(); } class MovePane extends JPanel implements Runnable { private Thread thread = null; private int i = 0; public MovePane() { this.setOpaque(false); thread = new Thread(this); thread.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(new Color(0, 0, 0)); g2d.drawString("这里用一个线程来画动画", i += 10, 100); } public void run() { while (true) { this.repaint(); if (i == 150) i = 0; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }} 一个简单的算法问题 把程序放在后台跑,就一定要 extends Thread 吗? repaint的行为 class中static的用法 质数(java两个问题) 初学java,谁能为我解惑? 请教关于“java内部类”??? Together6.1中文教程(TCC6.1版) 关于管道流的三个非常简单的问题: Applet可以建Socket吗 默认构造函数中一般写一些很么呀? 1.5新语法 @Task 到哪里能查到相关信息
也可以使用层,看你喜欢了
好象只能在JPanel上画吧?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;import javax.swing.JFrame;
import javax.swing.JPanel;public class Test extends JFrame {
private JPanel framePane = null; // 画背景图片的JPanel private MovePane move = null; // 画动画的类,继承:JPanel,接口:Runnable public Test() {
super("Test");
move = new MovePane();
framePane = new JPanel() {
public void paintComponent(Graphics g) {
g.setColor(new Color(100, 155, 100));
g.fillRect(0, 0, 300, 200);
g.setColor(new Color(80, 135, 80));
g.setFont(new Font("黑体", 1, 25));
g.drawString("画背景图片", 80, 30);
}
};
framePane.setLayout(new BorderLayout()); // 设置为JFrame所用的BorderLayout
this.setContentPane(framePane); // 用重写了paintComponent(Graphics g)方法的JPanel替换JFrame本身的Container
framePane.add(move);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
} public static void main(String[] arg) {
new Test();
} class MovePane extends JPanel implements Runnable {
private Thread thread = null;
private int i = 0; public MovePane() {
this.setOpaque(false);
thread = new Thread(this);
thread.start();
} public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(0, 0, 0));
g2d.drawString("这里用一个线程来画动画", i += 10, 100);
} public void run() {
while (true) {
this.repaint();
if (i == 150)
i = 0;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}}