
布局问题,默认是加在中间会全部铺满的
class LayoutTest {
LayoutTest() {
init();
}
void init() {
JFrame frame = new JFrame();
JPanel epanel = new JPanel();
JPanel spanel = new JPanel();
JPanel wpanel = new JPanel();
JPanel npanel = new JPanel();
Container contentPane = framegetContentPane();
contentPanesetBackground(ColorBLUE);
Canvas canvas = new Canvas();
canvassetBackground(ColorWHITE);
contentPaneadd(canvas,"Center");
contentPaneadd(epanel,"East");
contentPaneadd(spanel,"South");
contentPaneadd(wpanel,"West");
contentPaneadd(npanel,"North");
framesetSize(300,200);
framesetVisible(true);
}
public static void main(String[] args) {
new LayoutTest();
}
}
你看这个就知道了。
public enum ShapeTypes {
LINE, CIRCLE, RECTANGLE
}public interface Shape {
void paint(Graphics g);
}public class Rectangle implements Shape {
// 矩形左上角的坐标
private int x, y;
// 矩形的宽度和高度
private int width, height;
private Color rectangleColor;
public Rectangle() {
super();
}
public Rectangle(int x, int y, int width, int height, Color rectangleColor) {
super();
thisx = x;
thisy = y;
thiswidth = width;
thisheight = height;
thisrectangleColor = rectangleColor;
}
@Override
public void paint(Graphics g) {
gsetColor(rectangleColor);
gdrawRect(x, y, width, height);
}
}public class Line implements Shape {
// 直线的起始位置
private int x1, y1;
// 直线的终止位置
private int x2, y2;
private Color lineColor;
public Line(int x1, int y1, int x2, int y2, Color lineColor) {
super();
thisx1 = x1;
thisy1 = y1;
thisx2 = x2;
thisy2 = y2;
thislineColor = lineColor;
}
public Line() {
super();
}
@Override
public void paint(Graphics g) {
gsetColor(lineColor);
gdrawLine(x1, y1, x2, y2);
}
}public class Circle implements Shape {
// 圆的颜色
private Color circleColor;
// 圆心的坐标
private int x, y;
// 圆的半径
private int radius;
public Circle() {
super();
}
public Circle(int x, int y, int radius, Color circleColor) {
super();
thiscircleColor = circleColor;
thisx = x;
thisy = y;
thisradius = radius;
}
@Override
public void paint(Graphics g) {
gsetColor(circleColor);
// 画弧, 当弧的宽度和高度一致且从0~360度时就是原形了
gdrawArc(x, y, radius, radius, 0, 360);
}
}public class SketchpadPanel extends Canvas implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = -5229161042153132522L;
// 鼠标点击起始坐标和当前坐标
private int beginX = 0, beginY = 0, currentX = 0, currentY = 0;
// 判断鼠标是否被按下
private boolean isMousePressing = false;
// 保存当前的图形, 在撤销和恢复时使用
private final Stack<Shape> currentShapes = new Stack<Shape>();
// 保存已经删除过的图形
private final Stack<Shape> deletedShapes = new Stack<Shape>();
private ShapeTypes type;
private Color color;
public SketchpadPanel() {
addMouseListener(this);
addMouseMotionListener(this);
}
/
撤销方法
/
public void undo() {
if (currentShapessize() > 0) {
// 从所有保存过的图形中取出最后一个, 放入到已删除的图形中去
Shape shape = currentShapespop();
deletedShapespush(shape);
repaint();
}
}
/
恢复撤销方法
/
public void redo() {
if (deletedShapessize() > 0) {
// 从所有删除的图形中取出最后一个, 放入保存的图形中
Shape shape = deletedShapespop();
currentShapespush(shape);
repaint();
}
}
/
设置命令
@param type
/
public void setShapeType(ShapeTypes type) {
thistype = type;
}
/
设置颜色
@param color
/
public void setColor(Color color) {
thiscolor = color;
}
public void updete(Graphics g) {
paint(g);
}
/
绘制画板
/
@Override
public void paint(Graphics g) {
// 绘制画板
Dimension size = getSize();
int width = sizewidth;
int height = sizeheight;
gsetColor(ColorWHITE);
gfillRect(0, 0, width, height);
// 绘制所有图形
Shape shape = null;
Enumeration<Shape> e = currentShapeselements();
while (ehasMoreElements()) {
shape = enextElement();
shapepaint(g);
}
// 如果当前鼠标没有释放
if (isMousePressing) {
gsetColor(color);
switch (type) {
// 绘制直线
case LINE:
gdrawLine(beginX, beginY, currentX, currentY);
break;
// 绘制矩形
case RECTANGLE:
if (currentX < beginX) {
if (currentY < beginY) {
// 如果当前位置在起始位置的左上方, 则以鼠标当前位置为矩形的左上角位置
gdrawRect(currentX, currentY, beginX - currentX, beginY - currentY);
} else {
// 如果当前位置在起始位置的左下方, 则以鼠标当前位置的横坐标和起始位置的纵坐标作为矩形的左上角位置
gdrawRect(currentX, beginY, beginX - currentX, currentY - beginY);
}
} else {
if (currentY < beginY) {
// 如果当前位置在起始位置的右上方, 则以鼠标起始位置的很坐标和当前位置的纵坐标作为矩形的左上角位置
gdrawRect(beginX, currentY, currentX - beginX, beginY - currentY);
} else {
// 如果当前位置在起始位置的右下方, 则已起始位置作为矩形的左上叫位置
gdrawRect(beginX, beginY, currentX - beginX, currentY - beginY);
}
}
break;
// 绘制圆形
case CIRCLE:
// 半径为aa + bb的平方根
int radius = (int) Math
sqrt((beginX - currentX) (beginX - currentX) + (beginY - currentY) (beginY - currentY));
gdrawArc(beginX - radius / 2, beginY - radius / 2, radius, radius, 0, 360);
break;
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
/
当鼠标按下的时候获得起始坐标
/
@Override
public void mousePressed(MouseEvent e) {
beginX = egetX();
beginY = egetY();
isMousePressing = true;
}
/
当鼠标释放时获得当前坐标
/
@Override
public void mouseReleased(MouseEvent e) {
currentX = egetX();
currentY = egetY();
isMousePressing = false;
// 当释放鼠标时, 将绘制的图形保存到shapes中
switch (type) {
// 绘制直线
case LINE:
Line line = new Line(beginX, beginY, currentX, currentY, color);
currentShapespush(line);
break;
// 绘制圆形
case CIRCLE:
// 半径为aa + bb的平方根
int radius = (int) Math
sqrt((beginX - currentX) (beginX - currentX) + (beginY - currentY) (beginY - currentY));
Circle circle = new Circle(beginX - radius / 2, beginY - radius / 2, radius, color);
currentShapespush(circle);
break;
// 绘制矩形
case RECTANGLE:
Rectangle rectangle = null;
if (currentX < beginX) {
if (currentY < beginY) {
rectangle = new Rectangle(currentX, currentY, beginX - currentX, beginY - currentY, color);
} else {
rectangle = new Rectangle(currentX, beginY, beginX - currentX, currentY - beginY, color);
}
} else {
if (currentY < beginY) {
rectangle = new Rectangle(beginX, currentY, currentX - beginX, beginY - currentY, color);
} else {
rectangle = new Rectangle(beginX, beginY, currentX - beginX, currentY - beginY, color);
}
}
currentShapespush(rectangle);
break;
}
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
currentX = egetX();
currentY = egetY();
thisrepaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}public class SketchpadFrame extends JFrame {
private static final long serialVersionUID = -7080053971741609904L;
private final JPanel commandPanel = new JPanel(); // 存放命令的面板
private final JPanel colorPanel = new JPanel(); // 存放颜色的面板
private final JPanel mainPanel = new JPanel(); // 主面板
private final JButton redButton = new JButton("红色");
private final JButton blueButton = new JButton("蓝色");
private final JButton greenButton = new JButton("绿色");
private final JButton lineButton = new JButton("直线");
private final JButton circleButton = new JButton("圆");
private final JButton rectangleButton = new JButton("矩形");
private final JButton undoButton = new JButton("撤销");
private final JButton redoButton = new JButton("恢复撤销");
private final JButton exitButton = new JButton("退出");
SketchpadPanel sketchPanel = new SketchpadPanel();
private void initFrame() {
commandPanelsetLayout(new FlowLayout());
commandPaneladd(lineButton);
commandPaneladd(circleButton);
commandPaneladd(rectangleButton);
commandPaneladd(undoButton);
commandPaneladd(redoButton);
commandPaneladd(exitButton);
colorPanelsetLayout(new FlowLayout());
colorPaneladd(redButton);
colorPaneladd(blueButton);
colorPaneladd(greenButton);
mainPanelsetLayout(new BorderLayout());
mainPaneladd(commandPanel, BorderLayoutNORTH);
mainPaneladd(colorPanel, BorderLayoutCENTER);
getContentPane()add("South", mainPanel);
getContentPane()add("Center", sketchPanel);
// 初始化设置:颜色和命令
lineButtonsetForeground(ColorRED);
sketchPanelsetColor(ColorRED);
redButtonsetForeground(ColorRED);
sketchPanelsetShapeType(ShapeTypesLINE);
}
private void initListener() {
redButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redAction(e);
}
});
blueButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
blueAction(e);
}
});
greenButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
greenAction(e);
}
});
undoButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
undoAction(e);
}
});
redoButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redoAction(e);
}
});
exitButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitAction(e);
}
});
lineButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
lineAction(e);
}
});
circleButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
circleAction(e);
}
});
rectangleButtonaddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rectangleAction(e);
}
});
}
public SketchpadFrame() {
initFrame();
initListener();
thissetSize(500, 600);
setLocationByPlatform(true);
setResizable(true);
}
/ 处理事件 /
private void undoAction(ActionEvent e) {
sketchPanelundo();
}
private void redoAction(ActionEvent e) {
sketchPanelredo();
}
private void exitAction(ActionEvent e) {
Systemexit(0);
}
private void lineAction(ActionEvent e) {
// 选中按钮为红色, 其余为黑色
lineButtonsetForeground(ColorRED);
circleButtonsetForeground(ColorBLACK);
rectangleButtonsetForeground(ColorBLACK);
sketchPanelsetShapeType(ShapeTypesLINE);
}
private void circleAction(ActionEvent e) {
circleButtonsetForeground(ColorRED);
lineButtonsetForeground(ColorBLACK);
rectangleButtonsetForeground(ColorBLACK);
sketchPanelsetShapeType(ShapeTypesCIRCLE);
}
private void rectangleAction(ActionEvent e) {
rectangleButtonsetForeground(ColorRED);
lineButtonsetForeground(ColorBLACK);
circleButtonsetForeground(ColorBLACK);
sketchPanelsetShapeType(ShapeTypesRECTANGLE);
}
private void redAction(ActionEvent e) {
redButtonsetForeground(ColorRED);
blueButtonsetForeground(ColorBLACK);
greenButtonsetForeground(ColorBLACK);
sketchPanelsetColor(ColorRED);
}
private void blueAction(ActionEvent e) {
blueButtonsetForeground(ColorRED);
redButtonsetForeground(ColorBLACK);
greenButtonsetForeground(ColorBLACK);
sketchPanelsetColor(ColorBLUE);
}
private void greenAction(ActionEvent e) {
greenButtonsetForeground(ColorRED);
redButtonsetForeground(ColorBLACK);
blueButtonsetForeground(ColorBLACK);
sketchPanelsetColor(ColorGREEN);
}
}/
@author 不落的太阳(Sean Yang)
@version 10
@since JDK 18
/
public class SketchpadMain {
/
测试方法
@param args命令行参数
/
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new SketchpadFrame();
framesetVisible(true);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
});
}
}
不要用canvaspaint(gg);
你把Mycanvas当做参数传到你写canvaspaint(gg);的类里然后增加监听函数
final Display display = DisplaygetDefault();
mycanvasaddPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent arg0) {
GC gc = new GC(mycanvas);
gcsetBackground(displaygetSystemColor(SWTCOLOR_BLACK));
gcfillOval(450, 300, 20, 20);//画圆点
gcsetForeground(displaygetSystemColor(SWTCOLOR_BLUE));
gcdrawLine(500, 400, 700, 400);//画直线
}
});
这样就可以在画板里画出来~~
paint方法是自动调用的。如果你要绘图写一个自己的类,从Canvas类继承,重写paint方法,在paint方法里写你要绘制的图形、等内容。
界面刷新调用update(Graphics
g)方法
以上就是关于JAVA 改变Canvas画布尺寸全部的内容,包括:JAVA 改变Canvas画布尺寸、求一个java程序:绘图程序包括画圆,椭圆,线,矩形,自定义。并且可以调图形颜色!、JavaSE用Canvas画图的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)