Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Graphics2D  RSS feed

 
Amit Sachdev2
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I was trying to generated curve using Graphics2D object, but I do not get a neat curve, but a broken one.
Can any one suggest me how to draw continous curves.
I put my program here for reference:
============
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class Shape extends JPanel{
int[] xCords = new int[2001];
int[] yCords = new int[2001];
int iCounter=0;
Shape(){
for(int x=-1000,y=0;x<1000;x++,iCounter++){
y = x;
//System.out.println("x="+x+"y="+y+"iCounter="+iCounter);
xCords[iCounter] = (int)x+50;
yCords[iCounter] =(int)y+50;
}
System.out.println("The last counter = "+iCounter);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
//g2D.translate(100,100);
int a = 2;
int b = 5;
int c = 7;

g2D.drawLine(-100,0,100,0);
g2D.drawLine(0,-100,0,100);
//g2D.drawPolyline(xCords,yCords,xCords.length);
g2D.drawPolygon(xCords,yCords,xCords.length);
}
public static void main(String args[]){
JFrame frame = new JFrame("Second Example");
Shape panel = new Shape();
panel.setBackground(new Color(157,102,193));
final JTextField textField = new JTextField(20);
//panel.add(textField);
frame.pack();
frame.getContentPane().add(panel);
frame.setBounds(0,0,600,600);
frame.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
textField.setText(e.getX()+","+e.getY());
}
});
frame.setVisible(true);
}
}
===========
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!