一个JAVA程序,画一个三叶玫瑰线,再添加按钮来打开颜色选择器,自己写的代码无法成功实现,求大神帮忙。
开发环境为Netbeans,效果和图片差不多,只要将图形和按钮关联就好代码如下。importjava.awt.*;importjava.awt.event.*;impor...
开发环境为Netbeans,效果和图片差不多,只要将图形和按钮关联就好代码如下。import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.awt.Color;import java.awt.Graphics;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.util.ArrayList;import java.util.List;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class GraExp4 extendsJFrame{Container J;JButton btn=new JButton("选择颜色");Color color=new Color(200,200,200);public GraExp4() { J=getContentPane(); J.setLayout(new FlowLayout()); J.add(btn); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ color=JColorChooser.showDialog(null,"请选择你喜欢的颜色",color); if (color==null) color=Color.lightGray;J.setBackground(color);J.repaint(); } });setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);setSize(newDimension(400,300)); show(); BB(); { add(J); setSize(450, 550);//设置窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置一个默认的关闭操作,点击按钮时,退出程序。 setLocationRelativeTo(null); setVisible(true); this.validate(); JButton btn=new JButton("选择颜色"); Color color=new Color(200,200,200); }} public static void main(String[] args) { GraExp4 ge=new GraExp4(); new BB(); } private void BB() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private static class BB { public BB() { } }}class MyJPanel extends JPanel implements ComponentListener{ MyJPanel(){ this.setBackground(Color.BLACK); } int roses=3; @Override public void paint(Graphics g){ super.paint(g); int x0, y0; x0 = this.getWidth() / 2; y0 = this.getHeight() / 2; g.setColor(Color.RED); g.drawLine(x0, 0, x0, y0 * 2); g.drawLine(0, y0, x0 * 2, y0); List<Integer> listx=new ArrayList<Integer>(); List<Integer> listy=new ArrayList<Integer>(); for (int i = 0; i < 1024; i++) { double angle = i * Math.PI / 512; double radius = 200*Math.sin(roses * angle); int x = (int) Math.round(radius * Math.cos(angle)); int y = (int) Math.round(radius * Math.sin(angle)); listx.add(x0+x); listy.add(y0+y); } for (int i = 0; i < listx.size()-1; i++) { g.drawLine(listx.get(i),listy.get(i), listx.get(i+1), listy.get(i+1));//画点 } } @Override public void componentHidden(ComponentEvent arg0) {} @Override public void componentMoved(ComponentEvent arg0) {} @Override public void componentResized(ComponentEvent arg0) { repaint(); } @Override public void componentShown(ComponentEvent arg0) {}}
展开
展开全部
你的三叶玫瑰线程序我帮你改完了,你看看吧。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class GraExp4 extends JFrame{
Container J;
JButton btn=new JButton("选择颜色");
Color color=new Color(200,200,200);
JPanel jp=new JPanel();
MyJPanel mjp=new MyJPanel();
public GraExp4()
{
super("三叶玫瑰线");
J=getContentPane();
jp.setBackground(Color.WHITE);
jp.add(btn);
J.add(jp,BorderLayout.NORTH);
J.add(mjp,BorderLayout.CENTER);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
color=JColorChooser.showDialog(null,"请选择你喜欢的颜色",color);
if (color==null) color=Color.lightGray;
mjp.setForeground(color);
mjp.repaint();
}
});
setSize(450, 550);//设置窗口大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置一个默认的关闭操作,点击按钮时,退出程序。
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args)
{
GraExp4 ge=new GraExp4();
}
class MyJPanel extends JPanel implements ComponentListener{
int roses=3;
MyJPanel(){
this.setBackground(Color.WHITE);
}
@Override
public void paint(Graphics g){
super.paint(g);
int x0, y0;
x0 = this.getWidth() / 2;
y0 = this.getHeight() / 2;
g.setColor(Color.RED);
g.drawLine(x0, 0, x0, y0 * 2);
g.drawLine(0, y0, x0 * 2, y0);
List<Integer> listx=new ArrayList<Integer>();
List<Integer> listy=new ArrayList<Integer>();
g.setColor(color);
for(int j=0;j<200;j+=20)
for (int i = 0; i < 1024; i++) {
double angle = i * Math.PI / 512;
double radius = j*Math.sin(roses * angle);
int x = (int) Math.round(radius * Math.cos(angle));
int y = (int) Math.round(radius * Math.sin(angle));
listx.add(x0+x);
listy.add(y0+y);
}
for (int i = 0; i < listx.size()-1; i++) {
g.drawLine(listx.get(i),listy.get(i), listx.get(i+1), listy.get(i+1));
}
}
@Override
public void componentHidden(ComponentEvent arg0) {}
@Override
public void componentMoved(ComponentEvent arg0) {}
@Override
public void componentResized(ComponentEvent arg0) {
repaint();
}
@Override
public void componentShown(ComponentEvent arg0) {}
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询