Forums Register Login

Graphics2D

+Pie Number of slices to send: Send
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);
}
}
===========
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1082 times.
Similar Threads
JTextFields & storing data
exception java.lang.InterruptedException is never thrown in body of corresponding try statement
painting custom components without extending jcomponent
Wanted: example mouse coordinates->world coordinates
Adding graphics to JPanel using PopupMenu
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:36:59.