ROAD_DOGG33J
Premium
- 14,302
- IL, USA
- holyc0w1
- holyc0w
The program has to draw 15 lines of different color, length and thickness using Line2D.Double.
However, when I run the program no lines are drawn, so I need help with that.
Here is the LinePanel file:
Here is the Lines file:
However, when I run the program no lines are drawn, so I need help with that.
Here is the LinePanel file:
Code:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
class LinePanel extends JPanel
{
int numberOfLines;
int maxLength;
Random generator;
public LinePanel(int lines, int maxL)
{
super();
numberOfLines = lines;
maxLength = maxL;
}
public void draw(Graphics g)
{
System.out.println("running draw");
generator = new Random();
Line2D.Double [] lines = new Line2D.Double[15];
Graphics2D g2 = (Graphics2D)g;
for(int x = 0; x< numberOfLines; x++)
{
lines[x] = new Line2D.Double(x + 20, x + 10, x+20, generator.nextInt(maxLength));
}
for(int x = 0 ; x< numberOfLines; x++)
{
g2.setColor(new Color(generator.nextInt(256),generator.nextInt(256),generator.nextInt(256)));
g2.setStroke(new BasicStroke(generator.nextInt(5)));
g2.draw(lines[x]);
}
g2.draw(new Line2D.Double(10,35,65,56));
}
}
Here is the Lines file:
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
public class Lines extends JFrame
{
// Variables declaration
private JButton cmdDraw;
private JPanel contentPane;
LinePanel drawingPanel;
// End of variables declaration
public Lines()
{
super("Title");
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Windows Form Designer. Otherwise, retrieving design might not work properly.
* Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
* to retrieve your design properly in future, before revising this method.
*/
private void initializeComponent()
{
drawingPanel = new LinePanel(15, 200);
this.getContentPane().add(drawingPanel,BorderLayout.CENTER);
cmdDraw = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// cmdDraw
//
cmdDraw.setText("Draw Lines");
cmdDraw.setMaximumSize(new Dimension(90, 25));
cmdDraw.setMinimumSize(new Dimension(90, 25));
cmdDraw.setPreferredSize(new Dimension(90, 25));
cmdDraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
cmdDraw_actionPerformed(e);
}
});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, cmdDraw, 2,300,390,25);
//
// Lines
//
this.setTitle("Lines");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(400, 350));
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void cmdDraw_actionPerformed(ActionEvent e)
{
drawingPanel.draw(drawingPanel.getGraphics());
System.out.println("\ncmdDraw_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
//============================= Testing ================================//
//= =//
//= The following main method is just for testing this class you built.=//
//= After testing,you may simply delete it. =//
//======================================================================//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new Lines();
}
//= End of Testing =
}