java Graphics2D 怎么调用自绘图形,比如按下按钮,可以将某一个圆变成红色填充
packageone;importjavax.swing.*;importjava.awt.*;publicclassheiheiextendsJFrame{public...
package one;
import javax.swing.*;
import java.awt.*;
public class heihei extends JFrame
{
public static CkPanel ck;
public static Dimension OHeight;
public static int frameheigth=0;
public static int framewidth=0;
public static int preferbanjin=0;
public static heihei hh ;
public heihei()
{
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
ck= new CkPanel();
this.getContentPane().add(ck);
this.setBackground(Color.yellow);
}
public static void main(String[] args)
{
hh = new heihei();
}
public static class CkPanel extends JPanel
{
public CkPanel(){
}
public void paintComponent (Graphics page)
{
//super.paintComponent (page);
Graphics2D d2;
OHeight = hh.getGlassPane().getSize();
frameheigth = (int)OHeight.getWidth()-100;
framewidth = (int)OHeight.getHeight()-100;
if(frameheigth>=framewidth)
preferbanjin = framewidth;
else
preferbanjin = frameheigth;
d2=(Graphics2D)page;//转换d为2D对象
d2.setStroke(new BasicStroke(1));
d2.setColor(Color.red);
d2.drawOval(50,50,preferbanjin,preferbanjin);
//int i =0;
for (int i = 0; i < 36; i++) {
d2.drawOval(preferbanjin/2+30+(int)(preferbanjin/2*Math.cos(3.1415926*10*i/180.0)),
preferbanjin/2+30+(int)(preferbanjin/2*Math.sin(3.1415926*10*i/180.0)),
40,40);
/*d2.fillOval(preferbanjin/2+30+(int)(preferbanjin/2*Math.cos(3.1415926*10*i/180.0)),
preferbanjin/2+30+(int)(preferbanjin/2*Math.sin(3.1415926*10*i/180.0)),
40,40);*/
}
}
}
} 展开
import javax.swing.*;
import java.awt.*;
public class heihei extends JFrame
{
public static CkPanel ck;
public static Dimension OHeight;
public static int frameheigth=0;
public static int framewidth=0;
public static int preferbanjin=0;
public static heihei hh ;
public heihei()
{
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
ck= new CkPanel();
this.getContentPane().add(ck);
this.setBackground(Color.yellow);
}
public static void main(String[] args)
{
hh = new heihei();
}
public static class CkPanel extends JPanel
{
public CkPanel(){
}
public void paintComponent (Graphics page)
{
//super.paintComponent (page);
Graphics2D d2;
OHeight = hh.getGlassPane().getSize();
frameheigth = (int)OHeight.getWidth()-100;
framewidth = (int)OHeight.getHeight()-100;
if(frameheigth>=framewidth)
preferbanjin = framewidth;
else
preferbanjin = frameheigth;
d2=(Graphics2D)page;//转换d为2D对象
d2.setStroke(new BasicStroke(1));
d2.setColor(Color.red);
d2.drawOval(50,50,preferbanjin,preferbanjin);
//int i =0;
for (int i = 0; i < 36; i++) {
d2.drawOval(preferbanjin/2+30+(int)(preferbanjin/2*Math.cos(3.1415926*10*i/180.0)),
preferbanjin/2+30+(int)(preferbanjin/2*Math.sin(3.1415926*10*i/180.0)),
40,40);
/*d2.fillOval(preferbanjin/2+30+(int)(preferbanjin/2*Math.cos(3.1415926*10*i/180.0)),
preferbanjin/2+30+(int)(preferbanjin/2*Math.sin(3.1415926*10*i/180.0)),
40,40);*/
}
}
}
} 展开
1个回答
2015-04-02
展开全部
1、判断鼠标点击的是哪个圆,即鼠标点击时的X、Y
2、根据上面的判断,画的时候,区别开来
2、根据上面的判断,画的时候,区别开来
追问
能帮我示范写一下,怎么去判断XY,小弟非常感谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询