java jpanel调用构造函数的时候就开始执行repaint,如何使用Swing JPanel在Java中強制重新繪制?...

Why won't my image repaint for a simple animation? I call repaint() from two different methods and one causes repaint but the other doesn't. The method that does force the repaint is generated from an event listener. The one that doesn't is a timed animation thread. I know the animation thread is running correctly, and as long as I continuously slide the slider, it displays perfectly. Help please!

PS yes, I've seen many similar problems here, and I've tried validate, revalidate, and using paint vs paintComponent. Four classes that comprise the code follow:

為什么我的圖像不能重新繪制成一個簡單的動畫?我用兩種不同的方法調用repaint(),其中一個原因是重新油漆,另一個則沒有。強制重新繪制的方法是由事件監聽器生成的。不是一個定時的動畫線程。我知道動畫線程正在正確運行,只要我連續滑動滑塊,它就會顯示完美。請幫助!是的,我在這里看到過許多類似的問題,我嘗試過驗證、重新驗證和使用paint vs paintComponent。包含以下代碼的四個類:

import javax.swing.*;

public class gutz{

public static int windowWidth = 640;

public static int windowHeight = 480;

public static void main(String[] args){

hBod hb1 = new hBod(50, 30, 21, 111, 7, -11); //mass, radius, xpos, ypos, xvel, yvel

Thread t1 = new Thread(hb1);

windowmakr w = new windowmakr();

w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

w.setSize(windowWidth, windowHeight);

w.setVisible(true);

t1.start();

}

}

import java.awt.*;

import javax.swing.*;

import javax.swing.event.*;

public class windowmakr extends JFrame {

private JSlider slider;

private drawr panel;

public windowmakr(){

super("Orbit Explorer");

panel = new drawr();

panel.setBackground(Color.BLACK);

slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);

slider.setMajorTickSpacing(10);

slider.setPaintTicks(true);

slider.addChangeListener(

new ChangeListener(){

public void stateChanged(ChangeEvent e){

panel.setSpeed(slider.getValue());

}

}

);

add(slider, BorderLayout.SOUTH);

add(panel, BorderLayout.CENTER);

}

}

import java.awt.*;

import javax.swing.*;

public class drawr extends JPanel{

private int diameter = 10;

private static int rad;

private static int xpos;

private static int ypos;

public void paintComponent(Graphics g){

super.paintComponent(g);

g.setColor(Color.ORANGE);

g.fillOval((gutz.windowWidth-diameter)/2, ((gutz.windowHeight-diameter)/2)-40, diameter, diameter);

g.setColor(Color.CYAN);

g.fillOval(xpos, ypos, rad, rad);

}

/* public void paint(Graphics g){

super.paint(g);

g.setColor(Color.ORANGE);

g.fillOval((gutz.windowWidth-diameter)/2, ((gutz.windowHeight-diameter)/2)-40, diameter, diameter);

g.setColor(Color.CYAN);

g.fillOval(xpos, ypos, rad, rad);

}

*/

public void setSpeed(int newD){

diameter = (newD >= 0 ? newD : 10);

repaint();

}

public void renderImage(int r, int xp, int yp){

rad=r;

xpos=xp;

ypos=yp;

repaint();

}

public Dimension getPreferredSize(){

return new Dimension(200,200);

}

public Dimension getMinimumSize(){

return getPreferredSize();

}

}

public class hBod implements Runnable{

private int mass;

private int rad;

private int xpos;

private int ypos;

private double xvel;

private double yvel;

public drawr render;

public hBod(int m, int r, int xp, int yp, double xv, double yv){

mass=m;

rad=r;

xpos=xp;

ypos=yp;

xvel=xv;

yvel=yv;

render = new drawr();

}

public void run(){

try{

while(true){

xpos+=xvel;

ypos+=yvel;

yvel+=1;

System.out.printf("rad - %d, xpos - %d, ypos - %d\n", rad, xpos, ypos);

render.renderImage(rad, xpos, ypos);

Thread.sleep(1000);

}

}catch(Exception e){}

}

}

2 个解决方案

#1

3

Like I said before, the problem lies in this line render = new drawr(); inside the hBod constructor. Create a single instance like private drawr panel = new drawr() inside the gutz class and pass it to other two classes using their constructor, like hBod hb1 = new hBod(50, 30, 21, 111, 7, -11, panel) and windowmakr w = new windowmakr(panel) and simply use this reference to point to panel.renderImage(...) inside hBod class and panel.setSpeed(...) from windowmakr class.

就像我之前說過的,問題在於這一行渲染= new drawr();在hBod構造函數。創建一個實例與私人drawr面板= new drawr()內gutz類並將其傳遞給其他兩個類使用構造函數,就像hBod hb1 = new hBod(111年,50歲,30日,21日,7日,-11年,面板)和windowmakr w = new windowmakr(面板)和簡單地使用這個引用指向panel.renderImage(…)內部hBod類和panel.setSpeed從windowmakr類(…)。

Here is the modified code, Please learn Java Coding Conventions :

這里是修改后的代碼,請學習Java編碼約定:

import java.awt.*;

import javax.swing.*;

import javax.swing.event.*;

public class Gutz {

public static int windowWidth = 640;

public static int windowHeight = 480;

public static void main(String[] args){

Drawr panel = new Drawr();

panel.setBackground(Color.BLACK);

HBod hb1 = new HBod(50, 30, 21, 111, 7, -11, panel); //mass, radius, xpos, ypos, xvel, yvel

Thread t1 = new Thread(hb1);

WindowMakr w = new WindowMakr(panel);

w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

w.setSize(windowWidth, windowHeight);

w.setVisible(true);

t1.start();

}

}

class WindowMakr extends JFrame {

private JSlider slider;

private Drawr panel;

public WindowMakr(Drawr p){

super("Orbit Explorer");

final Drawr panel = p;

slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);

slider.setMajorTickSpacing(10);

slider.setPaintTicks(true);

slider.addChangeListener(

new ChangeListener(){

public void stateChanged(ChangeEvent e){

panel.setSpeed(slider.getValue());

}

}

);

add(slider, BorderLayout.SOUTH);

add(panel, BorderLayout.CENTER);

}

}

class Drawr extends JPanel{

private int diameter = 10;

private static int rad;

private static int xpos;

private static int ypos;

public void paintComponent(Graphics g){

super.paintComponent(g);

g.setColor(Color.ORANGE);

g.fillOval((Gutz.windowWidth-diameter)/2, ((Gutz.windowHeight-diameter)/2)-40, diameter, diameter);

g.setColor(Color.CYAN);

g.fillOval(xpos, ypos, rad, rad);

}

public void setSpeed(int newD){

diameter = (newD >= 0 ? newD : 10);

repaint();

}

public void renderImage(int r, int xp, int yp){

rad=r;

xpos=xp;

ypos=yp;

repaint();

}

public Dimension getPreferredSize(){

return new Dimension(200,200);

}

public Dimension getMinimumSize(){

return getPreferredSize();

}

}

class HBod implements Runnable{

private int mass;

private int rad;

private int xpos;

private int ypos;

private double xvel;

private double yvel;

public Drawr render;

public HBod(int m, int r, int xp, int yp, double xv, double yv, Drawr panel){

mass=m;

rad=r;

xpos=xp;

ypos=yp;

xvel=xv;

yvel=yv;

render = panel;

}

public void run(){

try{

while(true){

xpos+=xvel;

ypos+=yvel;

yvel+=1;

System.out.printf("rad - %d, xpos - %d, ypos - %d\n", rad, xpos, ypos);

render.renderImage(rad, xpos, ypos);

Thread.sleep(1000);

}

}catch(Exception e){}

}

}

#2

2

You can use the getContentPane().repaint() method in your JFrame class

您可以在JFrame類中使用getContentPane().repaint()方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值