Boshqa belgi
Belgilangan belgi yaratish uchun Swing painting API-dan ham foydalanish
mumkin. Grafik kontekst paintIcon()usulga o'tkaziladi.
CustomIconEx.java
package com.zetcode;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
class MissingIcon implements Icon {
private final int WIDTH = 32;
private final int HEIGHT = 32;
private final BasicStroke stroke = new BasicStroke(5);
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
doDrawing(g, x, y);
}
public void doDrawing(Graphics g, int x, int y) {
Dasturlash II fanidan 3-ma’ruza
14
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.white);
g2d.fillRect(x + 1, y + 1, WIDTH - 2, HEIGHT - 2);
g2d.setColor(Color.darkGray);
g2d.drawRect(x + 1, y + 1, WIDTH - 2, HEIGHT - 2);
g2d.setColor(Color.red);
g2d.setStroke(stroke);
g2d.drawLine(x + 10, y + 10, x + WIDTH - 10, y + HEIGHT - 10);
g2d.drawLine(x + 10, y + HEIGHT - 10, x + WIDTH - 10, y + 10);
g2d.dispose();
}
@Override
public int getIconWidth() {
return WIDTH;
}
@Override
public int getIconHeight() {
return HEIGHT;
}
}
class MyLabel extends JLabel {
public MyLabel(Icon icon) {
super(icon);
}
@Override
public boolean isOpaque() {
return true;
}
}
public class CustomIconEx extends JFrame {
public CustomIconEx() {
Dasturlash II fanidan 3-ma’ruza
15
initUI();
}
private void initUI() {
JLabel lbl = new MyLabel(new MissingIcon());
lbl.setBackground(Color.gray);
add(lbl);
setSize(250, 150);
setTitle("Custom icon");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
CustomIconEx ex = new CustomIconEx();
ex.setVisible(true);
});
}
}
Misol yo'qolgan maxsus belgini yaratadi va uni oynada ko'rsatadi JLabel.
class MissingIcon implements Icon {
Maxsus belgi yaratish uchun biz Iconinterfeysni amalga oshiramiz .
@Override
public int getIconWidth() {
return WIDTH;
}
@Override
public int getIconHeight() {
return HEIGHT;
}
Belgining o'lchamini aniqlaydigan usul getIconWidth() va getIconHeight()
usullarni bekor qilamiz.
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
doDrawing(g, x, y);
}
Biz paintIcon()ikonka bo'yalgan usulni bekor qilamiz . GraphicsOb'ekt 2D
shakllar chizish va programmaning grafik muhit haqida ma'lumot olish usullari bir
Dasturlash II fanidan 3-ma’ruza
16
qator fosh.
public void doDrawing(Graphics g, int x, int y) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.white);
g2d.fillRect(x + 1, y + 1, WIDTH - 2, HEIGHT - 2);
g2d.setColor(Color.darkGray);
g2d.drawRect(x + 1, y + 1, WIDTH - 2, HEIGHT - 2);
g2d.setColor(Color.red);
g2d.setStroke(stroke);
g2d.drawLine(x + 10, y + 10, x + WIDTH - 10, y + HEIGHT - 10);
g2d.drawLine(x + 10, y + HEIGHT - 10, x + WIDTH - 10, y + 10);
g2d.dispose();
}
doDrawing()Usul ichida biz belgini chizamiz. Jarayon paintComponent()usul
ichidagi rasm bilan bir xil. Graphics2D Sinf uzaytiradi Graphics, geometriya ustidan
yana murakkab nazorat taqdim o'zgarishlarni, rang boshqarish va matn tartibi
muvofiqlashtirish uchun sinfini.
class MyLabel extends JLabel {
public MyLabel(Icon icon) {
super(icon);
}
@Override
public boolean isOpaque() {
return true;
}
}
Bizda maxsus MyLabelkomponent mavjud. Biz uni noaniq qilamiz, ya'ni
yorliq fonga ega.
JLabel lbl = new MyLabel(new MissingIcon());
Belgi yorliq komponentasiga o'rnatildi.
Dasturlash II fanidan 3-ma’ruza
17
Shakl: moslashtirilgan belgi yo'q
Do'stlaringiz bilan baham: |