2014-09-28 102 views
-2

我在JPanel上顯示圖像,並在該圖像上創建一個8 * 8的網格setStrokeGraphics2D。現在,當我通過設置setStroke來縮放網格線時,它不適用於所有圖像。我認爲這個問題與圖像的分辨率有關。我怎樣才能解決這個問題放大和縮小網格圖像和揮杆圖像

BufferedImage bi; //loading buffered image some how 

JCombobox jComboBox; //creating combobox having different zoom factor 

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 

int gridLinesThikness = (int) ((float) jComboBox.getSelectedIndex() * (float) (bi.getWidth()/(float) screen.width)); 

if (gridLinesThikness < 1) 
    gridLinesThikness = 1; 
g2.setStroke(new BasicStroke(gridLinesThikness)); 
+0

請發表您的代碼的一個完整的例子。 – 2014-09-28 15:25:42

+0

感謝Sergiy的快速回復。但我無法在此發佈完整的代碼,因爲它包含大量的類依賴關係。如果您需要,我可以提供其他信息 – 2014-09-28 15:43:51

+0

而不是發佈大量無人關心的代碼,請按照幫助中心中的說明創建SSCCE或MVCE。這會增加你獲得良好答案的機會,相信我。 :-) – haraldK 2014-09-29 09:01:46

回答

1

試試這個代碼:

package sad; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagLayout; 
import java.awt.Point; 
import java.awt.event.MouseWheelEvent; 
import java.awt.event.MouseWheelListener; 
import java.awt.geom.Rectangle2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

public class zoom extends javax.swing.JPanel { 

private static int prevN = 0; 
private Dimension preferredSize = new Dimension(400,400);  
private Rectangle2D[] rects = new Rectangle2D[50]; 

public static void main(String[] args) {   
    JFrame jf = new JFrame("test"); 
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    jf.setSize(800, 800); 

    JPanel containerPanel = new JPanel();  // extra JPanel 
    containerPanel.setLayout(new GridBagLayout()); 
    containerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(1, 0, 1))); 

    zoom zoomPanel = new zoom(); 
    containerPanel.add(zoomPanel); 
    zoomPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); 

    jf.add(new JScrollPane(containerPanel)); 
    jf.setVisible(true); 
}  

public zoom() { 
    // generate rectangles with pseudo-random coords 
    for (int i=0; i<rects.length; i++) { 
     rects[i] = new Rectangle2D.Double(
       Math.random()*.8, Math.random()*.8, 
       Math.random()*.2, Math.random()*.2); 
    } 

    addMouseWheelListener(new MouseWheelListener() { 
     @Override 
     public void mouseWheelMoved(MouseWheelEvent e) { 
      updatePreferredSize(e.getWheelRotation(), e.getPoint()); 
     } 
    }); 
} 

private void updatePreferredSize(int n, Point p) { 

    if(n == 0)    // ideally getWheelRotation() should never return 0. 
     n = -1 * prevN;  // but sometimes it returns 0 during changing of zoom 
          // direction. so if we get 0 just reverse the direction. 

    double d = (double) n * 1.08; 
    d = (n > 0) ? 1/d : -d; 

    int w = (int) (getWidth() * d); 
    int h = (int) (getHeight() * d); 
    preferredSize.setSize(w, h); 

    int offX = (int)(p.x * d) - p.x; 
    int offY = (int)(p.y * d) - p.y; 
    getParent().setLocation(getParent().getLocation().x-offX,getParent().getLocation().y-offY); 
    //in the original code, zoomPanel is being shifted. here we are shifting containerPanel 

    getParent().doLayout();    // do the layout for containerPanel 
    getParent().getParent().doLayout(); // do the layout for jf (JFrame) 

    prevN = n; 
} 

@Override 
public Dimension getPreferredSize() { 
    return preferredSize; 
} 

private Rectangle2D r = new Rectangle2D.Float(); 
public void paint(Graphics g) { 
    super.paint(g); 
    g.setColor(Color.red); 
    int w = getWidth(); 
    int h = getHeight(); 
    for (Rectangle2D rect : rects) { 
     r.setRect(rect.getX() * w, rect.getY() * h, 
       rect.getWidth() * w, rect.getHeight() * h); 
     ((Graphics2D)g).draw(r); 
    }  
    } 
}