Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resizing 2D graphics on the JPanel

Status
Not open for further replies.

dkim18

Technical User
Feb 13, 2004
5
US
I have a class called Graph and all drawing functionality and resize/redrawing behavior is encapsulated in this class. So, I have created a graph by using 2D graphics on JPanel (instance of JInternal Frame)and want the graph to be resizable when the user dragging the corners of the window.

Any idea?
-----------------------
public class HW2 extends JApplet {


private JDesktopPane theDesktopPane;
Dimension component_dimen = new Dimension(100, 100);
public void init() {

setBackground(Color.gray);

theDesktopPane = new JDesktopPane();
getContentPane().add(theDesktopPane);

JInternalFrame frame = new JInternalFrame("Home Work2", true, true, true, true);
Container container = frame.getContentPane();
//---------------------
Graph topPanel = new Graph((new Dimension (400, 400)), // size
0, // minX
0, // minY
200.0, // maxX
1.0, // maxY
"Sin and Cos", // title
"X", // x label
"Y", // y label
20.0, // tick X
0.1, // tick Y
// "##0", // format axis X
//"##0.00" ); // format axis Y
new DecimalFormat( "##0" ),
new DecimalFormat( "##0.00") );
container.add(topPanel, BorderLayout.CENTER);
//this.validate();
frame.setMinimumSize(component_dimen);
//frame.resi
frame.pack();
theDesktopPane.add(frame);
frame.setVisible(true);
setSize(400, 400);
setVisible(true);
}
------------------------------
 
//Please look at this example from // CubicPanel.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class CubicPanel extends JPanel {
Grid grid;
CubicFunction function;
AffineTransform transform;
int width = 0, height = 0;
BoundingBox bbox;

public CubicPanel(double llx, double lly, double urx, double ury) {
setBackground(Color.white);
bbox = new BoundingBox(llx, lly, urx, ury);
grid = new Grid(this);
function = new CubicFunction(this);
}

public void paintComponent(Graphics gfx) {
super.paintComponent(gfx);
Rectangle size = getBounds();
Graphics2D g = (Graphics2D) gfx;
if (size.width != width || size.height != height) {
width = size.width; height = size.height;
float ratioX = (float) (width / (bbox.urx - bbox.llx));
float ratioY = (float) (-height /(bbox.ury - bbox.lly));
transform = new AffineTransform();
transform.scale(ratioX, ratioY);
transform.translate(-bbox.llx, -bbox.ury);
}
grid.plot(g);
function.plot(g);
}

public BoundingBox getBoundingBox() {
return bbox;
}

public AffineTransform getTransform() {
return transform;
}

public static void main(String[] args) {
JFrame frame = new JFrame("CubicPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CubicPanel cubic = new CubicPanel(-2, -3, 2, 3);
frame.getContentPane().add(cubic, BorderLayout.CENTER);
cubic.setPreferredSize(new Dimension(300, 300));
frame.pack();
frame.show();
}
}
// Grid.java
import java.awt.*;
import java.awt.geom.*;

public class Grid {
CubicPanel panel;

public Grid(CubicPanel cp) {
panel = cp;
}
public void plot(Graphics2D g) {
BoundingBox bbox = panel.getBoundingBox();
GeneralPath path = new GeneralPath();
int minX = (int) Math.floor(Math.min(bbox.llx, bbox.urx));
int maxX = (int) Math.ceil(Math.max(bbox.llx, bbox.urx));
for (int x = minX; x <= maxX; x++) {
path.moveTo(x, (float) bbox.lly);
path.lineTo(x, (float) bbox.ury);
}
int minY = (int) Math.floor(Math.min(bbox.lly, bbox.ury));
int maxY = (int) Math.ceil(Math.max(bbox.lly, bbox.ury));
for (int y = minY; y <= maxY; y++) {
path.moveTo((float) bbox.llx, y);
path.lineTo((float) bbox.urx, y);
}
AffineTransform transform = panel.getTransform();
g.draw(transform.createTransformedShape(path));
}
}
//CubicFunction.java
import java.awt.*;
import java.awt.geom.*;

public class CubicFunction {
CubicPanel panel;

public CubicFunction(CubicPanel cp) {
panel = cp;
}

public void plot(Graphics2D g) {
BoundingBox bbox = panel.getBoundingBox();
AffineTransform transform = panel.getTransform();
int steps = 50;
double stepsize = (bbox.urx - bbox.llx)/steps;
GeneralPath path = new GeneralPath();
path.moveTo((float) bbox.llx, (float) valueAt(bbox.llx));
for (int i = 1; i <= steps; i++) {
double x = bbox.llx + i*stepsize;
path.lineTo((float) x, (float) valueAt(x));
}
g.setPaint(Color.black);
g.draw(transform.createTransformedShape(path));
}

public double valueAt(double x) {
return x*x*x - x;
}
}
// BoundingBox.java
public class BoundingBox {
public double llx, lly, urx, ury;
public BoundingBox(double lowerLeftX, double lowerLeftY,
double upperRightX, double upperRightY) {
llx = lowerLeftX; lly = lowerLeftY;
urx = upperRightX; ury = upperRightY;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top