導航:首頁 > 編程語言 > javaswingcanvas

javaswingcanvas

發布時間:2023-01-29 22:27:04

java SWT中Canvas控制項畫圖問題。

沒有用Canvas,因為不知道你所說的滑動條,具體是指什麼。是不是JScrollPane的橫縱滾動條。

JScrollPane +JPanel

可以實現,下邊是代碼
--------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class LineAppFrame extends JFrame {
public LineAppFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(550, 550);
setLocationRelativeTo(null);
JScrollPane scrollPane = new JScrollPane();
MyCanvas panel = new MyCanvas();
panel.setPreferredSize(new Dimension(panel.width, panel.height));
scrollPane.setViewportView(panel);
getContentPane().add(scrollPane, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new LineAppFrame();
}
}
class MyCanvas extends JPanel {
int width = 0;
int height = 0;
private ImageIcon icon;
public MyCanvas() {
icon = new ImageIcon("D:\\2.jpg");
width = icon.getIconWidth();
height = icon.getIconHeight();
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(icon.getImage(), 0, 0, null);
}
}

㈡ java中如何用畫布繪制矩形圖形

importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
classOvalcanvasextendsCanvas
{
intN=10;
intx[]=newint[N];
inty[]=newint[N];
Ovalcanvas()
{
setSize(300,200);
setBackground(Color.cyan);
}
publicvoidsetOval(int[]x,int[]y,intN)
{
this.N=N;
for(inti=0;i<N;i++)
{
this.x[i]=x[i];
this.y[i]=y[i];
}
}
publicvoidpaint(Graphicsg)
{
g.drawPolygon(x,y,N);
}
}
publicclassExample6_
{
Ovalcanvascanvas;
TextFieldin_N;
Buttonbtn;
Example6_7()
{
super("畫布上繪制多邊形");
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
in_N=newTextField(6);
setLayout(newFlowLayout());
add(newLabel("請輸入變數:"));
add(in_N);
btn=newButton("確定");
btn.addActionListener(this);
add(btn);
canvas=newOvalcanvas();
add(canvas);
validate();
}
publicvoidactionPerformed(ActionEvente)
{
intN=Integer.parseInt(in_N.getText());
intx[]=newint[N];
inty[]=newint[N];
for(inti=0;i<N;i++)
{
x[i]=(int)(Math.random()*200);
y[i]=(int)(Math.random()*200);
}
canvas.setOval(x,y,N);
canvas.repaint();
}
publicstaticvoidmain(String[]args)
{
newExample6_7();
}
}

㈢ java swing組件 如何使子組件正好鋪滿父組件整個可顯示區域

setLayout(null); 這一句去掉就行
JFrame 默認的就是BorderLayout管理器,只要將JPanel 加入到中間位置就行

㈣ java panel 和 canvas 主要區別是什麼啊 swing里沒有canvas

從詞源可以看出 Canvas,麻布->畫布->畫油畫的畫板。 Panel 小塊布->塊版->面板、牆板、地板 Canvas,直接繼承自Component組件,主要用於繪圖,沒有控制項,更原始 Panel,繼承自Container容器,主要用於鑲嵌在其他控制項裡面當面板。

㈤ java中的canvas類有什麼作用

從詞源可以看出
Canvas,麻布->畫布->畫油畫的畫板。
Panel 小塊布->塊版->面板、牆板、地板
Canvas,直接繼承自Component組件,主要用於繪圖,沒有控制項,更原始
Panel,繼承自Container容器,主要用於鑲嵌在其他控制項裡面當面板。

由於Java SE版的Canvas和Panel都繼承自Component,共用Component的paint(Graphics g)
方式繪制自己的內容。由於使用同一個Graphic類,所以那些drawXXX都一致。

繪制的方式一樣,2者的繪制速度就沒大區別,
Canvas更適合畫全屏的、沒有控制項的情形。像手機上JavaME就主要用Canvas
Panel適合嵌入到其他控制項中使用。

㈥ java swing,怎麼在圖片里讓別人添加文字

swing裡面有沒現成的控制項 只能自己寫一個
簡單舉例來說 可以繼承JPanel控制項寫個JDrawPanel
用BufferedImage image來裝載你需要修改的圖片
在JDrawPanel中重寫printComponents(Graphics g)方法,將image draw到背景上
在panel上加mouse事件 當點擊時彈出對話框 讓用戶輸入文字 然後調用image的 image.createGraphics().drawString(String s)方法 把文字加上去
再調用panel的repaint()方法 把文字顯示出來

還有Canvas組件還有一些別的組件可以使用 但是都需要自己加功能

㈦ JAVA swing編程 畫圖問題 paint()方法

super.paint()的作用是把當前的區域清空,每次resize的時候就會自動調用paint()方法,paint()方法里先調用了super.paint()清空當前區域,再畫一個矩型筐,當然每次只有一個了。 另外你的演算法也有漏洞,當你想從右上角拉到左下角畫矩形的時候是沒有反應的。 下面這個程序修改了只畫一個的錯誤,改進了右上角拉到左下角的漏洞,還增加了拖動的中間過程。沒時間給你寫注釋,自己看吧。 import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.*; public class Hello extends JFrame implements MouseListener,MouseMotionListener{ int x一,y一; Vector rectangles=null; public Hello(){ this.setTitle("畫圖小程序"); this.addMouseListener(this); this.addMouseMotionListener(this); this.setSize(四00,四00); rectangles=new Vector(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g){ super.paint(g); g.setColor(Color.BLUE); for(int i=0;i<rectangles.size();i++){ Rectangle rec=rectangles.get(i); int tmp_x=rec.x; int tmp_y=rec.y; int tmp_w=rec.width; int tmp_h=rec.height; g.drawRect(tmp_x,tmp_y,tmp_w,tmp_h); } } public static void main(String[] args) { new Hello(); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { x一=e.getX();y一=e.getY(); } public void mouseReleased(MouseEvent e) { int x二=e.getX(); int y二=e.getY(); int x=0; int y=0; Graphics g=this.getGraphics(); g.setColor(Color.BLUE); int tmp_w=0; int tmp_h=0; if(x一x二){ x=x二; tmp_w=x一-x二; } if(y一y二){ y=y二; tmp_h=y一-y二; } rectangles.add(new Rectangle(x,y,tmp_w,tmp_h)); this.repaint(); } public void mouseDragged(MouseEvent e) { int x二=e.getX(); int y二=e.getY(); int x=0; int y=0; Graphics g=this.getGraphics(); g.setColor(Color.BLUE); int tmp_w=0; int tmp_h=0; if(x一x二){ x=x二; tmp_w=x一-x二; } if(y一y二){ y=y二; tmp_h=y一-y二; } paint(g); g.drawRect(x,y,tmp_w,tmp_h); } public void mouseMoved(MouseEvent e) { }

閱讀全文

與javaswingcanvas相關的資料

熱點內容
word2013更改圖片 瀏覽:980
win10plsql 瀏覽:819
香港電影開頭一個女的在床上自慰 瀏覽:512
win10cdromsys下載 瀏覽:30
桌面程序hibernate 瀏覽:14
如何建蔬菜網站 瀏覽:579
android網路通信聊天 瀏覽:1
電影頭上裹著布還有紐扣 瀏覽:246
iphone6nfc充電 瀏覽:422
鐵銹戰爭的文件夾是哪個 瀏覽:184
大數據業務描述 瀏覽:162
古惑仔粵語版歌詞 瀏覽:897
韓國劇情片網站 瀏覽:759
自學滅火器編程該如何入手 瀏覽:817
網站ip地址怎麼防禦 瀏覽:572
大數據自動化部署 瀏覽:368
自動編程軟體有哪些有什麼特色 瀏覽:140
韓國污片網站 瀏覽:758
主角要收集各種女子 瀏覽:463

友情鏈接