哪位仁兄能重点解释一下红字部分不?
我要一个正的心形,可就是没办法做到,求大侠帮忙!!
package java1;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class HeartJFrame extends JFrame implements ComponentListener,ItemListener
{
private JComboBox combobox_color;
public HeartJFrame()
{
super("心线");
this.setSize(600,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
Object data[] = {Color.red,Color.green,Color.CYAN,Color.blue};
combobox_color = new JComboBox(data);
combobox_color.addItemListener(this);
this.add(combobox_color);
this.addComponentListener(this);
this.setVisible(true);
}
public void paint(Graphics g)
{
int x0,y0;
x0=this.getWidth()/2;
y0=this.getHeight()/2;
g.setColor((Color)combobox_color.getSelectedItem());//设置线的颜色为组合框选中的颜色
g.drawLine(x0, 0, x0, y0*2);
g.drawLine(0, y0, x0*2, y0);
int j=20;
while(j<100)
{
for(int i=0;i<1023;i++)
{
double angle=i*Math.PI/512;//
double radius=j*(1-Math.cos(angle));//求出半径;
int x=(int)Math.round(radius*Math.cos(angle)*2);
int y=(int)Math.round(radius*Math.sin(angle));
g.fillOval(x0+x,y0+y,1,1);
}
j=j+10;
} }
public void itemStateChanged(ItemEvent e)
{
repaint();
}
public void componentResized(ComponentEvent e)
{
repaint();
}
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String args[])
{
new HeartJFrame();
}
}
我要一个正的心形,可就是没办法做到,求大侠帮忙!!
package java1;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class HeartJFrame extends JFrame implements ComponentListener,ItemListener
{
private JComboBox combobox_color;
public HeartJFrame()
{
super("心线");
this.setSize(600,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
Object data[] = {Color.red,Color.green,Color.CYAN,Color.blue};
combobox_color = new JComboBox(data);
combobox_color.addItemListener(this);
this.add(combobox_color);
this.addComponentListener(this);
this.setVisible(true);
}
public void paint(Graphics g)
{
int x0,y0;
x0=this.getWidth()/2;
y0=this.getHeight()/2;
g.setColor((Color)combobox_color.getSelectedItem());//设置线的颜色为组合框选中的颜色
g.drawLine(x0, 0, x0, y0*2);
g.drawLine(0, y0, x0*2, y0);
int j=20;
while(j<100)
{
for(int i=0;i<1023;i++)
{
double angle=i*Math.PI/512;//
double radius=j*(1-Math.cos(angle));//求出半径;
int x=(int)Math.round(radius*Math.cos(angle)*2);
int y=(int)Math.round(radius*Math.sin(angle));
g.fillOval(x0+x,y0+y,1,1);
}
j=j+10;
} }
public void itemStateChanged(ItemEvent e)
{
repaint();
}
public void componentResized(ComponentEvent e)
{
repaint();
}
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String args[])
{
new HeartJFrame();
}
}
r 是半径;angle:角度;
g.fillOval(x0 + x,y0 + y, 1, 1);
改成
g.fillOval(x0 + y, y0 - x, 1, 1);
试一下。