導航:首頁 > 編程語言 > java繪圖板

java繪圖板

發布時間:2022-11-30 10:48:48

㈠ 用java怎麼 將繪圖保存成圖片

用ImageIO類提供的靜態方法write(img,foamt,outStream);

先指定文件:
File f=new File("路徑");
文件不存在就創建
if(!f.exists()){
f.createNewFile();

}
打開文件流
FileOutputStream fos=new FileOutputStream(f);
然後寫入圖片數據類型是BufferedImage
ImageIO.write(img,"png",fos);

㈡ JAVA Canvas面板和Jpanel面板有什麼區別求解

1。Java Canvas(畫布)是重量級面板,Jpanel是輕量級的
2。Java Canvas 的功能主要是用來繪圖(個人理解),Jpanel的主要功能是添加組件

㈢ java 繪圖板 如何實現畫直線的功能

這個簡單
可以調用方法drawline(int x1, int y1, int x2, int y2)
其中(x1, y1), (x2, y2)分別為直線起點和終點的坐標
特意給你寫了個小例子,希望能幫到你
***************************************
import javax.swing.*;
import java.awt.*;

public class DrawLine extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("DrawLine");
frame.getContentPane().add(new DrawLine());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(50, 50, 200, 250);
}
}
****************************
將程序復制到記事本中並改名為DrawLine.java(注意大小寫)
然後在命令行中用 CD+文件夾 使命令范圍到DrawLine.java的文件夾中
然後執行命令javac DrawLine.java
再執行命令java DrawLine
你會看到結果

㈣ java,親,為什麼我給繪圖面板的背景色不起作用啊他還是默認的白色啊

public void init(){
setSize(400, 300);

設置一下大小,然後不停點擊按鈕,就有效果了。

如果想自動,用 javax.swing.Timer 吧

㈤ java 如何在jpanel上畫垂直線、水平線、平行線

定義一個類繼承JPanel類,重寫 JPanel 的 paint 方法,使用 Graphics 繪制線段,如下代碼:

importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Graphics;

importjavax.swing.JFrame;
importjavax.swing.JPanel;

classMyPanelextendsJPanel{

@Override
publicvoidpaint(Graphicsg){
super.paint(g);

g.setColor(Color.RED);

for(inti=0;i<10;i++){
g.drawLine(10,10+i*20,this.getWidth()-10,10+i*20);
}

for(inti=0;i<10;i++){
g.drawLine(10+i*20,10,10+i*20,this.getHeight()-10);
}
}
}

publicclassAppextendsJFrame{

publicApp(){

this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyPanelpanel=newMyPanel();
this.add(panel,BorderLayout.CENTER);
}

publicstaticvoidmain(String[]args){
newApp().setVisible(true);
}
}

運行結果:

㈥ 用Java實現畫圖板功能的程序,請問如何編寫一個繪制三角形的程序段

class Triangle extends drawings//空心三角形類
{
void draw(Graphics2D g2d)
{g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke,
BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL));
g2d.drawLine((int)((x1+x2)/2),Math.min(y1,y2),Math.max(x1,x2),Math.max(y1,y2));
g2d.drawLine(Math.max(x1,x2),Math.max(y1,y2),Math.min(x1,x2),Math.max(y1,y2));
g2d.drawLine(Math.min(x1,x2),Math.max(y1,y2),(int)((x1+x2)/2),Math.min(y1,y2));
}
}
以上是通過繪制三條直線作為三角形的三條邊來繪制三角形.
class fillTriangle extends drawings//實心三角形
{
void draw(Graphics2D g2d)
{g2d.setPaint(new Color(R,G,B));
g2d.setStroke(new BasicStroke(stroke));
int mx=(int)((x1+x2)/2);
int[] x={mx,Math.max(x1,x2),Math.min(x1,x2)};
int[] y={Math.min(y1,y2),Math.max(y1,y2),Math.max(y1,y2)};
g2d.fillPolygon(x,y,3);

}
}
以上是用填充多邊形的方式填充一個三角形,如果把最後的:g2d.fillPolygon(x,y,3)改為g2d.drawPolygon(x,y,3); 則是以繪制多邊形的方式繪制空心三角形.
這里說明一下:因為(x1,y1,x2,y2)只能確定一個矩形區域,即滑鼠拉動的起點和終點確定的矩形區域所以可以有多種方式確定三角形的三個頂點,我這個用的三個頂點是:
點1( (x1+x2)/2, min(y) ) 點2( max(x),max(y) ) 點3( min(x),max(y) )

你的補充內容太多了,沒心情看啊,太累了

㈦ GUI畫圖板(繪圖板)設計,用Java編寫程序代碼!!謝謝!!

只有矩形有圓形能移動,其它實現起來麻煩點,辦法有的只是代碼太多。
畫圓弧改成了畫曲線,圓弧稍麻煩,當然方法是很簡單的,你可以自己思考一下。
雙擊13個顏色中的任意一個都會彈出顏色選擇器。
有保存與打開功能。擴展名請用 .jdr
基本滿足條件,細節可能不是很好,另,代碼比較亂,怕不好看懂咯,呼呼。

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class JDraw {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setDefaultCloseOperation(3);
f.setSize(880,600);
f.setLocationRelativeTo(null);
f.getContentPane().add(M.c);
f.getContentPane().add(M.m,"South");
f.setVisible(true);
}
}

class CVS extends Component implements ComponentListener,MouseListener,MouseMotionListener{
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentResized(ComponentEvent e) {resized();}
public void componentShown(ComponentEvent e) {}
private void resized() {
int w=this.getWidth();
int h=this.getHeight();
tbuff=new BufferedImage(w,h,3);
makeBuff(w,h);
}
private void makeBuff(int w,int h) {
Graphics g = tbuff.getGraphics();
g.drawImage(buff,0,0,null);
g.dispose();
buff=new BufferedImage(w,h,3);
g=buff.getGraphics();
g.drawImage(tbuff,0,0,null);
g.dispose();
}

BufferedImage buff,tbuff;
CVS(){
this.addComponentListener(this);
this.addMouseListener(this);
this.addMouseMotionListener(this);
buff=new BufferedImage(1,1,3);
}

public void paint(Graphics gr){
Graphics2D g = buff.createGraphics();
g.setBackground(new Color(0xff000000,true));
g.clearRect(0,0,getWidth(),getHeight());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
M.sa.drawAll(g);
if(M.ts!=null)
M.ts.draw(g);
g.dispose();
gr.drawImage(buff,0,0,this);
gr.dispose();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
M.mp(e.getPoint());
}
public void mouseReleased(MouseEvent e) {
M.mr(e.getPoint());
}
public void mouseDragged(MouseEvent e) {
M.md(e.getPoint());
}
public void mouseMoved(MouseEvent e) {}
}
class Menu extends JComponent implements MouseListener,ActionListener{
JComboBox sbox,method;
CLabel[] cl;
JCheckBox fillC,drawB;
JRadioButton fc,bc;
ButtonGroup bg;
JButton clear,up,down,save,load;
Menu(){
this.setLayout(new FlowLayout());
method=new JComboBox(new Object[]{"draw","move","erase",});
add(method);
sbox=new JComboBox(new Object[]{"Pt","Ln","Rect","Cir","Arc",});
add(sbox);
cl=new CLabel[13];
for(int i=0; i<cl.length; i++){
cl[i]=new CLabel();
cl[i].addMouseListener(this);
add(cl[i]);
}
fc=new JRadioButton("fc",true);
bc=new JRadioButton("bc");
bg=new ButtonGroup();
bg.add(fc);bg.add(bc);
add(fc);add(bc);
fc.setOpaque(true);
bc.setOpaque(true);
fc.setBackground(Color.white);
bc.setBackground(Color.blue);
fillC=new JCheckBox("Fill",true);
drawB=new JCheckBox("Draw",true);
fillC.addActionListener(this);
drawB.addActionListener(this);
add(fillC);add(drawB);
clear=new JButton("clear");
clear.addActionListener(this);
add(clear);
up=new JButton("zUp");
up.addActionListener(this);
add(up);
down=new JButton("zDown");
down.addActionListener(this);
add(down);
save=new JButton("Save");
save.addActionListener(this);
add(save);
load=new JButton("Load");
load.addActionListener(this);
add(load);
}
public void mouseClicked(MouseEvent e) {
JLabel l=(JLabel)e.getSource();
if(e.getClickCount()==2){
Color sc=JColorChooser.showDialog(null, "Color chooser", l.getBackground());
l.setBackground(sc);
mousePressed(e);
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
Color c=((JLabel)e.getSource()).getBackground();
if(fc.isSelected())
fc.setBackground(c);
else if(bc.isSelected())
bc.setBackground(c);
M.cp();
}
public void mouseReleased(MouseEvent e) {}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==clear)M.clear();
else if(e.getSource()==up)M.up();
else if(e.getSource()==down)M.down();
else if(e.getSource()==save)M.save();
else if(e.getSource()==load)M.load();
else if(e.getSource()==fillC||e.getSource()==drawB)M.cp();
}
}
class CLabel extends JLabel{
static Color[] cs={Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,
Color.blue,Color.magenta,Color.magenta.brighter(),
Color.white,Color.black,Color.gray,Color.LIGHT_GRAY,Color.DARK_GRAY,};
static int i;
CLabel(){
this.setOpaque(true);
this.setBackground(cs[i++]);
this.setBorder(BorderFactory.createLineBorder(Color.black));
this.setPreferredSize(new Dimension(10,20));
}
}
class M{
static JFileChooser jfc=new JFileChooser();
static Menu m=new Menu();
static CVS c=new CVS();
static SA sa=new SA();
static S ts=null,selected=null;
static Color fc,bc;
static void clear(){
sa.ss.clear();
c.repaint();
}
public static void cp() {
System.out.println(selected);
if(selected!=null){
selected.fillColor=m.fc.getBackground();
selected.borderColor=m.bc.getBackground();
selected.fc=m.fillC.isSelected();
selected.db=m.drawB.isSelected();
c.repaint();
}
}
public static void up() {
if(selected!=null){
sa.upZ(selected);
c.repaint();
}
}
public static void down(){
if(selected!=null){
sa.downZ(selected);
c.repaint();
}
}
static{
jfc.setFileFilter(new FileNameExtensionFilter("JDraw file (*.jdraw,*.jdr)","jdr","jdraw"));
}
static void save(){
int x=jfc.showSaveDialog(c);
if(x==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
try{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(sa);
oos.flush();
oos.close();
}catch(Exception e){}
}
}
static void load(){
int x=jfc.showOpenDialog(c);
if(x==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
try{
ObjectInputStream oos=new ObjectInputStream(new FileInputStream(f));
Object r=oos.readObject();
if(r!=null){
sa=(SA)r;
c.repaint();
}
oos.close();
}catch(Exception e){e.printStackTrace();}
}
}
static int mx,my,tx,ty,ox,oy;
static int pc=0,pmax;
static int sm;
static boolean ne=true;
static int mid;
static void mp(Point p){
mid=m.method.getSelectedIndex();
if(mid==0){
if(ne){
mx=p.x;my=p.y;
pc=0;
sm=m.sbox.getSelectedIndex();
pmax=sm==4?2:1;
ne=false;
}
++pc;
}
else if(mid==1){
checkSel(p);
mx=p.x;my=p.y;
}
else if(mid==2){
checkSel(p);
if(selected!=null){
sa.ss.remove(selected);
c.repaint();
}
}
}
private static void checkSel(Point p) {
selected=null;
for(int i=sa.ss.size();i>0; i--)
if(sa.ss.get(i-1).shape.contains(p)){
selected=sa.ss.get(i-1);break;
}
sa.select(selected);
c.repaint();
}
static void mt(){
Shape s=null;
int cx,cy,cw,ch;
switch(sm){
case 0:
case 2:
cx=Math.min(mx,tx);
cy=Math.min(my,ty);
cw=Math.abs(mx-tx);
ch=Math.abs(my-ty);
if(sm==0)
s=new Ellipse2D.Double(cx,cy,cw,ch);
else
s=new Rectangle(cx,cy,cw,ch);
break;
case 1:
s=new Line2D.Float(mx,my,tx,ty);
break;
case 3:
cw=Math.abs(mx-tx);
ch=Math.abs(my-ty);
int cd=(int)Math.sqrt(Math.pow(mx-tx,2)+Math.pow(my-ty,2))*2;
cx=mx-cd/2;
cy=my-cd/2;
s=new Ellipse2D.Double(cx,cy,cd,cd);
break;
case 4:
Path2D p=new Path2D.Double();
p.moveTo(mx,my);
if(pc==1){
p.lineTo(tx, ty);
}
else{
p.quadTo(ox,oy,tx,ty);
}
s=p;
break;
}
ts=new S(s,m.fc.getBackground(),m.bc.getBackground(),m.fillC.isSelected(),m.drawB.isSelected(),null);
c.repaint();
}
static void md(Point p){
if(mid==0){
if(!sa.ss.isEmpty()){
sa.ss.get(sa.ss.size()-1).sel=false;
}
if(pc>1){
ox=p.x;oy=p.y;
}
else{
tx=p.x;ty=p.y;
}
mt();
}
else if(mid==1){
if(selected!=null){
moveTo(selected,p);
c.repaint();
}
}
else if(mid==2){
checkSel(p);
if(selected!=null){
sa.ss.remove(selected);
c.repaint();
}
}
}
static void moveTo(S s, Point p) {
if(s.shape instanceof Rectangle){
Rectangle r=(Rectangle)s.shape;
r.setLocation(r.x+p.x-mx,r.y+p.y-my);
mx=p.x;my=p.y;
}
else if(s.shape instanceof Ellipse2D){
Ellipse2D e=(Ellipse2D)s.shape;
e.setFrame(e.getX()+p.x-mx,e.getY()+p.y-my,e.getWidth(),e.getHeight());
mx=p.x;my=p.y;
}
}
static void mr(Point p) {
if(pc==pmax){
pc=0;
ne=true;
sa.add(ts);
selected=ts;
ts=null;
}
}
}

class S implements Serializable{
boolean fc,db,sel=true;
Shape shape;
Color fillColor,borderColor;
Stroke stroke;
static Stroke bstroke=new MyBasicStroke();
static Stroke selectStroke=new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,1,new float[]{5,2},1);
S(Shape s,Color c,Color b,boolean f,boolean d,Stroke k){
this.shape=s;this.fillColor=c;this.borderColor=b;this.fc=f;this.db=d;this.stroke=k==null?bstroke:k;
}
void draw(Graphics2D g){
if(fc){
g.setColor(fillColor);
g.fill(shape);
}
if(db){
g.setColor(borderColor);
g.setStroke(stroke);
g.draw(shape);
}
if(sel){
g.setColor(Color.green);
g.setStroke(selectStroke);
g.draw(shape.getBounds());
}
}
}
class MyBasicStroke extends BasicStroke implements Serializable{}
class SA implements Serializable{
ArrayList<S> ss=new ArrayList<S>();
void add(S s){
if(s!=null){
for(S sx:ss)
sx.sel=false;
ss.add(s);
}
}
S remove(int i){
return ss.remove(i);
}
void remove(S s){
ss.remove(s);
}
void upZ(S s){
indexZ(s,1);
}
void downZ(S s){
indexZ(s,-1);
}
void indexZ(S s, int i) {
int si=ss.indexOf(s);
if(si+i<0||si+i>ss.size()-1)return;
swap(s,ss.get(si+i));
}
void swap(S a,S b){
int ai=ss.indexOf(a);
int bi=ss.indexOf(b);
ss.set(ai,b);
ss.set(bi,a);
}
void select(S s){
for(S x:ss)
x.sel=false;
if(s!=null)
s.sel=true;
}
void drawAll(Graphics2D g){
for(S s:ss)
s.draw(g);
}
}

㈧ 求一個java程序:繪圖程序包括畫圓,橢圓,線,矩形,自定義。並且可以調圖形顏色!

publicenumShapeTypes{
LINE,CIRCLE,RECTANGLE
}
publicinterfaceShape{
voidpaint(Graphicsg);
}
{

//矩形左上角的坐標
privateintx,y;
//矩形的寬度和高度
privateintwidth,height;

privateColorrectangleColor;

publicRectangle(){
super();
}

publicRectangle(intx,inty,intwidth,intheight,ColorrectangleColor){
super();
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.rectangleColor=rectangleColor;
}

@Override
publicvoidpaint(Graphicsg){
g.setColor(rectangleColor);
g.drawRect(x,y,width,height);
}
}
{

//直線的起始位置
privateintx1,y1;
//直線的終止位置
privateintx2,y2;

privateColorlineColor;

publicLine(intx1,inty1,intx2,inty2,ColorlineColor){
super();
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.lineColor=lineColor;
}

publicLine(){
super();
}

@Override
publicvoidpaint(Graphicsg){
g.setColor(lineColor);
g.drawLine(x1,y1,x2,y2);
}
}
{

//圓的顏色
privateColorcircleColor;
//圓心的坐標
privateintx,y;
//圓的半徑
privateintradius;

publicCircle(){
super();
}

publicCircle(intx,inty,intradius,ColorcircleColor){
super();
this.circleColor=circleColor;
this.x=x;
this.y=y;
this.radius=radius;
}

@Override
publicvoidpaint(Graphicsg){
g.setColor(circleColor);
//畫弧,當弧的寬度和高度一致且從0~360度時就是原形了
g.drawArc(x,y,radius,radius,0,360);
}
}
,MouseMotionListener{

=-5229161042153132522L;

//滑鼠點擊起始坐標和當前坐標
privateintbeginX=0,beginY=0,currentX=0,currentY=0;

//判斷滑鼠是否被按下
privatebooleanisMousePressing=false;

//保存當前的圖形,在撤銷和恢復時使用
privatefinalStack<Shape>currentShapes=newStack<Shape>();

//保存已經刪除過的圖形
privatefinalStack<Shape>deletedShapes=newStack<Shape>();

privateShapeTypestype;
privateColorcolor;

publicSketchpadPanel(){
addMouseListener(this);
addMouseMotionListener(this);
}

/**
*撤銷方法
*/
publicvoindo(){
if(currentShapes.size()>0){
//從所有保存過的圖形中取出最後一個,放入到已刪除的圖形中去
Shapeshape=currentShapes.pop();
deletedShapes.push(shape);
repaint();
}
}

/**
*恢復撤銷方法
*/
publicvoidredo(){
if(deletedShapes.size()>0){
//從所有刪除的圖形中取出最後一個,放入保存的圖形中
Shapeshape=deletedShapes.pop();
currentShapes.push(shape);
repaint();
}
}

/**
*設置命令
*
*@paramtype
*/
publicvoidsetShapeType(ShapeTypestype){
this.type=type;
}

/**
*設置顏色
*
*@paramcolor
*/
publicvoidsetColor(Colorcolor){
this.color=color;
}

publicvoipdete(Graphicsg){
paint(g);
}

/**
*繪制畫板
*/
@Override
publicvoidpaint(Graphicsg){
//繪制畫板
Dimensionsize=getSize();
intwidth=size.width;
intheight=size.height;
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);

//繪制所有圖形
Shapeshape=null;
Enumeration<Shape>e=currentShapes.elements();
while(e.hasMoreElements()){
shape=e.nextElement();
shape.paint(g);
}

//如果當前滑鼠沒有釋放
if(isMousePressing){
g.setColor(color);
switch(type){
//繪制直線
caseLINE:
g.drawLine(beginX,beginY,currentX,currentY);
break;
//繪制矩形
caseRECTANGLE:
if(currentX<beginX){
if(currentY<beginY){
//如果當前位置在起始位置的左上方,則以滑鼠當前位置為矩形的左上角位置
g.drawRect(currentX,currentY,beginX-currentX,beginY-currentY);
}else{
//如果當前位置在起始位置的左下方,則以滑鼠當前位置的橫坐標和起始位置的縱坐標作為矩形的左上角位置
g.drawRect(currentX,beginY,beginX-currentX,currentY-beginY);
}
}else{
if(currentY<beginY){
//如果當前位置在起始位置的右上方,則以滑鼠起始位置的很坐標和當前位置的縱坐標作為矩形的左上角位置
g.drawRect(beginX,currentY,currentX-beginX,beginY-currentY);
}else{
//如果當前位置在起始位置的右下方,則已起始位置作為矩形的左上叫位置
g.drawRect(beginX,beginY,currentX-beginX,currentY-beginY);
}
}
break;
//繪制圓形
caseCIRCLE:
//半徑為a*a+b*b的平方根
intradius=(int)Math
.sqrt((beginX-currentX)*(beginX-currentX)+(beginY-currentY)*(beginY-currentY));
g.drawArc(beginX-radius/2,beginY-radius/2,radius,radius,0,360);
break;
}
}

}

@Override
publicvoidmouseClicked(MouseEvente){
}

@Override
publicvoidmouseEntered(MouseEvente){
}

@Override
publicvoidmouseExited(MouseEvente){
}

/**
*當滑鼠按下的時候獲得起始坐標
*/
@Override
publicvoidmousePressed(MouseEvente){
beginX=e.getX();
beginY=e.getY();
isMousePressing=true;
}

/**
*當滑鼠釋放時獲得當前坐標
*/
@Override
publicvoidmouseReleased(MouseEvente){
currentX=e.getX();
currentY=e.getY();
isMousePressing=false;

//當釋放滑鼠時,將繪制的圖形保存到shapes中
switch(type){
//繪制直線
caseLINE:
Lineline=newLine(beginX,beginY,currentX,currentY,color);
currentShapes.push(line);
break;
//繪制圓形
caseCIRCLE:
//半徑為a*a+b*b的平方根
intradius=(int)Math
.sqrt((beginX-currentX)*(beginX-currentX)+(beginY-currentY)*(beginY-currentY));
Circlecircle=newCircle(beginX-radius/2,beginY-radius/2,radius,color);
currentShapes.push(circle);
break;
//繪制矩形
caseRECTANGLE:
Rectanglerectangle=null;
if(currentX<beginX){
if(currentY<beginY){
rectangle=newRectangle(currentX,currentY,beginX-currentX,beginY-currentY,color);
}else{
rectangle=newRectangle(currentX,beginY,beginX-currentX,currentY-beginY,color);
}
}else{
if(currentY<beginY){
rectangle=newRectangle(beginX,currentY,currentX-beginX,beginY-currentY,color);
}else{
rectangle=newRectangle(beginX,beginY,currentX-beginX,currentY-beginY,color);
}
}
currentShapes.push(rectangle);
break;
}

repaint();
}

@Override
publicvoidmouseDragged(MouseEvente){
currentX=e.getX();
currentY=e.getY();
this.repaint();
}

@Override
publicvoidmouseMoved(MouseEvente){
}
}
{

=-7080053971741609904L;

=newJPanel();//存放命令的面板
privatefinalJPanelcolorPanel=newJPanel();//存放顏色的面板
privatefinalJPanelmainPanel=newJPanel();//主面板

privatefinalJButtonredButton=newJButton("紅色");
privatefinalJButtonblueButton=newJButton("藍色");
=newJButton("綠色");

privatefinalJButtonlineButton=newJButton("直線");
=newJButton("圓");
=newJButton("矩形");

privatefinalJButtonundoButton=newJButton("撤銷");
privatefinalJButtonredoButton=newJButton("恢復撤銷");
privatefinalJButtonexitButton=newJButton("退出");

SketchpadPanelsketchPanel=newSketchpadPanel();

privatevoidinitFrame(){
commandPanel.setLayout(newFlowLayout());
commandPanel.add(lineButton);
commandPanel.add(circleButton);
commandPanel.add(rectangleButton);
commandPanel.add(undoButton);
commandPanel.add(redoButton);
commandPanel.add(exitButton);

colorPanel.setLayout(newFlowLayout());
colorPanel.add(redButton);
colorPanel.add(blueButton);
colorPanel.add(greenButton);

mainPanel.setLayout(newBorderLayout());
mainPanel.add(commandPanel,BorderLayout.NORTH);
mainPanel.add(colorPanel,BorderLayout.CENTER);

getContentPane().add("South",mainPanel);
getContentPane().add("Center",sketchPanel);

//初始化設置:顏色和命令
lineButton.setForeground(Color.RED);
sketchPanel.setColor(Color.RED);
redButton.setForeground(Color.RED);

sketchPanel.setShapeType(ShapeTypes.LINE);
}

privatevoidinitListener(){
redButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
redAction(e);
}
});
blueButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
blueAction(e);
}
});
greenButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
greenAction(e);
}
});

undoButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
undoAction(e);
}
});
redoButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
redoAction(e);
}
});
exitButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
exitAction(e);
}
});

lineButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
lineAction(e);
}
});
circleButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
circleAction(e);
}
});
rectangleButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
rectangleAction(e);
}
});
}

publicSketchpadFrame(){
initFrame();
initListener();
this.setSize(500,600);
setLocationByPlatform(true);
setResizable(true);
}

/*********************處理事件**********************/
privatevoindoAction(ActionEvente){
sketchPanel.undo();
}

privatevoidredoAction(ActionEvente){
sketchPanel.redo();
}

privatevoidexitAction(ActionEvente){
System.exit(0);
}

privatevoidlineAction(ActionEvente){
//選中按鈕為紅色,其餘為黑色
lineButton.setForeground(Color.RED);
circleButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.LINE);
}

privatevoidcircleAction(ActionEvente){
circleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
rectangleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.CIRCLE);
}

privatevoidrectangleAction(ActionEvente){
rectangleButton.setForeground(Color.RED);
lineButton.setForeground(Color.BLACK);
circleButton.setForeground(Color.BLACK);
sketchPanel.setShapeType(ShapeTypes.RECTANGLE);
}

privatevoidredAction(ActionEvente){
redButton.setForeground(Color.RED);
blueButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.RED);
}

privatevoidblueAction(ActionEvente){
blueButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
greenButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.BLUE);
}

privatevoidgreenAction(ActionEvente){
greenButton.setForeground(Color.RED);
redButton.setForeground(Color.BLACK);
blueButton.setForeground(Color.BLACK);
sketchPanel.setColor(Color.GREEN);
}
}
/**
*
*@author不落的太陽(SeanYang)
*@version1.0
*@sinceJDK1.8
*
*/
publicclassSketchpadMain{

/**
*測試方法
*
*@paramargs命令行參數
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
@Override
publicvoidrun(){
JFrameframe=newSketchpadFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}

㈨ 請問java在繪圖程序的繪圖板上怎麼實現選中已經畫的圖形,移動選中圖形和改變圖形的大小操作

每個圖形都分別花在單獨的畫板上,就像圖層一樣,然後移動畫板就能有移動圖形的效果了。

㈩ 2. 參考Windows附件中的繪圖工具,使用Java語言設計並實現一個簡單的繪圖板。(界面使用A

Java語言設計並實現一個簡單的繪圖板。(界面使用A有的 很多,是個人原創。

閱讀全文

與java繪圖板相關的資料

熱點內容
看輕小說的網站 瀏覽:85
命令與征服3戰役存檔 瀏覽:147
台灣風月影片 瀏覽:326
彭偶么電視劇電影大全 瀏覽:291
重生井岡山林楓 瀏覽:519
日本大片網址大全 瀏覽:741
在線免費觀看完整版電影大全韓國 瀏覽:889
片子視頻破解網站 瀏覽:853
穿越抗戰時期送物資的小說 瀏覽:536
大轉折系列電影17部 瀏覽:86
蠟筆小新功夫小子國語版免費觀看 瀏覽:407
一個囚犯和一個小男孩的外國電影 瀏覽:974
yy寫的很詳細的小說 瀏覽:128
求個在線觀看的中文字幕 瀏覽:436
四台伺服器怎麼改成host 瀏覽:785
馬尾電影院今日影訊 瀏覽:815
老濕機免費看片網站 瀏覽:914
ck影院 瀏覽:942
ubuntuphpfpm安裝 瀏覽:414
末世之一女多男 瀏覽:178