
04-04-2009, 05:24 PM
|
 |
Member
|
|
Join Date: Mar 2009
Location: Turkey
Posts: 13
Rep Power: 0
|
|
[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
|
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();
}
}
} |
|
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, 07:55 PM
|
 |
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
|
|
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.
|
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;
}
} |
2 — Mount the sliders in a panel and add it to the south/last section of the BorderLayout.
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),
|
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)); |
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 06:40 PM.
|
|