我正在开发一个项目,我必须显示一个函数的图形并放大图形。完全放大或简单地有一个矩形,在那个矩形中只有选定的部分放大。事实是,我不知道如何做到这一点,我要求你的一些指导,所以。
这是我的代码,如何放大图片?
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
class SimpleJava2DExample extends Frame {
private static final long serialVersionUID = 1L;
double XRmin, XRmax, YRmin, YRmax;
int XEmin, XEmax, YEmin, YEmax;
double Sx, Sy;
int Nsteps;
String textMessage;
double deltaY = 0.3;
// Constructor
SimpleJava2DExample() {
textMessage = "";
XEmin = YEmin = 10;
Nsteps = 100;
XRmin = -Math.PI / 2.;
XRmax = 3 * Math.PI / 2.;// XRmax = 7*Math.PI;
// Calculates YRmin, YRmax
YRmin = F(XRmin);
YRmax = F(XRmax);
double xr, yr;
for (int i = 0; i < Nsteps; i++) {
xr = XRmin + (XRmax - XRmin) * i / Nsteps;
yr = F(xr);
if (yr < YRmin)
YRmin = yr;
if (yr > YRmax)
YRmax = yr;
}
YRmax += deltaY;
// Enables the closing of the window.
WindowListener listener = new WindowAdapter() {
// Override
public void windowClosing(WindowEvent w) {
System.exit(0);
}
};
addWindowListener(listener);
// Mouse click event
MouseListener mouselistener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int ex = (int) e.getPoint().getX();
int ey = (int) e.getPoint().getY();
textMessage = "(x, y) : (" + Xreal(ex) + ", " + Yreal(ey) + ")";
repaint();
}
};
addMouseListener(mouselistener);
// Enables the resize of the window.
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent arg0) {
repaint();
}
});
}
// The function to be plotted
double F(double x) {
return (double) ((Math.sin(x) + 0.5 * Math.sin(3 * x)) * Math.exp(-1.0
* x));
}
// Conversions between user (real) space and device space
// Conversion Xreal --> Xdevice
int Xpix(double xr) {
return XEmin + (int) ((xr - XRmin) * Sx);
}
// Conversion Yreal --> Ydevice
int Ypix(double yr) {
return YEmax - (int) ((yr - YRmin) * Sy);
}
// Conversion Xdevice --> Xreal
double Xreal(double xe) {
return XRmin + (xe - XEmin) / Sx;
}
// Conversion Ydevice --> Yreal
double Yreal(double ye) {
return YRmin + (YEmax - ye) / Sy;
}
// This function plots the axes of the graph
void drawAxes(Graphics2D g2d) {
g2d.setColor(Color.black);
g2d.drawLine(Xpix(XRmin), Ypix(0.0), Xpix(XRmax), Ypix(0.0));
g2d.drawLine(Xpix(0.0), Ypix(YRmin), Xpix(0.0), Ypix(YRmax));
int fontsize = this.getFont().getSize();
g2d.drawString("0", Xpix(0.0) - fontsize, Ypix(0.0) + fontsize);
g2d.drawString("" + XRmax, Xpix(XRmax) - 8 * fontsize, Ypix(0.0)
- fontsize);
g2d.drawString("" + XRmin, Xpix(XRmin), Ypix(0.0) - fontsize);
g2d.drawString("" + YRmax, Xpix(0.0) + fontsize, Ypix(YRmax) + 4
* fontsize);
g2d.drawString("" + YRmin, Xpix(0.0) + fontsize, Ypix(YRmin) - fontsize);
}
public void paint(Graphics g) {
super.paint(g);
// In order to use Java 2D, it is necessary to cast the Graphics object
// into a Graphics2D object.
Graphics2D g2d = (Graphics2D) g;
// The current size of the window
XEmax = this.getWidth() - 50;
YEmax = this.getHeight() - 50;
// Calculates the scale factors
Sx = (XEmax - XEmin) / (XRmax - XRmin);
Sy = (YEmax - YEmin) / (YRmax - YRmin);
// Draw the axes
drawAxes(g2d);
// Draw the graph
g2d.setColor(Color.red);
double xr, yr;
Point2D lastPoint = new Point2D.Double(Xpix(XRmin), Ypix(YRmin));
for (int i = 1; i < Nsteps; i++) {
xr = XRmin + (XRmax - XRmin) * i / Nsteps;
yr = F(xr);
Point2D currentPoint = new Point2D.Double(Xpix(xr), Ypix(yr));
g2d.draw(new Line2D.Double(lastPoint, currentPoint));
lastPoint = currentPoint;
}
g2d.setColor(Color.blue);
int fontsize = this.getFont().getSize();
g2d.drawString(textMessage, 2 * fontsize, this.getHeight() - 2
* fontsize);
textMessage = "";
}
}
public class MainJava2D {
public static void main(String[] args) {
//Generate the window.
SimpleJava2DExample f = new SimpleJava2DExample();
//Define a title for the window.
String s = new String("The first Java 2D graph of a univariate function");
f.setTitle(s);
//Definition of the window size in pixels
f.setSize(500, 400);
//Show the window on the screen.
f.setVisible(true);
}
}
答案 0 :(得分:2)
缩放所有内容的最简单方法是在paint方法开头的scale图形2D对象,但在保存旧的AffineTransform并在结束时重置它之前。
// save the original transform so that it can be restored later
AffineTransform oldTransform = g2d.getTransform();
g2d.scale(zoom, zoom);
...
// use g2d normally
...
// restore the transform because the same Graphics2D object
// might be used to draw other components
g2d.setTransform(oldTransform );
正如Andrew Thompson评论的那样,你应该使用Swing而不是旧的AWT - 如果你这样做,那么你需要覆盖paintComponent(而不是paint),但缩放机制是相同的。