2011-12-08 147 views
0

我想旋轉圖形,但由於某種原因它不起作用。我在其他論壇上做了大量的研究,但似乎無法解決這個問題。旋轉圖形2D

所以,這是我的程序作品。

  1. 收集文件。
  2. 創建緩衝的圖像
  3. bufferedimage.createGraohics();

Jlabel是在窗格中,其示出了緩衝的圖像作出的Graphics2D創建。 然後我有一個方法來在圖像上寫文本,並有一種方法來保存圖像。 如果我寫文本,然後保存,工作正常。 這使用:

graphic2D.drawString("Hello, this is my test.",10,10); 

我也看到了這個更新的JLabel,這樣我就可以看到圖像上的文字。

但是,如果我創建了一個法:

graphics2D.rotate(45); 

我看絕對沒有什麼變化。

有沒有人有任何想法爲什麼?

這是我的方法:

public void actionPerformed(ActionEvent e){  
    graphic2D.rotate(4.5); 
    saveImage(); 
} 

感謝,

import java.io.File; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.imageio.*; 
import java.awt.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 

class EditorView implements ActionListener, ChangeListener{ 

    JFrame editorFrame; 
    JPanel container = new JPanel(); 
    JPanel toolbox = new JPanel(); 
    JPanel editorPanel = new JPanel(); 
    JScrollPane editorScrollPane; 
    File imageFile; 
    BufferedImage bi; 
    ImageIcon ii; 
    Graphics2D graphic2D; 
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 10, 5); 

    public EditorView(File file){ 
     this.imageFile = file; 

     try{ 
     bi = ImageIO.read(imageFile); 
     }catch(Exception e){} 

     graphic2D = bi.createGraphics(); 

     createAndShowGUI(); 

    } 


    void createAndShowGUI(){ 

     editorFrame = new JFrame("Editor"); 
     editorFrame.setSize(1000,650); 
     container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS)); 
     editorFrame.add(container); 
     toolbox.setMaximumSize(new Dimension(Integer.MAX_VALUE,50)); 
     toolbox.setPreferredSize(new Dimension(Integer.MAX_VALUE,50)); 
     toolbox.setLayout(new GridLayout(2,10)); 
     JButton rotateLeftBtn = new JButton("Rotate Left"); 
     toolbox.add(rotateLeftBtn); 
     rotateLeftBtn.addActionListener(this); 
     toolbox.add(new JButton("Save")); 
     toolbox.add(new JButton("Close")); 
     toolbox.add(new JButton("Freeform")); 
     toolbox.add(new JButton("Colour")); 
     toolbox.add(new JButton("Text")); 
     toolbox.add(new JButton("Square")); 
     toolbox.add(new JButton("Circle")); 
     toolbox.add(new JButton("Elipse")); 
     toolbox.add(new JButton("Rotate Right")); 


     slider.addChangeListener(this); 
     toolbox.add(slider); 


     container.add(toolbox); 

     editorScrollPane = new JScrollPane(new JLabel(new ImageIcon(bi))); 

     container.add(editorScrollPane); 
     editorFrame.setVisible(true); 
     editorFrame.validate(); 
     container.validate(); 
     editorPanel.validate(); 

     drawLabel(); 
     //editorScrollPane.repaint(); 
    } 

    void drawLabel(){ 
     graphic2D.rotate(1); 
     graphic2D.drawString("Hello, this is my test.",10,10); 

    } 

    void drawCircle(){ 

    } 

    void saveImage(){ 

     try{ 
     ImageIO.write(bi,getFileExtension(), imageFile); 
     }catch(Exception e){} 
    } 

    String getFileExtension(){ 
     int positionOfDot = imageFile.getName().lastIndexOf("."); 
     String returner = null; 
     if(positionOfDot !=-1){ 
      returner = imageFile.getName().substring((positionOfDot+1), imageFile.getName().length()); 
     } 
     System.out.println(returner); 
     return returner; 
    } 

    public void actionPerformed(ActionEvent e){ 

     graphic2D.rotate(1); 
     saveImage(); 

    } 

    public void stateChanged(ChangeEvent c){ 
     graphic2D.scale(slider.getValue()/5, slider.getValue()/5); 
     editorScrollPane.repaint(); 

    } 

} 
+0

你能請張貼整個上下文(即,無論是'rotate'呼叫和'drawString')? –

回答

4

graphics2D.rotate調用轉換以後的渲染,所以你需要重畫,並把您呈現文本之前的旋轉通話。

參見:Javadocs

而且,該方法要求輸入要以弧度表示。

+0

感謝您的幫助。我仍然不知道我應該如何做到這一點。這是我對應用程序中其中一個代碼所在的框架的看法。請你能讓我知道我要去哪裏錯了嗎?非常感謝。 (添加代碼來提問)。這是我想要旋轉的文件內的全部圖像,包括添加到其中的任何內容(如標籤)。 – ThePerson

+0

現在,旋轉位於actionPerformed中,用於任何按鈕。那只是因爲它還沒完全完成。 – ThePerson

+0

好的,在繪製正方形和旋轉之後,我發現我可以旋轉,但實際加載的圖像不會隨着Graphic2D旋轉。我在想,因爲我稱之爲createGraphic,圖像將在graphic2d上? – ThePerson

1

對於

graphics2D.rotate(); 

方法,嘗試

graphics2D.rotate(Math.toRadians(45));