java 的绘图问题

斌仔 发布于 2013/09/02 15:58
阅读 235
收藏 0

package com.randybin;

import java.awt.BasicStroke;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.util.Vector;

import javax.swing.*;

public class BigMapDemo extends JFrame {

 private JPanel panel;
 private JLabel picture;
 Image image1;
 private Image image2;
 private JScrollPane scrollPane;
 private Vector<Image> images = new Vector<Image>();

 public BigMapDemo() throws IOException {
  super("地图导航");
  this.setLayout(null);
  this.setBounds(200, 200, 300, 300);

  image2 = new ImageIcon("d:/locate2.jpg").getImage();
  image1 = new ImageIcon("d:/small2.jpg").getImage();

  scrollPane = new JScrollPane(panel);
  scrollPane.setBounds(0, 0, 1220, 830);

  panel = new JPanel() {
   protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    panel.addMouseListener(new mouseListener());
    drawImageinpanel((Graphics2D) g, panel, 0, 0, 0);
    g.dispose();
   }
  };

  /*JScrollBar bar = scrollPane.getHorizontalScrollBar();
  bar.addAdjustmentListener(new AdjustmentListener() {
   public void adjustmentValueChanged(AdjustmentEvent e) {
    panel.repaint();
   }
  });*/
  //点击滚动条后重绘
  
  scrollPane.setViewportView(panel);
  // panel.setLayout(null);
  panel.setPreferredSize(new Dimension(1500, 1500));
  // panel.repaint();

  this.getContentPane().add(scrollPane);

  this.setResizable(false);
  this.setVisible(true);
  this.setLocation(0, 0);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(1500, 900);
 }

 class mouseListener implements MouseListener {

  @Override
  public void mouseClicked(MouseEvent e) {
   int x = e.getX();
   int y = e.getY();
   /*
    * if (SwingUtilities.isRightMouseButton(e)) {// 右键撤销选择 //
    * System.out.println(images.size()); images.removeAllElements();
    * repaint(); } else { System.out.println(e.getX() + "  " +
    * e.getY()); Graphics2D g = (Graphics2D) panel.getGraphics();
    * //Graphics2D g = (Graphics2D)getGraphics();
    * //这个方法会使滑动滚动条后获得到的坐不准确 images.addElement(image2); int x=e.getX();
    * int y=e.getY(); //paint(g); g.drawImage(image2, x-2, y-10, 20,
    * 30, picture); }
    */

   Graphics2D g = (Graphics2D) getGraphics();
   drawImageinpanel(g, panel, 1, x, y);

  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }

  @Override
  public void mousePressed(MouseEvent e) {
  }

  @Override
  public void mouseReleased(MouseEvent e) {
  }

 }

 private void drawImageinpanel(Graphics2D g, JPanel panel, int i, int x,
   int y) {
  Stroke stroke;
  stroke = g.getStroke();
  g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
    BasicStroke.JOIN_BEVEL, 0, new float[] { 1, 1 }, 0));
  if (i == 0) {
   g.drawImage(image1, 0, 0, 1420, 800, panel);
  } else {
   g.drawImage(image2, x, y, 20, 30, panel);
  }
  g.setStroke(stroke);

 }

 public static void main(String[] args) throws IOException {
  new BigMapDemo();
 }
}

 

如上述代码所示,我想做一个类似地图的标注地点的功能,现在做好了,但是地图比较大,当滑动滚动条的时候之前标注过的地点的图片会消失,请问各位大神有什么好的方法解决呢?谢谢了

加载中
返回顶部
顶部