Java程序 实现图形面板上的字幕滚动效果

1.自定义一个继承自JPanel的类MyJPanel,重写MyJPanel类的paintComponent方法,在此方法内部完成诗句的绘制。2.创建一个MyJPanel对... 1. 自定义一个继承自JPanel的类MyJPanel,重写MyJPanel类的paintComponent方法,在此方法内部完成诗句的绘制。
2. 创建一个MyJPanel对象,替换Jframe默认的内容面板。(setContentPane方法)。
3. 在程序主线程中创建一个新的线程,此线程不断的刷新MyJPanel。刷新时会自动调用paintComponent方法,从而完成界面的更新。
MyJPanel类编程指导:
1. 定义成员变量sentences保存要诗词: String[] sentences = { "滚滚长江东逝水", "浪花淘尽英雄","是非成败转头空", "青山依旧在", "几度夕阳红","白发渔樵江渚上", "惯看秋月春风","一壶浊酒喜相逢", "古今多少事", "都付笑谈中" };
2. 将MyJPanel组件区域按字体大小划分成固定列数,每列用于显示一条诗句。本部分功能在paintComponent方法中完成。因为一个组件只有开始绘制时JVM才能确定其大小位置等。
a) 申明两个成员变量:int[]cols;Graphics canvas;,cols用于保存每列的起始横坐标,canvas指向当前组件的图形绘制对象,用Jcomponent类的getGraphics()可以获得这个图形绘制对象(以下称之为画布)。
b) 获取字的宽度过程是首先取得画布,创建字体对象并设置画布的字体,随后便可以获取字体在此图形上下文环境中字的尺寸了:
canvas = getGraphics();
Font font = new Font("华文行楷", Font.BOLD, 40);
canvas.setFont(font);
FontMetrics metrics =canvas.getFontMetrics(font);
Rectangle2D rect =metrics.getStringBounds("水",canvas);
int wordWidth = (int)rect.getWidth();
c) 计算区域总列数。计算方法是用组件的总宽度去除以每个字的宽度与间隙和。代码如下,其中wordGap是一个局部变量,表示两列间的间隙:
int totalCols = getWidth() / (wordWidth + wordGap);
cols = new int[totalCols];
for (int i = 0; i < cols.length; i++) {
cols[i] = i * (wordGap+ wordWidth);
}
展开
 我来答
1997排长
高粉答主

2016-01-10 · 说的都是干货,快来关注
知道大有可为答主
回答量:1.1万
采纳率:0%
帮助的人:801万
展开全部
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
public class mi
{
private static String username;
private static String password ;
private static JTextField []t={
new JTextField("账号:",8),new JTextField(10),
new JTextField("密码:",8),new JPasswordField(10)};

public static void main (String args[]){
JFrame app=new JFrame("账号密码演示程序");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(280,120);
Container c=app.getContentPane();
c.setLayout(new FlowLayout());

t[0].setEditable(false);
t[2].setEditable(false);
for(int i=0;i<4;i++)
c.add(t[i]);t[1].setText("");

JButton[]b={new JButton("确定"),new JButton("重置")};
c.add(b[0]);c.add(b[1]);
app.setLocationRelativeTo(null);
app.setVisible(true);

b[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t[1].setText("");
t[3].setText("");
}
});
// 登录按钮加事件监听器
b[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username = t[1].getText();
password = t[3].getText();
//判断用户名密码是否正确
if (username.equals("数字") && password.equals("123")) {
JOptionPane.showMessageDialog(null, "登陆成功!", "消息",
JOptionPane.INFORMATION_MESSAGE);

} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!", "错误",
JOptionPane.ERROR_MESSAGE);
}
}
}); }

}
匿名用户
2015-06-05
展开全部
楼主,简单说,转成图片,然后不断绘、不断改变x坐标
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Laughing强2013
推荐于2018-03-12
知道答主
回答量:3
采纳率:0%
帮助的人:2.5万
展开全部
package excise2;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class Demo {

public static void main(String[] args) {
// TODO Auto-generated method stubfg
final JFrame f = new JFrame();
f.setBounds(400, 400, 800, 400);
f.setTitle("滚动字幕演示");
f.setContentPane(new MyJpanel());
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
f.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
}
});
}

}

package excise2;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;

import javax.swing.JPanel;

public class MyJpanel extends JPanel {

// int i;
String[] sentences = { "滚滚长江东逝水", "浪花淘尽英雄", "是非成败转头空", "青山依旧在", "几度夕阳红","白发渔樵江渚上", "惯看秋月春风", "一壶浊酒喜相逢", "古今多少事", "都付笑谈中" };
int[] cols;
Graphics canvas;
int wordHeight;//fgf
private LinkedList<String> paintSentence;
int left,right;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
canvas=g;
// canvas=getGraphics();
Font f=new Font("华文行楷",Font.BOLD,40);
canvas.setFont(f);

FontMetrics metrics = canvas.getFontMetrics(f);
Rectangle2D rect = metrics.getStringBounds("水", canvas);
int wordWidth = (int) rect.getWidth();
wordHeight=(int)rect.getHeight();

int wordGap=10;
int totalCols = getWidth() / (wordWidth + wordGap);
cols = new int[totalCols];
for (int i = 0; i < cols.length; i++) {
cols[i] = i * (wordGap + wordWidth);
}
if(paintSentence==null){
paintSentence=new LinkedList<String>();
for (int i = 0; i < sentences.length; i++) {
paintSentence.addFirst(sentences[i]);
}
for (int i = 0; i < cols.length-sentences.length; i++) {
paintSentence.addFirst(null);
}
// while (!paintSentence.get(0).equals(sentences[0])) {
// moveRight();
// //paintSentence.addAll(sentences);
// }
}

drawColumns();
// g.drawString(String.valueOf(i++), this.getWidth()/2, getHeight()/2);
}

private void drawColumn(int col) {
int y = wordHeight;
String msg = paintSentence.get(col);
if (null == msg) {
return;
}
for (int i = 0; i < msg.length(); i++) {
canvas.drawString(String.valueOf(msg.charAt(i)), cols[col], y);
y += wordHeight;
}
}
private void drawColumns() {
for (int i = 0; i < cols.length; i++) {
drawColumn(i);
}
moveRight();
}

private void moveRight() {
paintSentence.addFirst(paintSentence.removeLast());
// 修改左右指针,刚开始时的情况left=0,right<诗句数,诗句全部加入进来后就循环右移
// if (left == 0 && right < sentences.length - 1) {
// right++;
// } else {
// left = (left + 1) % paintSentence.length;
// right = (right + 1) % paintSentence.length;
// }
//
// // 移动诗句
// if (right < left) {// 210nnnnnn9876543
// for (int i = right; i > 0; i--) {
// paintSentence[i] = paintSentence[i - 1];
// }
// paintSentence[0] = paintSentence[paintSentence.length - 1];
// for (int i = paintSentence.length - 1; i >= left; i--) {
// paintSentence[i] = paintSentence[i - 1];
// }
// paintSentence[left - 1] = null;
// } else if (left == 0 && right < sentences.length) {// 210nnnnnnnnnnnnnn
// for (int i = right; i > 0; i--) {
// paintSentence[i] = paintSentence[i - 1];
// }
// paintSentence[0] = sentences[right];
// if (right == sentences.length - 1)
// paintSentence[paintSentence.length - 1] = null;
// } else {// nnnnn9876543210nn
// for (int i = right; i >= left; i--) {
// paintSentence[i] = paintSentence[i - 1];
// }
// paintSentence[left - 1] = null;
// }
}

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式