Java 移动球
我试着制作一个程序,让球在屏幕上自动移动。但问题是它不做重绘() 有什么建议可以解决吗 (主类)main.java:Java 移动球,java,swing,Java,Swing,我试着制作一个程序,让球在屏幕上自动移动。但问题是它不做重绘() 有什么建议可以解决吗 (主类)main.java: import javax.swing.*; import java.awt.*; public class Main extends JFrame{ static int x = 10; public static void main(String[] args){ JFrame f = new JFrame("title");
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
static int x = 10;
public static void main(String[] args){
JFrame f = new JFrame("title");
f.setVisible(true);
f.setSize(300,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sekon m = new sekon();
f.add(m);
antr t = new antr();
Thread th = new Thread(t);
th.start();
}
}
(第二类)sekon.java:
import javax.swing.*;
import java.awt.*;
public class sekon extends JPanel{
int xiu = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(xiu, 10, 20, 20);
}
public void changeX(int b){
this.xiu = b;
}
}
class antr extends JPanel implements Runnable{
int xi = 10;
sekon s = new sekon();
public void run(){
xi += 1;
s.changeX(xi);
JPanel p = new JPanel();
p.repaint();
try{
Thread.sleep(5);
}catch(Exception e){}
}
}
antr
对象拥有的sekon
实例与添加到GUI的实例不同。antr对象拥有的sekon
实例与添加到GUI的实例不同。您的类antr和主类使用不同的sekon实例。您可能应该将在main方法中创建的实例传递给antr类。您的类antr和main类使用不同的sekon实例。您可能应该将在main方法中创建的实例传递给antr类。1)您的代码中有repaint()
被代码行线程阻塞。sleep(5)代码>
2) 您的代码不起作用,因为遗漏了…,在屏幕上移动Oval
的所有坐标
3) 对于Swing容器,Swing JComponent仅用于处理、移动、重新绘制、
通过使用Runnable#Thread
可以确定,但不是这样
关于摆动计时器的示例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationJPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int cx = 0;
private int cy = 150;
private int cw = 20;
private int ch = 20;
private int xinc = 1;
private int yinc = 1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
AnimationJPanel panel = new AnimationJPanel();
panel.setPreferredSize(new Dimension(400, 300));
panel.animate();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public AnimationJPanel() {
setLayout(new BorderLayout());
JLabel label = new JLabel("This is an AnimationJPanel");
label.setForeground(Color.RED);
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label);
setBackground(Color.BLACK);
setForeground(Color.RED);
setOpaque(true);
}
public void animate() {
new Timer(15, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle oldCircle = new Rectangle(cx - 1, cy - 1, cw + 2, ch + 2);
cx += xinc;
cy += yinc;
if (cx >= getWidth() - cw || cx <= 0) {
xinc *= -1;
}
if (cy >= getHeight() - ch || cy <= 0) {
yinc *= -1;
}
repaint(oldCircle);
repaint(cx - 1, cy - 1, cw + 2, ch + 2);
}
}).start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(cx, cy, cw, ch);
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类AnimationJPanel扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私有int cx=0;
私人int cy=150;
私人int cw=20;
私有int ch=20;
私有int xinc=1;
私有整数yinc=1;
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
AnimationJPanel面板=新建AnimationJPanel();
面板。设置首选尺寸(新尺寸(400300));
panel.animate();
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(面板);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共动画jpanel(){
setLayout(新的BorderLayout());
JLabel标签=新的JLabel(“这是一个动画JPanel”);
标签。设置前景(颜色。红色);
标签.设置水平对齐(旋转角度.中心);
添加(标签);
挫折背景(颜色:黑色);
设置前景(颜色为红色);
set不透明(true);
}
public void animate(){
新计时器(15,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
矩形oldCircle=新矩形(cx-1、cy-1、cw+2、ch+2);
cx+=xinc;
cy+=yinc;
如果代码中的(cx>=getWidth()-cw | | cx=getHeight()-ch | | cy1)被代码行Thread.sleep(5);
2) 您的代码不起作用,因为遗漏了…,在屏幕上移动Oval
的所有坐标
3) 对于Swing容器,Swing JComponent仅用于处理、移动、重新绘制、
通过使用Runnable#Thread
可以确定,但不是这样
关于摆动计时器的示例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationJPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int cx = 0;
private int cy = 150;
private int cw = 20;
private int ch = 20;
private int xinc = 1;
private int yinc = 1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
AnimationJPanel panel = new AnimationJPanel();
panel.setPreferredSize(new Dimension(400, 300));
panel.animate();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public AnimationJPanel() {
setLayout(new BorderLayout());
JLabel label = new JLabel("This is an AnimationJPanel");
label.setForeground(Color.RED);
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label);
setBackground(Color.BLACK);
setForeground(Color.RED);
setOpaque(true);
}
public void animate() {
new Timer(15, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle oldCircle = new Rectangle(cx - 1, cy - 1, cw + 2, ch + 2);
cx += xinc;
cy += yinc;
if (cx >= getWidth() - cw || cx <= 0) {
xinc *= -1;
}
if (cy >= getHeight() - ch || cy <= 0) {
yinc *= -1;
}
repaint(oldCircle);
repaint(cx - 1, cy - 1, cw + 2, ch + 2);
}
}).start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(cx, cy, cw, ch);
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类AnimationJPanel扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私有int cx=0;
私人int cy=150;
私人int cw=20;
私有int ch=20;
私有int xinc=1;
私有整数yinc=1;
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
AnimationJPanel面板=新建AnimationJPanel();
面板。设置首选尺寸(新尺寸(400300));
panel.animate();
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(面板);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共动画jpanel(){
setLayout(新的BorderLayout());
JLabel标签=新的JLabel(“这是一个动画JPanel”);
标签。设置前景(颜色。红色);
标签.设置水平对齐(旋转角度.中心);
添加(标签);
挫折背景(颜色:黑色);
设置前景(颜色为红色);
set不透明(true);
}
public void animate(){
新计时器(15,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
矩形oldCircle=新矩形(cx-1、cy-1、cw+2、ch+2);
cx+=xinc;
cy+=yinc;
如果(cx>=getWidth()-cw | | cx=getHeight()-ch | | cy请研究以下说明示例,了解如何导致重新绘制动画
public static void main(String args[]) throws Exception {
new JFrame("AnimationStudy") {
int x = 0;
JPanel j = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, 10, 20, 20);
g.setColor(Color.BLACK);
g.drawChars(("" + x).toCharArray(), 0, ("" + x).length(), x, 10);
}
};
{
setSize(300, 100);
setLocation(300, 300);
setVisible(true);
setLayout(new BorderLayout());
add(j);
new Thread(new Runnable() {
public void run() {
int t = 250;
for (x = 10; x < t; x += 1) {
j.repaint();
try {
Thread.sleep((t - x) / 4);
} catch (Exception e) {
e.printStackTrace();
}
}
System.exit(0);
}
}).start();
}
};
}
publicstaticvoidmain(字符串args[])引发异常{
新JFrame(“动画研究”){
int x=0;
JPanel j=新的JPanel(){
受保护组件(图形g){
超级组件(g);
g、 setColor(Color.RED);
g、 椭圆形(x,10,20,20);
g、 设置颜色(颜色为黑色);
g、 drawChars((“”+x).tocharray(),0,(“”+x).length(),x,10);
}
};
{
设置大小(300100);
设置位置(300300);
setVisible(真);
设置布局(新B