Results 1 to 2 of 2
- 04-04-2009, 04:24 PM #1
[SOLVED] help about drawing graphs
Design and implement a simple GUI application (or applet) that displays the graph of a quadratic function, Ax2 + Bx. Below the plot should be two sliders which allow the user to interactively set the values of A & B, the display being updated continuously in response to any changes the user makes to the sliders.
this is my assignment i wrote some code but the graph doesnt move when i moved sliders can you help me?
Here is my code
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; public class PlotLuckPanel extends JPanel { protected JPanel controls, graph, general; protected JSlider aSlider, bSlider; protected JLabel aLabel, bLabel; int a, b; public PlotLuckPanel() { setLayout(new FlowLayout()); setPreferredSize( new Dimension( 500, 500)); aSlider = new JSlider( JSlider.HORIZONTAL, -100, 100, 0); bSlider = new JSlider( JSlider.HORIZONTAL, -100, 100, 0); aSlider.setMajorTickSpacing(25); aSlider.setMinorTickSpacing(5); aSlider.setPaintTicks(true); aSlider.setPaintLabels(true); aSlider.setAlignmentX( Component.LEFT_ALIGNMENT); bSlider.setMajorTickSpacing(25); bSlider.setMinorTickSpacing(5); bSlider.setPaintTicks(true); bSlider.setPaintLabels(true); bSlider.setAlignmentX( Component.LEFT_ALIGNMENT); PlotLuckListener listener = new PlotLuckListener(); aSlider.addChangeListener( listener); bSlider.addChangeListener( listener); a = aSlider.getValue(); b = bSlider.getValue(); aLabel = new JLabel( "A"); bLabel = new JLabel( "B"); aLabel.setAlignmentX( Component.LEFT_ALIGNMENT); bLabel.setAlignmentX( Component.LEFT_ALIGNMENT); controls = new JPanel(); controls.setLayout( new BoxLayout( controls, BoxLayout.Y_AXIS)); controls.add( aLabel); controls.add( aSlider); controls.add( Box.createRigidArea( new Dimension(0,30))); controls.add( bLabel); controls.add( bSlider); add(controls); graph = new JPanel(); graph.setPreferredSize( new Dimension( 250, 250)); graph.add(new Draw()); general = new JPanel(); general.setLayout( new BorderLayout()); general.add( graph, BorderLayout.NORTH); general.add( controls, BorderLayout.SOUTH); add(general); } private class Draw extends JPanel { private int x1, x2, y1, y2; private int a, b; public Draw() { setPreferredSize( new Dimension (500, 300)) ; } public void paintComponent( Graphics g) { super.paintComponent( g); x1 = 0; x2 = 0; y1 = 0; y2 = 0; while( x1 < 1000) { x2 = x1 + 1; y1 = a * x1 * x1 + b * x1; y2 = a * x2 * x2 + b * x2; g.drawLine( 200 + x1, 200 - y1,200 + x2, 200 - y2 ); g.drawLine( 200 - x1, 200 - y1,200 - x2, 200 - y2 ); x1 = x2; } } } private class PlotLuckListener implements ChangeListener { Draw graph = new Draw(); public void stateChanged( ChangeEvent e) { aSlider.setVisible( true); bSlider.setVisible( true); a = aSlider.getValue(); b = bSlider.getValue(); aLabel.setText( "A: " + a); bLabel.setText( "B: " + b); repaint(); } } }Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class PlotLuck { public static void main(String[] args) { JFrame frame = new JFrame("Bandit"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.getContentPane().add( new PlotLuckPanel()); frame.pack(); frame.setVisible(true); } }
- 04-04-2009, 06:55 PM #2
Suggestions:
1 — FlowLayout is not a good choice for your Draw component. Inspect the bounds (r) of Draw after it is rendered (code below). Better to use the center section of a BorderLayout.
2 — Mount the sliders in a panel and add it to the south/last section of the BorderLayout.Java Code:public void paintComponent( Graphics g) { super.paintComponent( g); Graphics2D g2 = (Graphics2D)g; Rectangle r = getBounds(); System.out.printf("width = %d height = %d " + "bounds = [%d, %d, %d, %d]%n", getWidth(), getHeight(), r.x, r.y, r.width, r.height); g2.setPaint(Color.magenta); g2.draw(r); g2.setPaint(Color.black); x1 = 0; x2 = 0; y1 = 0; y2 = 0; while( x1 < 1000) { x2 = x1 + 1; y1 = a * x1 * x1 + b * x1; y2 = a * x2 * x2 + b * x2; if(x1 == 200) { System.out.printf("x1 = %d y1 = %d%n", x1, y1); g2.setPaint(Color.red); g2.fill(new Ellipse2D.Double(x1-1.5, y1-1.5, 4, 4)); g2.setPaint(Color.green.darker()); g2.fill(new Ellipse2D.Double(200 + x1, 200 - y1, 4, 4)); g2.setPaint(Color.blue); g2.drawLine(200, 200, x1, y1); g2.setPaint(Color.black); } g.drawLine( 200 + x1, 200 - y1,200 + x2, 200 - y2 ); g.drawLine( 200 - x1, 200 - y1,200 - x2, 200 - y2 ); x1 = x2; } }
3 — Code above gives some ideas about how to explore/investigate what is going on in your graphic component.
4 — For dynamic graphics, viz, fluid resizing behavior, consider using the width and height of the component for drawing, eg (pseudo code),
Java Code:protected void paintComponent( Graphics g) { super.paintComponent( g); Graphics2D g2 = (Graphics2D)g; int w = getWidth(); int h = getHeight(); // center of this component: int cx = w/2; int cy = h/2; g2.fill(new Ellipse2D.Double(cx-1.5, cy-1.5, 4, 4));
Similar Threads
-
Help me with graphics
By 7oclock in forum New To JavaReplies: 12Last Post: 04-04-2009, 11:20 PM -
Help with 2d graphics please
By xbox_nutter in forum New To JavaReplies: 0Last Post: 04-02-2009, 11:48 AM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM -
graphics
By Joe2003 in forum Advanced JavaReplies: 4Last Post: 01-18-2008, 07:44 PM -
Graphics
By feniger in forum New To JavaReplies: 1Last Post: 12-29-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks