我试图旋转一个图形,但由于某种原因,它不能工作。我在其他论坛上做了很多研究,但似乎无法解决这个问题。
所以,这是我的程序的一部分工作。
bufferedimage.createGraohics();Jlabel是在一个显示缓冲图像的窗格中生成的。然后,我有一个在图像上写文本的方法,还有一个保存图像的方法。如果我写的文字,然后保存,这是很好的。这一用途如下:
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();
}
}发布于 2011-12-08 21:20:10
graphics2D.rotate调用转换后续的呈现,因此需要在呈现文本之前重新绘制和放置旋转调用。
另见:爪哇
此外,该方法还要求输入以弧度表示。
发布于 2013-11-19 06:44:18
对于
graphics2D.rotate();方法,尝试
graphics2D.rotate(Math.toRadians(45));https://stackoverflow.com/questions/8437764
复制相似问题