空布局上的Java图形?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (95)

我为这个面板的最终游戏是,我有一个IMG图标能够在屏幕上移动,当他们降落在我的一个当前的按钮,新的面板打开,你会得到一个小游戏,即真假,迷宫,或文字查找。

我做了一个基本的空布局,并把按钮作为位置持有人,在那里的球员图标将去打开下一个面板。

我正在努力在屏幕上放置一个简单的矩形,这个矩形可以使用箭头键盘监听器移动。我在网上观看了关于创建这个网站的教程,并搜索了这个数据库。

我的当前代码仍然显示我的空布局与我的地图IMG背景和按钮与IMG图标在这些按钮。它不会显示我的长方形。

是的,我是一个学生,这是一个来自学校的项目,我希望你能给我正确的方向指导我在这里做的三件主要事情。在屏幕上设置矩形并移动它。获取矩形上的图像图标。我应该从哪里开始观察它的制作,所以当移动的物体碰到一个特定的点时,JLable,Jtton,Jany-我想不出怎么拿出我已经做好的新面板。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class map extends JPanel implements ActionListener, KeyListener{

    Timer t = new Timer(5,this);
    int x = 0, y = 0, velX = 0, velY = 0;
    JButton mapButton, worldCampusB, universityParkB, fayetteB, erieB, yorkB, 
            hazeltonB;  
    JLabel background;
    ImageIcon img;


    public map(){


        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);


        setBackground(new Color(9, 49, 98));  
        setLayout(new BorderLayout());
        ImageIcon oldmain = new ImageIcon("images/oldmain.jpg");
        ImageIcon hazelton = new ImageIcon("images/hazelton.jpeg");
        ImageIcon york = new ImageIcon("images/york.jpg");
        ImageIcon erie = new ImageIcon("images/erie.jpg");
        ImageIcon fayette = new ImageIcon("images/fayette.jpg");
        ImageIcon worldcampus = new ImageIcon("images/worldcampus.png");
        background = new JLabel(new ImageIcon("images/pennmap.jpg"));
        add (background);
        background.setLayout(null);       
        mapButton = new JButton("Map Menu: Click to return to main menu.");
        mapButton.setBounds(new Rectangle(300,20,300,50));
        worldCampusB = new JButton("World Campus");
        worldCampusB.setIcon(worldcampus);
        universityParkB = new JButton("University Park");
        universityParkB.setIcon(oldmain);
        fayetteB = new JButton("Fayette");
        fayetteB.setIcon(fayette);
        erieB = new JButton ("Erie");
        erieB.setIcon(erie);
        yorkB = new JButton ("York");
        yorkB.setIcon(york);
        hazeltonB = new JButton ("Hazelton");
        hazeltonB.setIcon(hazelton);
        background.add(mapButton);
        background.add(worldCampusB);
        background.add(universityParkB);
        background.add(fayetteB);
        background.add(erieB);
        background.add(yorkB);
        background.add(hazeltonB);
        //adjusted the button locations on the map - jpk5816
        worldCampusB.setBounds(new Rectangle (750,20,195,150));
        worldCampusB.setHorizontalTextPosition(JButton.CENTER);
        worldCampusB.setVerticalTextPosition(JButton.BOTTOM);
        universityParkB.setBounds(new Rectangle(380,250,175,140));
        universityParkB.setHorizontalTextPosition(JButton.CENTER);
        universityParkB.setVerticalTextPosition(JButton.BOTTOM);
        fayetteB.setBounds(new Rectangle(40,445,200,150));
        fayetteB.setHorizontalTextPosition(JButton.CENTER);
        fayetteB.setVerticalTextPosition(JButton.BOTTOM);
        erieB.setBounds(new Rectangle(50,100,175,170));
        erieB.setHorizontalTextPosition(JButton.CENTER);
        erieB.setVerticalTextPosition(JButton.BOTTOM);
        yorkB.setBounds(new Rectangle(625,460,185,130));
        yorkB.setHorizontalTextPosition(JButton.CENTER);
        yorkB.setVerticalTextPosition(JButton.BOTTOM);
        hazeltonB.setBounds(new Rectangle(690,190,170,140));
        hazeltonB.setHorizontalTextPosition(JButton.CENTER);
        hazeltonB.setVerticalTextPosition(JButton.BOTTOM);
    }
        public void paintCompent(Graphics g){
            super.paintComponent(g);
            g.setColor(new Color(9, 49, 98));
            g.fillRect(x, y, 50, 30);
        }
        public void actionPerformed(ActionEvent e){                        
            repaint();
            x += velX;
            y += velY;            
        }
        public void up(){
            velY = -1;
            velX = 0;
        }
        public void down(){
            velY = 1;
            velX = 0;
        }
        public void left(){
            velX = -1;
            velY = 0;
        }
        public void right(){
            velX = 1;
            velY = 0;
        }
        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_LEFT){
                left();
            }
            if (code == KeyEvent.VK_RIGHT){
                right();
            }

        }
        public void keyTyped(KeyEvent e){}
        public void keyReleased(KeyEvent e){}

}
提问于
用户回答回答于
in myJPanel
   public class myJPanel extends JPanel implements ActionListener {

   ImageIcon img; //declare 

public myJPanel(){
    super();
    setBackground(Color.white);
    setLayout (new BorderLayout());
    credits = new credits();
    instructions = new instructions();
    characterTheme = new characterTheme();
    img = new ImageIcon("images/pennmap.jpg");//grab from images foler.

在map.java中,这是我想要绘制IMG的地方。

public class map extends JPanel {

    ImageIcon img; 

public map (ImageIcon img){
    this.img = img;
    Dimension size = new Dimension(getWidth(),getHeight());
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);  
}
public void paintComponent(Graphics g){
    g.drawImage(img.getImage(), 0, 0, null);
} 
用户回答回答于

一个更好的解决方案是去掉背景JLabel,简单地在map的ploytComponent方法中绘制该图像,然后再绘制矩形。

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(backgroundImg, 0, 0, this); // draw image
    g.setColor(new Color(9, 49, 98));
    g.fillRect(x, y, 50, 30);
}

// rename this to Map so that it complies with Java standards
public class Map extends JPanel implements ActionListener, KeyListener {

    private static String IMAGE_PATH = "images/pennmap.jpg";

    // ..... other code here

    // JLabel background;  // **** get rid of this ****
    // ImageIcon img;

    private BufferedImage backgroundImg;


    // constructor needs to be re-named
    public Map(){

        backgroundImg = ImageIO.read(new File(IMAGE_PATH)); // read in image. Better to use resources though


        // .... code here



        // background = new JLabel(new ImageIcon("images/pennmap.jpg")); // again get rid of
        // add (background); // get rid of

        // .... code here
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(backgroundImg, 0, 0, this); // draw image
        g.setColor(new Color(9, 49, 98));
        g.fillRect(x, y, 50, 30);
    }

    // ..... 

}   

扫码关注云+社区

领取腾讯云代金券