[英]Paint() method not executed, when I call repaint()
I´m having some issues when trying to call the paint method with the instruction repaint()
. 尝试使用指令
repaint()
调用paint方法时遇到一些问题。 At first, when the program executes the code included in the constructor, the paint method is executed. 首先,当程序执行构造函数中包含的代码时,执行paint方法。 However, when I use the instruction
repaint()
, which is executed when I press the enter key, the content of method paint is not executed. 但是,当我使用按下回车键时执行的指令
repaint()
,不会执行方法绘制的内容。 I've seen other questions in SO that seemed similar to mine, but I haven't found the key for solving the problem. 我在SO中看到过与我的相似的其他问题,但我还没有找到解决问题的关键。
The code (The call of repaint is done in the method calcularTrayectoria()
) 代码(重绘的调用在方法
calcularTrayectoria()
)
package elementosJuego;
import elementosJuego.paneles.PanelCanon;
import elementosJuego.paneles.PanelMapaCanon;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.plaf.basic.BasicProgressBarUI;
/**
*
* @author ignacioaranguren
*/
public class Juego extends JFrame {
private Casilla[][] mapa;
private JPanel contenedorTiempo, contenedorVidas,contenedorBarra;
private PanelMapaCanon contenedorMapaCanon;
private PanelCanon contenedorCanon;
private Dimension d1, d2, d3, d4, d5;
private double anguloRotacion = Math.PI/2, anguloMin = 0.656173986, anguloMax = 2.485418668;
private double vX, vY, x1, x2;
private JProgressBar barra;
private ActionListener ac ;
private Timer t;
private int progreso = 0;
private boolean flag = false, flagBala = false;
public Juego(){
setLayout(null);
d1 = new Dimension(646, 699);
contenedorMapaCanon = new PanelMapaCanon(d1);
contenedorMapaCanon.setBounds(0, 0, 646, 699);
barra = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
barra.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
barra.setUI(new BasicProgressBarUI(){
@Override
protected void paintDeterminate(Graphics g, JComponent c) {
Graphics2D g2d = (Graphics2D)g;
int ancho = barra.getWidth();
int alto = barra.getHeight();
int espacioAncho = ancho ;
int espacioAlto = alto ;
barra.setPreferredSize( new Dimension(200,40));
double porcentajeProgres = barra.getPercentComplete();
espacioAncho = (int)(espacioAncho * porcentajeProgres);
if(porcentajeProgres <= 0.25){
g2d.setColor(Color.GREEN);
}else if( porcentajeProgres > 0.25 && porcentajeProgres <= 0.5){
g2d.setColor(Color.yellow);
}else if( porcentajeProgres > 0.5 && porcentajeProgres <= 0.75){
g2d.setColor(Color.orange);
}else{
g2d.setColor(Color.red);
}
Rectangle rec1 = new Rectangle(0,0, espacioAncho, espacioAlto);
g2d.fill(rec1);
}
});
ac = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(flag == false){
progreso = progreso + 1;
if(progreso == 99){
flag = true;
}
}else{
progreso = progreso - 1;
if(progreso == 1){
flag = false;
}
}
barra.setValue(progreso);
}
};
t = new Timer(20, ac);
t.start();
d2 = new Dimension(646, 120);
contenedorCanon = new PanelCanon(d2);
contenedorCanon.setLayout(new FlowLayout(FlowLayout.RIGHT));
d4 = new Dimension(100, 100);
contenedorVidas = new JPanel();
contenedorVidas.setBackground(Color.red);
contenedorVidas.setSize(d4);
d5 = new Dimension(100, 100);
contenedorTiempo = new JPanel();
contenedorTiempo.setBackground(Color.blue);
contenedorTiempo.setSize(d5);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evento) {
switch (evento.getKeyCode()) {
case KeyEvent.VK_LEFT:
if(anguloRotacion > anguloMin && anguloRotacion < anguloMax ){
anguloRotacion = anguloRotacion + 0.05;
contenedorCanon.setAngulo(anguloRotacion);
contenedorCanon.repaint();
}
break;
case KeyEvent.VK_RIGHT:
if(anguloRotacion > anguloMin && anguloRotacion < anguloMax){
anguloRotacion = anguloRotacion - 0.05;
contenedorCanon.setAngulo(anguloRotacion);
contenedorCanon.repaint();
}
break;
case KeyEvent.VK_ENTER:
puntoIncialBala();
flagBala = true;
double i = 0.05;
while(i < 200){
puntoIncialBala();
calcularTrayectoria(i);
i = i + 0.05;
}
break;
}
requestFocus();
}
});
add(contenedorMapaCanon);
add(contenedorCanon);
contenedorCanon.add(barra);
setSize(900,900);
setVisible(true);
}
private void puntoIncialBala(){
if(anguloRotacion < Math.PI/2){
vX = 369 + Math.sin(anguloRotacion);
vY = 788 + Math.cos(anguloRotacion);
}else{
vX = 369 - Math.sin(anguloRotacion);
vY = 788 - Math.cos(anguloRotacion);
}
}
private void calcularTrayectoria(double gamma){
x1 = 335+contenedorCanon.getPuntoMedX()+ gamma * vX;
x2 = 700+contenedorCanon.getPuntoMedY()+ gamma * vY;
revalidate();
this.repaint();
}
@Override
public void paint(Graphics g){
Graphics g2d = (Graphics2D) g;
contenedorCanon.setBounds(30, 700, 646, 120);
contenedorCanon.paintComponent(g);
contenedorMapaCanon.setBounds(0, 0, 676, 699);
contenedorMapaCanon.paintComponent(g);
Image img = new ImageIcon(getClass().getResource("/imagenes/BalaCanon.png")).getImage();
if(flagBala == true)
g2d.drawImage(img, (int)x1, (int)x2, this);
}
private void puntoFinalbala(){
}
}
JFrame
is not a JComponent
, it doesn't have a paintComponent or paint method you can override. JFrame
不是JComponent
,它没有可以覆盖的paintComponent或paint方法。 Instead you could extend a JPanel
and add it to the frame. 相反,您可以扩展
JPanel
并将其添加到框架中。
Your class doesn't extend from any Component capable of painting 您的类不会从任何能够绘画的组件扩展
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.