📄 drawpanel.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.lang.*;
/**the class used to show the image*/
public class DrawPanel extends JPanel {
/**store each shape*/
public Vector<ImageInfo> store;
/**the creation function*/
DrawPanel()
{
store = new Vector<ImageInfo>();
}
/**add a pencial path*/
public void add(GeneralPath g, Color c, Integer t)
{
store.add(new ImageInfo(g, c, t));
}
/**add a general shape*/
public void add(Shape s, Color c, Integer f, Integer t)
{
store.add(new ImageInfo(s, c, f, t));
}
/**add a new Image*/
public void add(ImageInfo o)
{
store.add(o);
}
/**clear the Image*/
public void clear()
{
store.clear();
}
/**set the index element*/
public void setElementAt(int index, Shape s, Color c, Integer f, Integer t)
{
store.setElementAt(new ImageInfo(s, c, f, t), index);
}
/**set the index element*/
public void setElementAt(int index, ImageInfo i)
{
store.setElementAt(i, index);
}
/**remove the index element*/
public void removeElementAt(int index)
{
store.removeElement(index);
}
/**remove the index element*/
public void remove(int index)
{
store.remove(index);
}
/**return the index shape*/
public ImageInfo getObj(int index)
{
return store.get(index);
}
/**return the number of shapes*/
public int length()
{
return (int)(store.size());
}
/**return the number of object who is selected*/
public int check(int x, int y)
{
for(int i = 0; i < store.size(); i++)
{
if(store.elementAt(i).fill == 0 || store.elementAt(i).fill == 1)
{
if(store.elementAt(i).shape.contains(x, y))
return i;
}else if(store.elementAt(i).fill == 2){//System.exit(0);
int x1 = store.elementAt(i).shape.getBounds().x;
int w = store.elementAt(i).shape.getBounds().width;
int y1 = store.elementAt(i).shape.getBounds().y;
int h = store.elementAt(i).shape.getBounds().height;
if(x >= x1 && x <= x1+w && y >= y1 && y <= y1+h )
return i;
}else if(store.elementAt(i).fill == 3){
int x1 = store.elementAt(i).shape.getBounds().x;
int w = store.elementAt(i).shape.getBounds().width;
int y1 = store.elementAt(i).shape.getBounds().y;
int h = store.elementAt(i).shape.getBounds().height;
if(x >= x1 && x <= x1+w && y >= y1 && y <= y1+h )
return i;
}else if(store.elementAt(i).fill == 4){
if(x >= store.elementAt(i).x && x <= store.elementAt(i).x+store.elementAt(i).font.getSize()*store.elementAt(i).chat.length()*0.6 && y <= store.elementAt(i).y && y >= store.elementAt(i).y-store.elementAt(i).font.getSize()*0.7)
return i;
}
}
return -1;
}
/**the repaint function*/
public void paintComponent(Graphics g) {
super.paintComponent(g); //use father class
Graphics2D g2D = (Graphics2D)g;
for(int i = 0; i < store.size(); i++)
{
g2D.setColor(store.elementAt(i).color); //set color
if(store.elementAt(i).fill == 0) //draw or fill
{
g2D.setStroke(new BasicStroke(store.elementAt(i).thick, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); //设置新的画刷
g2D.draw(store.elementAt(i).shape);
}else if(store.elementAt(i).fill == 1){
g2D.fill(store.elementAt(i).shape);
}else if(store.elementAt(i).fill == 2){
g2D.setStroke(new BasicStroke(store.elementAt(i).thick, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); //设置新的画刷
g2D.draw(store.elementAt(i).shape);
}else if(store.elementAt(i).fill ==3){
g2D.setStroke(new BasicStroke(store.elementAt(i).thick, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); //设置新的画刷
g2D.draw(store.elementAt(i).shape);
}else if(store.elementAt(i).fill == 4){
g2D.setFont(store.elementAt(i).font);
g2D.drawString(store.elementAt(i).chat, store.elementAt(i).x, store.elementAt(i).y);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -