package v01;
import javaxswing;
import javaawt;
public class ColorP extends JFrame {
public ColorP(){
thisadd(new NewPanel());
}
public static void main(String[] args) {
ColorP frame = new ColorP();
framesetTitle("Content");
framesetSize(300, 150);
framesetLocationRelativeTo(null);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
class NewPanel extends JPanel{//扩展JPanel
public NewPanel(){
}
protected void paintComponent(Graphics g){//运用画笔Graphics绘制
superpaintComponent(g);//添加画笔
int[] x = new int[4];
int[] y = new int[4];
for(int i = 0; i < 10; i++){
x[0] = i 40 ;y[0] = 0;
x[1] = x[0] + 40;y[1] = 0;
x[2] = 0;y[2] = (i+1) 40;
x[3] = 0;y[3] = y[2] - 40 ;
if(i % 2 == 0)
gsetColor(Colorblack);
else
gsetColor(Coloryellow);
gfillPolygon(x, y, xlength);
}
}
}
}
按照你的要求编写的Java程序如下:
import javaawtBorderLayout;import javaawtColor;
import javaawtGraphics;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventItemEvent;
import javaawteventItemListener;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javaawteventMouseMotionListener;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioObjectInputStream;
import javaioObjectOutputStream;
import javaioSerializable;
import javautilArrayList;
import javautilList;
import javaxswingButtonGroup;
import javaxswingJButton;
import javaxswingJColorChooser;
import javaxswingJFileChooser;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJRadioButton;
import javaxswingfilechooserFileNameExtensionFilter;
public class CAA extends JFrame implements ActionListener,ItemListener{
List<PainterObject> arraylist=new ArrayList<PainterObject>();
JRadioButton jrb1=new JRadioButton("Line",true);
JRadioButton jrb2=new JRadioButton("Rect");
JRadioButton jrb3=new JRadioButton("RoundRect");
JRadioButton jrb4=new JRadioButton("FillRect");
ButtonGroup bg=new ButtonGroup();
JButton jb1=new JButton("Color Chooser");
JButton jb2=new JButton("Save");
JButton jb3=new JButton("Load");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
PainterJPanel pjp=new PainterJPanel();
int type=1;//type=1画线2画长方形3画圆角长方形4画填充长方形
Color c;
String saveType[] = {"obj"}; //存储文件的扩展名
CAA(){
super("Painter");
bgadd(jrb1);bgadd(jrb2);bgadd(jrb3);bgadd(jrb4);
jp1setLayout(new GridLayout(2,2));
jp1add(jrb1);jp1add(jrb2);jp1add(jrb3);jp1add(jrb4);
jp2setLayout(new GridLayout(1,3));
jp2add(jb1);jp2add(jb2);jp2add(jb3);
jrb1addItemListener(this);
jrb2addItemListener(this);
jrb3addItemListener(this);
jrb4addItemListener(this);
jb1addActionListener(this);
jb2addActionListener(this);
jb3addActionListener(this);
add(jp1,BorderLayoutNORTH);
add(jp2,BorderLayoutSOUTH);
add(pjp,BorderLayoutCENTER);
setSize(500, 500);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if(aegetSource()==jb1){
c = JColorChoosershowDialog(this, "Choose a Color",pjpgetForeground());
if (c != null)
pjpsetForeground(c);
}
if(aegetSource()==jb2){
JFileChooser fd = new JFileChooser();
fdsetFileFilter(new FileNameExtensionFilter("obj", saveType));
fdshowSaveDialog(this);
File f = fdgetSelectedFile();
if(f != null){
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f+"obj"));
outwriteObject(arraylist);
outclose();
}catch(Exception e){
egetStackTrace();
}
}
}
if(aegetSource()==jb3){
JFileChooser fd = new JFileChooser();
fdsetFileFilter(new FileNameExtensionFilter("obj", saveType));
fdshowOpenDialog(this);
File f = fdgetSelectedFile();
if(f != null){
try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
arraylist=(ArrayList)inreadObject();
pjprepaint();
inclose();
}catch(Exception e){
egetStackTrace();
}
}
}
}
@Override
public void itemStateChanged(ItemEvent ie) {
if(iegetStateChange()==ItemEventSELECTED){
if(iegetItem()==jrb1){
type=1;
}
if(iegetItem()==jrb2){
type=2;
}
if(iegetItem()==jrb3){
type=3;
}
if(iegetItem()==jrb4){
type=4;
}
}
}
public static void main(String[] args) {
new CAA();
}
class PainterJPanel extends JPanel implements MouseListener,MouseMotionListener{
int x,y;
int preparex,preparey;
PainterPoint pPointStart;
PainterPoint pPointEnd;
PainterJPanel(){
setOpaque(true);
setBackground(ColorWHITE);
thisaddMouseListener(this);
thisaddMouseMotionListener(this);
}
public void paint(Graphics g){
superpaint(g);
for(int i=0;i<arraylistsize();i++){
PainterObject pObject=(PainterObject)arraylistget(i);
gsetColor(pObjectgetColor());
int x1=pObjectgetPpStart()getX();
int y1=pObjectgetPpStart()getY();
int x2=pObjectgetPpEnd()getX();
int y2=pObjectgetPpEnd()getY();
switch(pObjectgetType()){
case 1:gdrawLine(x1,y1,x2,y2);break;
case 2:gdrawRect(x1, y1, Mathabs(x2-x1), Mathabs(y2-y1));break;
case 3:gdrawRoundRect(x1, y1, Mathabs(x2-x1), Mathabs(y2-y1),20,20);break;
case 4:gfillRect(x1, y1, Mathabs(x2-x1), Mathabs(y2-y1));break;
}
}
gsetColor(c);
switch(type){
case 1:gdrawLine(x, y, preparex, preparey);break;
case 2:gdrawRect(x, y, Mathabs(preparex-x), Mathabs(preparey-y));break;
case 3:gdrawRoundRect(x, y, Mathabs(preparex-x), Mathabs(preparey-y),20,20);break;
case 4:gfillRect(x, y, Mathabs(preparex-x), Mathabs(preparey-y));break;
}
}
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent me) {
x=megetX();
y=megetY();
pPointStart=new PainterPoint();
pPointStartsetX(x);
pPointStartsetY(y);
}
@Override
public void mouseReleased(MouseEvent me) {
x=megetX();
y=megetY();
pPointEnd=new PainterPoint();
pPointEndsetX(x);
pPointEndsetY(y);
PainterObject po=new PainterObject();
posetPpStart(pPointStart);
posetPpEnd(pPointEnd);
posetType(type);
posetColor(c);
arraylistadd(po);
repaint();
}
@Override
public void mouseDragged(MouseEvent me) {
preparex=megetX();
preparey=megetY();
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {}
}
}
class PainterObject implements Serializable{
PainterPoint ppStart;
PainterPoint ppEnd;
int type;
Color color;
public PainterPoint getPpStart() {
return ppStart;
}
public void setPpStart(PainterPoint ppStart) {
thisppStart = ppStart;
}
public PainterPoint getPpEnd() {
return ppEnd;
}
public void setPpEnd(PainterPoint ppEnd) {
thisppEnd = ppEnd;
}
public int getType() {
return type;
}
public void setType(int type) {
thistype = type;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
thiscolor = color;
}
}
class PainterPoint implements Serializable{
int x;
int y;
public int getX() {
return x;
}
public void setX(int x) {
thisx = x;
}
public int getY() {
return y;
}
public void setY(int y) {
thisy = y;
}
}
运行结果:
首先java画布是一个容器,要在容器里面添加组件或者需要先导入或者声明组件然后将组件添加到容器中。
具体实现:
Icon ic=new ImageIcon("loadjpg");
JLabel lb=new JLabel(ic);
JPanel p=new JPanel();
padd(lb);
这样就可以了。首先要应用GUI相关的包比如import javaawt;import javaxswing;
给你个例子吧
public class Test extends JFrame{
MyPanel mp=null;
public static void main(String[] args){
// TODO Auto-generated method stub
Test jf= new Test();
}
public Test(){
mp=new MyPanel();
thisadd(mp);
//设置标题
thissetTitle("绘图");
//设置窗体大小
thissetSize(400, 300);
//设置窗体的位置
thissetLocation(100,100);
//限制窗体的大小
thissetResizable(false);
//关闭窗体时,同时退出java虚拟机
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
//显示窗体
thissetVisible(true);
}
}
//定义一个MyPanel(我自己的面板,用于绘图和实现绘图区域)
class MyPanel extends JPanel
{
//覆盖JPanel的paint方法
//Graphics是绘图的重要类,可以把它理解成一只画笔
public void paint(Graphics g)
{
//1。调用父类函数完成初始化
superpaint(g);
// //画圆
// gdrawOval(100, 100, 20, 20);
// //画直线
// gdrawLine(50, 150,150, 200);
// //画矩形边框
// gdrawRect(150, 150, 30, 40);
//
// //设置颜色。默认为黑色
// gsetColor(Colorblue);
// //填充矩形
// gfillRect(10, 10, 20, 30);
//画弧形
gdrawArc(200,10, 100,150, 120,-80);
//在面板上画
Image im=ToolkitgetDefaultToolkit()getImage(PanelclassgetResource("路径"));
//显示
gdrawImage(im, 10, 10,200,180,this);
//画字
gsetColor(Colorred);
gsetFont(new Font("华文彩云",FontBOLD,20));
gdrawString("要写的字", 80,220);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)