简体   繁体   English

JFrame背景图片无法正常工作

[英]JFrame Background Image Wont work

I have tried everything to get a background image, and it either just is a gray background, or displays the image with no player, or a player with no image. 我已经尽一切努力来获取背景图像,它要么只是灰色背景,要么显示没有播放器的图像,或者显示没有图像的播放器。 For some reason I can't draw over the background? 由于某种原因,我无法绘制背景? Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class grow extends JPanel implements ActionListener, KeyListener{
double x = 0, y = 0, velx = 0, vely = 0;
Timer t = new Timer(5, this);

public grow(){
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 g2d.setRenderingHint(RenderingHints.KEY_RENDERING,      RenderingHints.VALUE_RENDER_QUALITY);
g2d.setPaint(Color.blue);
g2d.fill(new Ellipse2D.Double(x, y, 70, 70));


}

public void actionPerformed(ActionEvent e) {
repaint();
x += velx;
y += vely;

}

public void up(){
vely = -2;
velx = 0;
}
public void down(){
vely = 2;
velx = 0;
}
public void left(){
vely = 0;
velx = -2;
}

public void right(){
vely = 0;
velx = 2;
} 




 public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_UP){
    up();
}
if(code == KeyEvent.VK_DOWN){
    down();
}

if(code == KeyEvent.VK_RIGHT){
    right();
}

if(code == KeyEvent.VK_LEFT){
    left();
}

}
public void stop(){
velx = 0;
vely = 0;
}
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_UP){
    stop();
}
if(code == KeyEvent.VK_DOWN){
    stop();
}

if(code == KeyEvent.VK_RIGHT){
    stop();
}

if(code == KeyEvent.VK_LEFT){
    stop(); 
    }

}

public void keyTyped(KeyEvent e) {

}

}

and here is my class that displays the jframe: 这是显示jframe的类:

package main;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;

public class playerandframe{

static grow g = new grow();
public static void main(String args[]){
    JFrame j = new JFrame("|The Wizards Of Lleon|");
    j.add(g);
    j.setSize(600,600);
    j.setVisible(true);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
  1. Use key bindings instead of a KeyListener as you may run into focus problems with KeyListener . 使用键绑定而不是KeyListener因为您可能会遇到KeyListener焦点问题。 key bindings are more appropriate for key event with Swing apps. 按键绑定更适合用于Swing应用程序的按键事件。 You can see an example here . 您可以在此处查看示例。
  2. Just draw the background and the image in the same paintComponent method. 只需使用相同的paintComponent方法绘制背景图像。 I currently only see one of those things being drawn. 我目前仅能看到其中之一。
  3. Use proper java naming convention. 使用正确的Java命名约定。 Class names should being with capital letters. 班级名称应使用大写字母。
  4. @Override the getPreferredSize() of the JPanel class ( grow ) instead of setting the size of the frame, and use frame.pack() , instead of frame.setSize() . @Override JPanel类的getPreferredSize()grow )而不是设置框架的大小,并使用frame.pack()而不是frame.setSize() packing the frame will respect the preferred size you gave to the panel 包装框架将遵循您给面板提供的首选尺寸

     @Override public Dimension getPreferredSize() { return new Dimension(600, 600); } .... and frame.pack(); // in the main 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM