Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.event.MouseWheelEvent;
- import java.awt.event.MouseWheelListener;
- import java.awt.geom.AffineTransform;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- public class MyAppFrame extends JFrame {
- private JScrollPane scroll;
- private JPanel nullLayoutPanel;
- private JPanel subPanel;
- private double zoomFactor;
- private static Point pressed;
- public MyAppFrame(){
- setSize(200, 200);
- setLocation(50, 50);
- zoomFactor = 1.0;
- nullLayoutPanel = new JPanel(){
- private void zoom() {
- //the zoom logic should perform in here
- }
- //when you try it with the paint method you'll see my problem
- //move the rect by dragging it around then
- //scroll out or in and try to move the rect
- //you'll see that the position of the logical rect doesn't fit with the painted one
- @Override
- public void paint(Graphics g){
- Graphics2D g2d = (Graphics2D) g;
- AffineTransform old = g2d.getTransform();
- g2d.scale(zoomFactor, zoomFactor);
- super.paint(g);
- //old.translate(zoomFactor, zoomFactor);
- g2d.setTransform(old);
- }
- };
- nullLayoutPanel.setPreferredSize(new Dimension(400,400));
- nullLayoutPanel.setLayout(null);
- nullLayoutPanel.addMouseWheelListener(new MouseWheelListener() {
- @Override
- public void mouseWheelMoved(MouseWheelEvent e) {
- if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL){
- if((.1 * e.getWheelRotation()) > 0 && zoomFactor < 2.0){
- zoomFactor += (.1 * e.getWheelRotation());
- } else if((.1 * e.getWheelRotation()) < 0 && zoomFactor > 0.1){
- zoomFactor += (.1 * e.getWheelRotation());
- }
- zoomFactor = Math.round(zoomFactor*10.0)/10.0;
- nullLayoutPanel.repaint();
- }
- }
- });
- scroll = new JScrollPane(nullLayoutPanel);
- scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
- scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- getContentPane().add(scroll);
- subPanel = new JPanel(){
- @Override
- public void paint(Graphics g){
- g.setColor(Color.BLUE);
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
- }
- };
- subPanel.addMouseListener(new MouseListener() {
- @Override
- public void mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- pressed = e.getPoint();
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- });
- subPanel.addMouseMotionListener(new MouseMotionListener() {
- @Override
- public void mouseMoved(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- Point pPoint;
- pPoint = subPanel.getLocation();
- int x = (int)(pPoint.x - pressed.getX()+e.getX());
- int y = (int)(pPoint.y - pressed.getY()+e.getY());
- if(x>0&&y>0)
- subPanel.setLocation(x,y);
- }
- });
- subPanel.setBounds(10, 10, 10, 10);
- nullLayoutPanel.add(subPanel);
- setVisible(true);
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- new MyAppFrame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement