本文目录一览:
用java画板drawString的字,可以改变大小吗
可以,用Graphics类的void setFont(Font font)方法来,设定字体的名称,大小,是否加粗斜体等.
例如 Font f=new Font(null,Font.PLAIN,20);用这个语句定义一个缺省字体名称大小20磅的普通字体对象f,然后用g.setFont(f);为图形对象Graphics g设定字体大小,画板drawString的字体大小就会改变了.
java画板
//有问题的话,给我发百度消息
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**功能介绍
* 1.改变线条长度时鼠标形状的改变(鼠标位于线条两端时,四种鼠标状态)
* 2.移动线条时鼠标形状的改变(鼠标位于线条中间)
* 3.删除时鼠标形状的改变
* 4.最后一条直线颜色的不同
* 5.最大化、最小化、窗口大小改变时,线条的重绘
*
*/
public class LineDraw extends JFrame{
/**
*我需要在一个背景下能在任意位置添加线条,
能通过鼠标左键拉长,并且如果当鼠标点到直线中央时能拖动线条,当用鼠标右键点击直线时直线能消失。
*/
private LineDrawPanel drawPanel=new LineDrawPanel();
public static void main(String[] args) {
new LineDraw("画图面板");
}
public LineDraw(String title) {
super(title);
setBounds(300, 100, 500, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(drawPanel);
validate();
}
}
class LineDrawPanel extends JPanel{
private int x1;
private int y1;
private Line lastLine;//最后一条线,也是当前正在画的线,注意线的颜色
private boolean isDrawing;//是否正在画的标记
private Line moveLine;//正在移动的线条
private Line changeLine;//正在改变长度的线条
private ListLine list=new LinkedListLine();
public LineDrawPanel() {
init();
}
public void init() {
MouseAdapter adapter=new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON1){
x1 = e.getX();
y1 = e.getY();
moveLine=getMoveLine(x1, y1);
if(moveLine!=null){
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
setToolTipText("移动");
return;
}
changeLine=getChangeLine(x1, y1);
if(changeLine!=null){
return;
}
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setToolTipText("画图");
isDrawing=true;
}
}
@Override
public void mouseMoved(MouseEvent e) {
Line l=getChangeLine(e.getX(), e.getY());
if(l!=null){//设置四种鼠标形状
setToolTipText("变长");
if(Math.abs(l.x1-l.x2)4){
setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
}
else if(Math.abs(l.y1-l.y2)4){
setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
}
else{
double k=(l.y1-l.y2)*1.0/(l.x1-l.x2);
if(k=0) setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
else setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
}
}else{
l=getMoveLine(e.getX(), e.getY());
if(l!=null) {
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
setToolTipText("移动");
return;
}
l=getDelLine(e.getX(), e.getY());
if(l!=null){
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
setToolTipText("右键删除");
}else {
setCursor(Cursor.getDefaultCursor());
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
if(moveLine!=null){
moveLine.move(e.getX()-x1, e.getY()-y1);
x1 = e.getX();
y1 = e.getY();
repaint();
return;
}
if(changeLine!=null){
int xVal=e.getX()-x1;
int yVal=e.getY()-y1;
if(Math.abs(xVal)5||Math.abs(yVal)5)
return;
changeLine.changeLength(xVal,yVal );
x1 = e.getX();
y1 = e.getY();
repaint();
return;
}
if(isDrawing){
int xVal=e.getX()-x1;
int yVal=e.getY()-y1;
if(Math.abs(xVal)3||Math.abs(yVal)3)
return;
lastLine=new Line(x1, y1, e.getX(), e.getY());
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON1){
if(moveLine!=null){//移动
moveLine=null;
repaint();
return;
}
if(changeLine!=null){//改变长度
return;
}
if(isDrawing){//画线条
isDrawing=false;//重置
lastLine=null;
setCursor(Cursor.getDefaultCursor());
int xVal=e.getX()-x1;
int yVal=e.getY()-y1;
if(Math.abs(xVal)3||Math.abs(yVal)3)
return;
Line l=new Line(x1, y1, e.getX(), e.getY());
list.add(l);
}
}else if(e.getButton()==MouseEvent.BUTTON3){//鼠标右键
Line delLine=getDelLine(e.getX(), e.getY());//该删除的线
if(delLine!=null){
list.remove(delLine);
int size=list.size();
lastLine=(size0?list.get(size-1):null);
repaint();
}
}
}
};
addMouseListener(adapter);//添加鼠标动作事件
addMouseMotionListener(adapter);//添加鼠标移动事件
}
public void paint(Graphics g) {
super.paint(g);//调用父类的方法
g.setColor(Color.BLUE);
for (Line l:list) {
l.drawLine(g);
}
g.setColor(Color.red);//画最后一个线条
if(lastLine!=null){
lastLine.drawLine(g);
}else {
if(list.size()0) list.get(list.size()-1).drawLine(g);
}
}
public Line getChangeLine(int x,int y){//获取能够改变的直线
for (Line l:list) {
if(l.isCanChange(x,y))
return l;
}
return null;
}
public Line getDelLine(int x,int y){//获取能够删除的直线
for (Line l:list) {
if(l.isCanDel(x,y))
return l;
}
return null;
}
public Line getMoveLine(int x,int y){//获取能够移动的直线
for (Line l:list) {
if(l.isCanMove(x, y))
return l;
}
return null;
}
}
class Line{
public int x1;
public int y1;
public int x2;
public int y2;
public static boolean isStart;//决定改变的线条的那一端
public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void drawLine(Graphics g){
g.drawLine(x1, y1, x2, y2);
}
public void move(int xVal,int yVal){//线条移动
x1+=xVal;
x2+=xVal;
y1+=yVal;
y2+=yVal;
}
public void changeLength(int xVal,int yVal){//改变长度 (该算法有点问题)
if(x1==x2){
xVal=0;
}else {
double k=(y1-y2)*1.0/(x1-x2);
System.out.println(k);
yVal=(int) Math.round(k*xVal);
}
if(isStart){
x1+=xVal;
y1+=yVal;
}else {
x2+=xVal;
y2+=yVal;
}
//x1=(x10?0:x1);
//y1=(y10?0:y1);
}
public boolean isLineValue(){//判断所画的线条是否有效
int len2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
if(len2==0) return false;
if(Math.sqrt(len2)=1)//长度小于1的,视为无效线条
return false;
return true;
}
public boolean isCanDel(int x,int y){//线条是否可被删除
int sideA2=(x-x1)*(x-x1)+(y-y1)*(y-y1);
int sideB2=(x-x2)*(x-x2)+(y-y2)*(y-y2);
int sideC2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
if(sideA2==0||sideB2==0)return true;
double cosC=(sideA2+sideB2-sideC2)/(2*Math.sqrt(sideA2*sideB2));
if(cosC-0.99) return true;
return false;
}
public boolean isCanChange(int x,int y){//线条是否可改变长度,仅鼠标位于线条两端时才可改变
if(Math.abs(x1-x)2Math.abs(y1-y)2){
isStart=true;
return true;
}
if(Math.abs(x2-x)2Math.abs(y2-y)2){
isStart=false;
return true;
}
return false;
}
public boolean isCanMove(int x,int y){//线条是否可移动
int midX=(x1+x2)/2;
int midY=(y1+y2)/2;
//if(midX==xmidY==y) return false;
if(Math.abs(midX-x)5Math.abs(midY-y)5)
return true;
return false;
}
}
java中的canvas类有什么作用?
从词源可以看出\x0d\x0aCanvas,麻布-画布-画油画的画板。\x0d\x0aPanel 小块布-块版-面板、墙板、地板\x0d\x0aCanvas,直接继承自Component组件,主要用于绘图,没有控件,更原始\x0d\x0aPanel,继承自Container容器,主要用于镶嵌在其他控件里面当面板。\x0d\x0a\x0d\x0a由于Java SE版的Canvas和Panel都继承自Component,共用Component的paint(Graphics g)\x0d\x0a方式绘制自己的内容。由于使用同一个Graphic类,所以那些drawXXX都一致。\x0d\x0a\x0d\x0a绘制的方式一样,2者的绘制速度就没大区别,\x0d\x0aCanvas更适合画全屏的、没有控件的情形。像手机上JavaME就主要用Canvas\x0d\x0aPanel适合嵌入到其他控件中使用。
java 涂鸦画板
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseDrawPanel extends Frame
{
private static final long serialVersionUID = 1L;
MouseEvent e = null;
int startX = -1;
int startY = -1;
int endX = -1;
int endY = -1;
boolean left = false;
public MouseDrawPanel ( String title )
{
setTitle (title);
setLayout (new BorderLayout ());
setResizable (false);
setSize (500, 400);
setLocationRelativeTo (null);
addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
});
}
@Override
public void paintComponents ( Graphics g )
{
float lineWidth = 8.0f;
( (Graphics2D) g ).setStroke (new BasicStroke (lineWidth));
g.setColor (Color.YELLOW);
g.drawLine (startX, startY, endX, endY);
g.dispose ();
}
public static void main ( String[] args )
{
final MouseDrawPanel mdp = new MouseDrawPanel ("Use Mouse Draw");
Panel panel = new Panel ();
panel.setLayout (new FlowLayout (FlowLayout.LEFT));
Label startL = new Label ("start: ");
Label endL = new Label ("end: ");
final Label nowL = new Label ("now: ");
final Label startR = new Label ("000,000");
final Label endR = new Label ("000,000");
final Label nowN = new Label ("000,000");
panel.add (startL);
panel.add (startR);
panel.add (endL);
panel.add (endR);
panel.add (nowL);
panel.add (nowN);
mdp.add (panel, "South");
mdp.addMouseMotionListener (new MouseMotionListener ()
{
@Override
public void mouseMoved ( MouseEvent e )
{
if (mdp.left)
{
nowN.setText (e.getX () + " , " + e.getY ());
}
}
@Override
public void mouseDragged ( MouseEvent e )
{
if (mdp.left)
{
mdp.endX = e.getX ();
mdp.endY = e.getY ();
mdp.paintComponents (mdp.getGraphics ());
mdp.startX = mdp.endX;
mdp.startY = mdp.endY;
endR.setText (mdp.endX + " , " + mdp.endY);
}
}
});
mdp.addMouseListener (new MouseAdapter ()
{
@Override
public void mousePressed ( MouseEvent e )
{
if (e.getButton () == MouseEvent.BUTTON1)
{
mdp.startX = e.getX ();
mdp.startY = e.getY ();
startR.setText (mdp.startX + " , " + mdp.startY);
mdp.left = true;
}
else
{
mdp.left = false;
}
}
@Override
public void mouseReleased ( MouseEvent e )
{
if (mdp.left)
{
endR.setText (e.getX () + " , " + e.getY ());
}
}
});
mdp.setVisible (true);
}
}
JAVA 第一次绘制画板的时候画板会闪烁
可能是你加载canvas的时候闪的
可以新建一个BufferedImage img 和Graphics2D g2d=img.createGraphics;
用g2d绘图,例如g2d.drawRect(0,0,100,100);
然后直接在jpanel上利用public void paintComponent(Graphics g)函数 g.drawImage(img,0,0,width,height,this)画出来, 可以解决闪烁问题。canvas属于比较重的组件 不推荐使用