无法在我的照片上画出三角形

时间:2015-04-23 14:58:18

标签: java swing jpanel paintcomponent

我正在创建一个地图程序,应该可以添加地铁,学校等地方..

我可以打开一张地图图片,我的窗口应调整大小以适应图像。现在我不能在地图上添加类似的针脚(我的三角形)。事情是我无法让它工作..我有一个MouseAdapter应该收集我的X和Y并将我的三角形放在地图上。它不会工作,我无法弄清楚为什么......当我创建MapPanel时,我甚至已经硬编码了一个三角形,该三角形显示但它总是最终位于地图的顶部中心..即使我在创建它时更改x和y的值..

这是我的主程序

package inlupp2_prog2;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;



public class MapProgram extends JFrame {

JComboBox place;
MapPanel mp = null;
JTextField searchField;
JButton searchButton, hideButton, deletePlaceButton, whatIsHere, hideCat, newCat, delCat;
JFileChooser jfc = new JFileChooser(".");
boolean changed = false;




MapProgram(){
    super("inlupp2 av Filip Blom (fibl7163)");

    //FILEMENU  TOPP
    JMenuBar fileBar = new JMenuBar();
    setJMenuBar(fileBar);

    JMenu archive = new JMenu("File");
    fileBar.add(archive);

    JMenuItem open = new JMenuItem("Open");
    archive.add(open);
    open.addActionListener(new OpenLis());

    JMenuItem save = new JMenuItem("Save");
    archive.add(save);
    //ACTIONLISTENER

    JMenuItem close = new JMenuItem("Close");
    archive.add(close);
    close.addActionListener(new CloseLis());


    //valmeny   TOPP
    JPanel north = new JPanel();
    add(north, BorderLayout.NORTH);
    north.add(new JLabel("New"));

    String[] places = { "NamedPlace", "DescribedPlace" };
    place = new JComboBox(places);
    north.add(place);
    //ACTIONLISTENER

    searchField = new JTextField("search", 10);
    north.add(searchField);
    //ACTIONLISTERNER

    searchButton = new JButton("Search");
    north.add(searchButton);
    //ACTIONLISTENER

    hideButton = new JButton("Hide places");
    north.add(hideButton);
    //ACTIONLISTER

    deletePlaceButton = new JButton("Delete places");
    north.add(deletePlaceButton);
    //ACTIONLISTERN

    whatIsHere = new JButton("What is here?");
    north.add(whatIsHere);
    //ACTIONLISTER


    //kategorier ÖST

    JPanel east = new JPanel();
    add(east, BorderLayout.EAST);

    east.add(new JLabel("Categories"));
    //SCROLLPANE

    hideCat = new JButton("Hide category");
    east.add(hideCat);
    //ACTIONLISTER

    newCat = new JButton("New category");
    east.add(newCat);
    //ACTIONLISTERN

    delCat = new JButton("Delete category");
    east.add(delCat);
    //ACTIONLISTER

    BoxLayout eastLayout = new BoxLayout(east, BoxLayout.Y_AXIS);
    east.setLayout(eastLayout);



    addWindowListener(new CloseLis());


    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
    setSize(800,300);
    setVisible(true);
    setLocationRelativeTo(null);
    setResizable(false);
}

class MouseLis extends MouseAdapter{
    public void MouseClicked(MouseEvent mev){
        int x = mev.getX();
        int y = mev.getY();
        Place p = new Place(x,y);
        mp.add(p);
        mp.repaint();
        mp.validate();
    }
}

class OpenLis implements ActionListener{
    public void actionPerformed(ActionEvent ave){
        int answer = jfc.showOpenDialog(MapProgram.this);
        if(answer != JFileChooser.APPROVE_OPTION){
            return;
        }

        File file = jfc.getSelectedFile();
        String filename = file.getAbsolutePath();
        if(mp != null){
            remove(mp);
        }

        mp = new MapPanel(filename);
        add(mp, BorderLayout.CENTER);
//          mp.add(new Place(500,500));
        pack();
        validate();
        repaint();
        mp.addMouseListener(new MouseLis());
    }
}

class CloseLis extends WindowAdapter implements ActionListener{

    public void closeSave(){
        if(changed){
        int answer = JOptionPane.showConfirmDialog(null,"You have unsaved changes...\nYou Still want to close this program?", "WARNING", JOptionPane.YES_NO_OPTION);
//          int answer = JOptionPane.showConfirmDialog(MapProgram.this, "You have unsaved changes...\nYou Still want to close this program?");
        if(answer==0){
            System.exit(0);
        } 
        }

        System.exit(0);
    }

    public void windowClosing(WindowEvent wev){
        closeSave();
    }


    public void actionPerformed(ActionEvent ave){
        closeSave();
    }

}

public static void main(String[] args){
    new MapProgram();
}

}

这是我创建三角形

的类
package inlupp2_prog2;

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

public class Place extends JComponent {

public Place(int x, int y){
    setBounds(x,y,50,50);
    Dimension d = new Dimension(50, 50);
    setPreferredSize(d);
    setMaximumSize(d);
    setMinimumSize(d);
}

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    int[] xes = {0,25,50};
    int[] yes = {0,50,0};
    g.fillPolygon(xes, yes, 3);
}

}

这是我的MapPanel

package inlupp2_prog2;

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

public class MapPanel extends JPanel{
private ImageIcon map;

public MapPanel(String filename){
    map = new ImageIcon(filename);
    int w = map.getIconWidth();
    int h = map.getIconHeight();
    Dimension d = new Dimension(w,h);
    setPreferredSize(d);
    setMaximumSize(d);
    setMinimumSize(d);
}

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(map.getImage(), 0, 0, getWidth(), getHeight(), this);
}


}

1 个答案:

答案 0 :(得分:2)

class MouseLis extends MouseAdapter{
    public void MouseClicked(MouseEvent mev){
        int x = mev.getX();
        int y = mev.getY();
        Place p = new Place(x,y);
        mp.add(p);
        mp.repaint();
        mp.validate();
    }
}

请注意mouseClicked != MouseClicked。课程:始终在覆盖方法时使用@Override,因为这会向您显示错误:

class MouseLis extends MouseAdapter{
    @Override
    public void MouseClicked(MouseEvent mev){
        int x = mev.getX();
        int y = mev.getY();
        Place p = new Place(x,y);
        mp.add(p);
        mp.repaint();
        mp.validate();
    }
}

注意:我是如何找到您的错误的?通过玩你的代码并在其中喷洒println,包括:

  public void MouseClicked(MouseEvent mev) {
     System.out.println("" + mev.getPoint());
     int x = mev.getX();
     int y = mev.getY();
     Place p = new Place(x, y); //!!
     p.setBorder(BorderFactory.createLineBorder(Color.red)); //!!
     mp.add(p);
     mp.repaint();
     mp.validate();
  }

这个家伙从来没有打印过,所以它让我知道为什么它不起作用。此外,将来请考虑创建并发布Minimal, Complete, and Verifiable Example Program。我们不希望看到您的整个程序,而是您应该将代码压缩到仍然编译的最小位,没有与您的问题无关的额外代码,但仍然可以证明您的问题。您可以通过简单地尝试隔离并暴露错误来自己解决问题。