import?java.awt.Color;
創(chuàng)新互聯(lián)長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為振興企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè),振興網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
import?java.awt.Graphics;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
public?class?LineDemo?extends?JFrame?{
public?LineDemo()?{
this.add(new?DrawPanel());//窗口添加畫線的JPanel
this.setTitle("畫線窗口");//窗口標(biāo)題
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關(guān)閉的操作
this.setLocation(100,?200);//窗口位置
this.setSize(300,?200);//窗口大小
this.setVisible(true);//窗口可見
}
public?static?void?main(String[]?args)?{
new?LineDemo();//創(chuàng)建窗口
}
}
class?DrawPanel?extends?JPanel?{//畫線的JPanel
@Override
public?void?paint(Graphics?g)?{
super.paint(g);
//兩點(diǎn)確定一條直線.??三個(gè)點(diǎn)(不在同一直線)兩兩連接就可以成為三角形
g.setColor(Color.RED);//設(shè)置第一條線的顏色
g.drawLine(50,?50,?100,?100);//畫第一條線?點(diǎn)(50,50)?到點(diǎn)??(100,100)
g.setColor(Color.BLUE);
g.drawLine(50,?50,?50,?150);//畫第二條線?點(diǎn)(50,50)?到點(diǎn)??(50,150)
g.setColor(Color.GREEN);
g.drawLine(50,?150,?100,?100);//畫第三條線?點(diǎn)(50,150)?到點(diǎn)??(100,100)
}
}
效果圖
這個(gè)簡單
可以調(diào)用方法drawline(int x1, int y1, int x2, int y2)
其中(x1, y1), (x2, y2)分別為直線起點(diǎn)和終點(diǎn)的坐標(biāo)
特意給你寫了個(gè)小例子,希望能幫到你
***************************************
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);
}
}
****************************
將程序復(fù)制到記事本中并改名為DrawLine.java(注意大小寫)
然后在命令行中用 CD+文件夾 使命令范圍到DrawLine.java的文件夾中
然后執(zhí)行命令javac DrawLine.java
再執(zhí)行命令java DrawLine
你會(huì)看到結(jié)果
我暈,組件要重繪,就使用paintComponent方法吧,沒有什么靈活不靈活的
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class FrameDemo extends JFrame {
private JPanel contentPane;
TestComponent tc1, tc2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameDemo frame = new FrameDemo();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FrameDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(2, 1, 10, 10));
tc1 = new TestComponent();
tc1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
tc2 = new TestComponent();
tc2.setBorder(BorderFactory.createLineBorder(Color.PINK));
contentPane.add(tc1);
contentPane.add(tc2);
}
}
class TestComponent extends JComponent {
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setColor(Color.RED);
g.drawLine(0, 0, this.getWidth(), this.getHeight());
}
}