日期:2014-05-20 浏览次数:20871 次
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
public class MyPaint extends JFrame {
public MyPaint(String title){
super(title);
}
Container container = getContentPane();
DrawingPanel panel = new DrawingPanel();
public void go(){
setSize(500, 500);
setLocation(200, 200);
panel.setOpaque(false);
panel.setBackground(Color.white);
container.add(panel);
setVisible(true);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e){
System.out.print(e);
}
MyPaint paint = new MyPaint("java painter");
paint.go();
}
}
class DrawingPanel extends JPanel{
static double R = 30;
int oldX, oldY, newX, newY;
Color color = Color.blue;
public DrawingPanel(){
this.setOpaque(false);
setBackground(Color.white);
this.setOpaque(true);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseListener());
}
public void paint(Graphics g){
//g.drawArc(10, 10, 10, 10, 10, 190);
g.setColor(color);
g.drawLine(oldX, oldY, newX, newY);
//specify the max level of map
drawMap(g, 4);
}
/*
* draw a map of Catan
* @param g
*/
private void drawMap(Graphics g, int maxLevel) {
double r = R * Math.pow(3, 0.5) / 2;
double offsetX = R * (maxLevel * 2+ 1) + 5;
double offsetY = r * (maxLevel * 2+ 1) + 5;
drawPoly6(g, 0 + offsetX, 0 + offsetY); //the central one
double xi[] = new double[6];
double yi[] = new double[6];
double tempx, tempy;
for(int level = 1; level <= maxLevel; level++) {
for(int j = 0; j < 6; j++) {