Testoni aftësitë tuaja në Java - Seria IV,,en,Aftësitë e Kodimit Java,,en,Testoni njohuritë tuaja,,en,Kjo është një tjetër seri e këshillave Java,,en,Kemi përqendruar kryesisht në pjesën e kodimit dhe implementimit,,en,Ne kemi diskutuar implementime të ndryshme Java të zakonshme me shembuj kodimi,,en,Ju mund ta provoni direkt duke krijuar kodin tuaj Java,,en,Shpresoj se kjo do t'ju ndihmojë të kuptoni gjërat themelore në Java kryesore,,en,Si të konvertohet një shprehje infix në shprehjen postfix,,en,Shembulli i mëposhtëm tregon se si të konvertohet një infix në shprehjen e postfix duke përdorur konceptin e pirg,,en,Këtu shtyni,,en,pop,,en,dhe shikoni,,en,metodat janë përdorur për të kryer detyrën,,en,Dhe klasa Stack përdoret për të mbajtur shprehjet,,en,klasë publike InToPost,,en,private Stack theStack,,en,input privat String,,en,prodhimi privat String =,,en,public InToPost,,en,String në,,en,input = in,,en,int stackSize = input.length,,en

Java Coding Skills

Java Coding Skills – Test your knowledge

Parathënie: This is another Java tips series. We have mainly focused on the coding and implementation part. We have discussed different common Java implementations with coding examples. You can test it directly by creating your own Java code. Hope this will help you understand the basic things in core Java.







How to convert an infix expression to postfix expression?

Një: Following example demonstrates how to convert an infix to postfix expression by using the concept of stack. Here push (), pop () and peek () methods are used to perform the task. And the Stack class is used to hold the expressions.

java.io.IOException importit;

public class InToPost {

private Stack theStack;

private String input;

private String output = “”;

public InToPost(String in) {

input = in;

int stackSize = input.length();

theStack = Stack i ri,,en,stackSize,,en,publik String doTrans,,en,input.length,,en,char ch = input.charAt,,en,rasti ' ',,en,rast '-',,en,gotOper,,en,rast '*',,en,rast '/',,en,rast ',,en,theStack.push,,en,gotParen,,eu,output = dalje,,en,theStack.isEmpty,,en,theStack.pop,,en,kthimin e prodhimit,,en,publike void gotOper,,en,char opKjo,,en,int preci1,,en,char opTop = thesar.pop,,en,onTop == ',,nl,në krye,,nl,int preci2,,en,atTop == ' ,,nl,atTop == '-',,nl,prec2 =,,en,prec2,,en,prec1,,en,opThis,,en,public void gotParen,,en,char chx = theStack.pop,,en,chx == ',,en,chx,,en,Input String =,,en,Prodhimi String,,en,InToPost theTrans = InToPost i ri,,en,output = theTrans.doTrans,,en,Postfix është,,en,klasa Stack,,en,private int maxSize,,en,private char,,en,Stack array,,sv,privat privat,,en,public Stack,,en,int max,,en,maxSize = max,,en,stackArray = char i ri,,en,MAXSIZE,,en,lart =,,en,shtytje publike e pavlefshme,,en,char j,,en,më i lartë,,en,= j,,en,publik pop,,en,kthimi i stackArray,,en,shikime publike,,en,public boolean isEmpty,,en(stackSize);

}

public String doTrans() {

për (int j = 0; j < input.length(); j ) {

char ch = input.charAt(j);

switch (ch) {

case ‘ ’:

case ‘-‘:

gotOper(ch, 1);

break;

case ‘*’:

case ‘/’:

gotOper(ch, 2);

break;

case ‘(&#8216;:

theStack.push(ch);

break;

case ‘)’:

gotParen(ch);

break;

default:

output = output + ch;

break;

}

}

derisa (!theStack.isEmpty()) {

output = output + theStack.pop();

}

Println(output);

return output;

}

public void gotOper(char opThis, int prec1) {

derisa (!theStack.isEmpty()) {

char opTop = theStack.pop();

nëse (opTop == ‘(&#8216;) {

theStack.push(opTop);

break;

}

tjetër {

int prec2;

nëse (opTop == ‘ ’ || opTop == ‘-‘)

prec2 = 1;

tjetër

prec2 = 2;

nëse (prec2 < prec1)

{

theStack.push(opTop);

break;

} tjetër

output = output + opTop;

}

}

theStack.push(opThis);

}

public void gotParen(char ch){

derisa (!theStack.isEmpty()) {

char chx = theStack.pop();

nëse (chx == ‘(&#8216;)

break;

tjetër

output = output + chx;

}

}

publike statike kryesore void(Varg[] args) hedh IOException {

String input = “1+2*4/5-7+3/6”;

String output;

InToPost theTrans = new InToPost(input);

output = theTrans.doTrans();

Println(“Postfix is ” + output + ‘n’);

}

class Stack {

private int maxSize;

private char[] stackArray;

private int top;

public Stack(int max) {

maxSize = max;

stackArray = new char[maxSize];

top = -1;

}

public void push(char j) {

stackArray[++top] = j;

}

public char pop() {

return stackArray[top–];

}

public char peek() {

return stackArray[top];

}

public boolean isEmpty() {

return (krye ==,,en,Shembulli i mësipërm i kodit do të prodhojë rezultatin vijues,,en,Si të zbatohet rradha,,en,Shembulli i mëposhtëm tregon se si të implementohet një radhë në një strukturë punonjësish,,en,Lista e lidhur është një implementim i ndërfaqes Lista,,en,Zbaton të gjitha operacionet e listës zgjedhore,,en,dhe lejon të gjitha elementet,,en,duke përfshirë null,,en,Përveç zbatimit të ndërfaqes së Listës. Klasa LinkedList siguron metoda të njëjta me emër për të marrë,,en,hiqni dhe futni një element në fillim dhe në fund të listës,,en,Këto operacione lejojnë që listat e lidhura të përdoren si një pirg,,en,radhe,,en,ose radhë të dyfishtë,,en,import java.util.LinkedList,,en,klasa GenQueue,,en,listë private LinkedList = LinkedList i ri,,en,enqueue publike pavlefshme,,en,E artikull,,en,list.addLast,,en,publike E dequeue,,en,lista e kthimit.poll,,en,hasItems boolean publike,,en,list.isEmpty,,en,madhësia publike int,,en,kthehuni list.size,,en,shtesa të pavlefshme publike,,en,GenQueue q,,en,q.hasItems,,en,q.dequeue,,en -1);

}

}

}

The above code sample will produce the following result.

124*5/+7-36/+

Postfix is 124*5/+7-36/+

How to implement Queue?

Një: Following example shows how to implement a queue in an employee structure. Linked list is an implementation of the List interface. Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface.The LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue.

import java.util.LinkedList;

class GenQueue {

private LinkedList list = new LinkedList();

public void enqueue(E item) {

list.addLast(artikull);

}

public E dequeue() {

return list.poll();

}

public boolean hasItems() {

return !list.isEmpty();

}

public int size() {

return list.size();

}

public void addItems(GenQueue q) {

derisa (q.hasItems())

list.addLast(q.dequeue());

}

}

klasë publike GenQueueTest,,en,GenQueue empList,,fr,empList = GenQueue i ri,,fr,GenQueue hList,,en,hList = GenQueue i ri,,en,hList.enqueue,,en,punonjës i ri në orë,,en,empList.addItems,,en,hList,,en,Të punësuarit,,en,emrat janë,,en,empList.hasItems,,fr,Punonjësi emp = empList.dequeue,,fr,emp.firstName,,en,emp.lastName,,en,`public String lastName,,en,public Emri i parë i stringit,,en,String i fundit,,en,String fillimisht,,en,this.lastName = e fundit,,en,this.firstName = e para,,en,public String toString,,en,kthej emrin e parë,,en,klasa Punonjësi i orëve zgjat Punonjësin,,en,publikisht dy orë në javë,,en,publik,,en,i fundit,,en,T D,,en,G B,,en,F S,,en,Si të ndryshoni një varg duke përdorur pirg,,en,Shembulli i mëposhtëm tregon se si të ndryshoni një varg duke përdorur pirg me ndihmën e metodës së përcaktuar nga përdoruesi StringReverserThroughStack,,en,Një Stack thjeshtë në Java,,en,zbatuar si një objekt-orientuar,,en,struktura rekurzuese e të dhënave,,en,një listë të vetme të lidhur,,en,Vini re se Java tashmë ofron një klasë Stack,,en,klasa publike StringReverserThroughStack,,en {

publike statike kryesore void(Varg[] args) {

GenQueue empList;

empList = new GenQueue();

GenQueue hList;

hList = new GenQueue();

hList.enqueue(new HourlyEmployee(“T”, “D”));

hList.enqueue(new HourlyEmployee(“G”, “B”));

hList.enqueue(new HourlyEmployee(“F”, “S”));

empList.addItems(hList);

Println(“The employees’ names are:”);

derisa (empList.hasItems()) {

Employee emp = empList.dequeue();

Println(emp.firstName + ” ” + emp.lastName);

}

}

}

class Employee {

` public String lastName;

public String firstName;

punonjës publik() {

}

punonjës publik(String last, String first) {

this.lastName = last;

this.firstName = first;

}

public String toString() {

return firstName + ” ” + lastName;

}

}

class HourlyEmployee extends Employee {

public double hourlyRate;

public HourlyEmployee(String last, String first) {

super(last, first);

}

}

The above code sample will produce the following result.

The employees’ names are:

T D

G B

F S

How to reverse a string using stack?

Një: Following example shows how to reverse a string using stack with the help of user defined method StringReverserThroughStack ().A simple Stack in Java, implemented as an object-oriented, recursive data structure, a singly linked list. Note that Java already provides a Stack class

java.io.IOException importit;

public class StringReverserThroughStack {

private String input;

prodhimit privat String,,en,publik StringReverserThroughStack,,en,publike String doRev,,en,Stack theStack = Stack ri,,en,output =,,en,char ch = theStack.pop,,en,Java Burimi dhe Mbështetja,,en,StringReverserThroughStack theReverser =,,en,i ri StringReverserThroughStack,,en,output = theReverser.doRev,,en,përmbyset,,en,JavaStringReversal,,nl,lasreveRgnirtSavaJ,,no,Si të kontrolloni nëse antialiasing është aktivizuar apo jo,,en,Shembulli i mëposhtëm tregon se si të kontrolloni nëse antialiasing është aktivizuar ose nuk përdor klasën RenderingHints,,en,Aliasing ndodh kur një sinjal,,en,një sinjal grafik 2D,,en,është kampionuar dhe quantizuar nga një hapësirë ​​e vazhdueshme në një hapësirë ​​të diskretizuar,,en,Marrja e mostrave është procesi i leximit të një vlere nga një sinjal që ndryshon vazhdimisht,,en,Quantization është procesi me të cilin këto vlera të vazhdueshme të mostruara u caktohen një vlerë diskrete në hapësirën e fundme të përfaqësuar nga dixhital,,en,binary me bazë,,en;

public StringReverserThroughStack(String in) {

input = in;

}

public String doRev() {

int stackSize = input.length();

Stack theStack = new Stack(stackSize);

për (int i = 0; unë < input.length(); i ) {

char ch = input.charAt(unë);

theStack.push(ch);

}

output = “”;

derisa (!theStack.isEmpty()) {

char ch = theStack.pop();

output = output + ch;

}

return output;

}

publike statike kryesore void(Varg[] args) hedh IOException {

String input = “Java Source and Support”;

String output;

StringReverserThroughStack theReverser =

new StringReverserThroughStack(input);

output = theReverser.doRev();

Println(“Reversed: ” + output);

}

class Stack {

private int maxSize;

private char[] stackArray;

private int top;

public Stack(int max) {

maxSize = max;

stackArray = new char[maxSize];

top = -1;

}

public void push(char j) {

stackArray[++top] = j;

}

public char pop() {

return stackArray[top–];

}

public char peek() {

return stackArray[top];

}

public boolean isEmpty() {

return (krye ==,,en,Shembulli i mësipërm i kodit do të prodhojë rezultatin vijues,,en,Si të zbatohet rradha,,en,Shembulli i mëposhtëm tregon se si të implementohet një radhë në një strukturë punonjësish,,en,Lista e lidhur është një implementim i ndërfaqes Lista,,en,Zbaton të gjitha operacionet e listës zgjedhore,,en,dhe lejon të gjitha elementet,,en,duke përfshirë null,,en,Përveç zbatimit të ndërfaqes së Listës. Klasa LinkedList siguron metoda të njëjta me emër për të marrë,,en,hiqni dhe futni një element në fillim dhe në fund të listës,,en,Këto operacione lejojnë që listat e lidhura të përdoren si një pirg,,en,radhe,,en,ose radhë të dyfishtë,,en,import java.util.LinkedList,,en,klasa GenQueue,,en,listë private LinkedList = LinkedList i ri,,en,enqueue publike pavlefshme,,en,E artikull,,en,list.addLast,,en,publike E dequeue,,en,lista e kthimit.poll,,en,hasItems boolean publike,,en,list.isEmpty,,en,madhësia publike int,,en,kthehuni list.size,,en,shtesa të pavlefshme publike,,en,GenQueue q,,en,q.hasItems,,en,q.dequeue,,en -1);

}

 

}

}

The above code sample will produce the following result.

JavaStringReversal

Reversed:lasreveRgnirtSavaJ







How to check whether antialiasing is enabled or not?

Një: Following example demonstrates how to check if antialiasing is turned on or not using RenderingHints Class. Aliasing occurs when a signal (in this case, a 2D graphics signal) is sampled and quantized from a continuous space into a discretized space. Sampling is the process of reading a value from a continuously varying signal. Quantization is the process by which these continuous sampled values are assigned a discrete value in the finite space represented by digital (binary-based) systems.

import java.awt.Graphics,,en,importi java.awt.Graphics2D,,en,import java.awt.RenderingHints,,en,import javax.swing.JComponent,,en,JFrame frame = JFrame e re,,en,frame.add,,en,ri MyComponent,,en,frame.setSize,,en,frame.setVisible,,en,class MyComponent shtrihet JComponent,,en,Graphics2D g2 =,,en,Graphics2D,,en,RenderingHints rh = g2d.getRenderingHints,,de,boolean bl = rh.containsValue,,en,RenderingHints.VALUE_ANTIALIAS_ON,,en,me,,mt,g2.setRenderingHint,,ku,RenderingHints,,en,KEY_ANTIALIASING,,en,I rremë,,en,Si të shfaqni ngjyrat në një kornizë,,en,Shembulli i mëposhtëm tregon se si të shfaqni të gjitha ngjyrat në një kornizë duke përdorur metodën setRGB të klasës së imazhit,,en,Imazhi abstrakt i klasës është superklasë e të gjitha klasave që përfaqësojnë imazhe grafike,,en,Imazhi duhet të merret në një mënyrë specifike për platformën,,en,import java.awt.event.WindowAdapter,,en,import java.awt.event.WindowEvent,,en,import java.awt.image.BufferedImage,,en,klasa publike kryesore shtrihet JComponent,,en,Imazhi BufferedImage,,en;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import javax.swing.JComponent;

import javax.swing.JFrame;

publik klasë Main {

publike statike kryesore void(Varg[] args) {

JFrame frame = new JFrame();

frame.add(new MyComponent());

frame.setSize(300, 300);

frame.setVisible(i vërtetë);

}

}

class MyComponent extends JComponent {

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

RenderingHints rh = g2d.getRenderingHints();

boolean bl = rh.containsValue

(RenderingHints.VALUE_ANTIALIAS_ON);

Println(bl);

g2.setRenderingHint(RenderingHints.

KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

}

}

The above code sample will produce the following result.

False

False

False

How to display colors in a frame?

Një: Following example displays how to a display all the colors in a frame using setRGB method of image class. The abstract class Image is the superclass of all classes that represent graphical images. The image must be obtained in a platform-specific manner.

import java.awt.Graphics,,en,importi java.awt.Graphics2D,,en,import java.awt.RenderingHints,,en,import javax.swing.JComponent,,en,JFrame frame = JFrame e re,,en,frame.add,,en,ri MyComponent,,en,frame.setSize,,en,frame.setVisible,,en,class MyComponent shtrihet JComponent,,en,Graphics2D g2 =,,en,Graphics2D,,en,RenderingHints rh = g2d.getRenderingHints,,de,boolean bl = rh.containsValue,,en,RenderingHints.VALUE_ANTIALIAS_ON,,en,me,,mt,g2.setRenderingHint,,ku,RenderingHints,,en,KEY_ANTIALIASING,,en,I rremë,,en,Si të shfaqni ngjyrat në një kornizë,,en,Shembulli i mëposhtëm tregon se si të shfaqni të gjitha ngjyrat në një kornizë duke përdorur metodën setRGB të klasës së imazhit,,en,Imazhi abstrakt i klasës është superklasë e të gjitha klasave që përfaqësojnë imazhe grafike,,en,Imazhi duhet të merret në një mënyrë specifike për platformën,,en,import java.awt.event.WindowAdapter,,en,import java.awt.event.WindowEvent,,en,import java.awt.image.BufferedImage,,en,klasa publike kryesore shtrihet JComponent,,en,Imazhi BufferedImage,,en;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import javax.swing.JFrame;

public class Main extends JComponent {

BufferedImage image;

public void initialize() {

int width = getSize,,en,int height = getSize,,en,data = int i ri,,en,int index =,,en,int red =,,en,int gjelbër =,,en,int blu =,,en,indeksi i ,,en,blu,,en,image = new BufferedImage,,en,BufferedImage.TYPE_INT_RGB,,en,image.setRGB,,en,image == null,,en,inicializoj,,en,g.drawImage,,en,imazh,,en,JFrame f = JFrame e re,,en,Ngjyrat e ekranit,,en,f.getContentPane,,en,Kryesore e re,,en,f.setSize,,en,f.setLocation,,en,f.addWindowListener,,en,i ri WindowAdapter,,en,dritare publike e zbrazët Mbyllja,,en,WindowEvent e,,en,f.setVisible,,en,Shfaq të gjitha ngjyrat në një kornizë,,en,Çfarë është threading në java,,en,Një program Java mund të përmbajë shumë tema,,en,të cilat mund të krijohen pa njohuri eksplicite të zhvilluesit,,en,të gjithë ju duhet të konsideroni është se kur shkruani një aplikacion Java,,en,ka një fill fillestar që fillon punën e tij duke ekzekutuar kryesore,,en,metodën e aplikimit tuaj,,en,Kur shkruani një applet Java,,en().width;

int height = getSize().height;

int[] data = new int[width * height];

int index = 0;

për (int i = 0; unë < height; i ) {

int red = (unë * 255) / (height – 1);

për (int j = 0; j < width; j ) {

int green = (j * 255) / (width – 1);

int blue = 128;

data[index ] = (i kuq < < 16) | (e gjelbër < < 8) | blue;

}

}

image = new BufferedImage

(width, height, BufferedImage.TYPE_INT_RGB);

image.setRGB(0, 0, width, height, data, 0, width);

}

public void paint(Graphics g) {

nëse (image == null)

initialize();

g.drawImage(image, 0, 0, this);

}

publike statike kryesore void(Varg[] args) {

JFrame f = new JFrame(“Display Colours”);

f.getContentPane().shtoj(new Main());

f.setSize(300, 300);

f.setLocation(100, 100);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

f.setVisible(i vërtetë);

}

}

The above code sample will produce the following result.

Displays all the colours in a frame.








What is threading in java?

Një: A Java program can contain many threads, all of which may be created without the explicit knowledge of the developer. For now, all you need to consider is that when you write a Java application, there is an initial thread that begins its operation by executing the main() method of your application. When you write a Java applet, ekziston një fije që po ekzekuton metodat e rikthimit,,en,INIT,,en,actionPerformed,,en,e appletit tuaj,,en,ne flasim për këtë fije si fije e applets,,en,Në secilin rast,,en,programi juaj fillon me atë që mund ta konsideroni si një fije të vetme,,en,Nëse dëshironi të kryeni I / O,,en,veçanërisht nëse I / O mund të bllokojë,,en,filloni një orë me zile,,en,ose të bëjë ndonjë detyrë tjetër paralelisht me fije fillestare,,en,ju duhet të filloni një fije të re për të kryer këtë detyrë,,en,Si të shfaqet teksti në fonts të ndryshëm,,en,Shembulli i mëposhtëm tregon se si të shfaqet teksti në fontë të ndryshëm duke përdorur setFont,,en,metoda e klasës së shkronjave,,en,Klasa Font paraqet fontet,,en,të cilat përdoren për të dhënë tekstin në mënyrë të dukshme,,en,Një font siguron informacionin e nevojshëm për të përcaktuar sekuencat e karaktereve për sekuencat e glyphs dhe për të dhënë sekuenca të glyphs në objektet e Grafika dhe Komponenti,,en (init(), actionPerformed(), etc.) of your applet; we speak of this thread as the applet’s thread. In either case, your program starts with what you can consider as a single thread. If you want to perform I/O (particularly if the I/O might block), start a timer, or do any other task in parallel with the initial thread, you must start a new thread to perform that task.

How to display text in different fonts?

Një: Following example demonstrates how to display text in different fonts using setFont () method of Font class. The Font class represents fonts, which are used to render text in a visible way. A font provides the information needed to map sequences of characters to sequences of glyphs and to render sequences of glyphs on Graphics and Component objects.

java.awt importit. *;

import java.awt.event. *,,en,klasa publike kryesore shtrihet JPanel,,en,shrift me dhëmbëza,,en,SansSerif,,en,stilet =,,en,Font.PLAIN,,en,Font.ITALIC,,en,Font.BOLD,,en,stylenames =,,nl,qartë,,en,kursiv,,en,guximtar,,en,int f =,,en,type.length,,en,f ,,en,int s =,,en,styles.length,,en,s ,,en,Font font = font i ri,,en,stilet,,en,g.setFont,,en,burim,,en,Emri i String = type,,en,zonjat e stilit,,nl,f.setContentPane,,en,Emrat e ndryshëm të shkronjave shfaqen në një kornizë,,en,Si të vizatoni një linjë duke përdorur GUI,,en,Shembulli i mëposhtëm tregon se si të vizatoni një vijë duke përdorur barazimin,,en,metodë e klasës Graphics2D me objektin Line2D si një argument,,en,SetPaint,,en,metoda e klasës Graphics2D është përdorur për të pikturuar vijën,,en,Këtu applet java përdoret për të shfaqur vijën në një JFrame,,en,import java.awt.geom.Line2D,,en,import javax.swing.Japper,,en,klasa publike kryesore shtrihet JApplet,,en,RenderingHints.KEY_ANTIALIASING,,en,g2.setPaint,,en,int x =,,en,int y =,,es,g2.draw,,en,i ri Line2D.Double,,en,g2.drawString,,en,Linjë,,en,String s,,en,Aplikacioni i JApplet = i ri Kryesor,,en,qendër,,en,applet,,en

importojë javax.swing. *

public class Main extends JPanel {

Varg[] type = { “Serif”,”SansSerif”};

int[] styles = { Font.PLAIN, Font.ITALIC, Font.BOLD,

Font.ITALIC + Font.BOLD };

Varg[] stylenames =

{ “Plain”, “Italic”, “Bold”, “Bold & Italic” };

public void paint(Graphics g) {

për (int f = 0; f < type.length; f ) {

për (int s = 0; s < styles.length; s ) {

Font font = new Font(type[f], styles[s], 18);

g.setFont(font);

String name = type[f] + ” ” + stylenames[s];

g.drawString(emër, 20, (f * 4 + s + 1) * 20);

}

}

}

publike statike kryesore void(Varg[] një) {

JFrame f = new JFrame();

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

);

f.setContentPane(new Main());

f.setSize(400,400);

f.setVisible(i vërtetë);

}

}

The above code sample will produce the following result.

Different font names are displayed in a frame.

How to draw a line using GUI?

Një: Following example demonstrates how to draw a line using draw() method of Graphics2D class with Line2D object as an argument. The setPaint () method of the Graphics2D class has been used to paint the line. Here java applet is used to display the line on a JFrame.

java.awt importit. *;

import java.awt.event. *,,en,klasa publike kryesore shtrihet JPanel,,en,shrift me dhëmbëza,,en,SansSerif,,en,stilet =,,en,Font.PLAIN,,en,Font.ITALIC,,en,Font.BOLD,,en,stylenames =,,nl,qartë,,en,kursiv,,en,guximtar,,en,int f =,,en,type.length,,en,f ,,en,int s =,,en,styles.length,,en,s ,,en,Font font = font i ri,,en,stilet,,en,g.setFont,,en,burim,,en,Emri i String = type,,en,zonjat e stilit,,nl,f.setContentPane,,en,Emrat e ndryshëm të shkronjave shfaqen në një kornizë,,en,Si të vizatoni një linjë duke përdorur GUI,,en,Shembulli i mëposhtëm tregon se si të vizatoni një vijë duke përdorur barazimin,,en,metodë e klasës Graphics2D me objektin Line2D si një argument,,en,SetPaint,,en,metoda e klasës Graphics2D është përdorur për të pikturuar vijën,,en,Këtu applet java përdoret për të shfaqur vijën në një JFrame,,en,import java.awt.geom.Line2D,,en,import javax.swing.Japper,,en,klasa publike kryesore shtrihet JApplet,,en,RenderingHints.KEY_ANTIALIASING,,en,g2.setPaint,,en,int x =,,en,int y =,,es,g2.draw,,en,i ri Line2D.Double,,en,g2.drawString,,en,Linjë,,en,String s,,en,Aplikacioni i JApplet = i ri Kryesor,,en,qendër,,en,applet,,en;

import java.awt.geom.Line2D;

import javax.swing.JApplet;

import javax.swing.JFrame;

public class Main extends JApplet {

public void init() {

setBackground(Color.white);

setForeground(Color.white);

}

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(Color.gray);

int x = 5;

int y = 7;

g2.draw(new Line2D.Double(x, y, 200, 200));

g2.drawString(“Line”, x, 250);

}

publike statike kryesore void(String s[]) {

JFrame f = new JFrame(“Line”);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

JApplet applet = new Main();

f.getContentPane().shtoj(“Center”, applet);

applet.init,,en,f.pack,,en,Linja shfaqet në një kornizë,,en,Si të shfaqet një mesazh në një kornizë të re,,en,Shembulli i mëposhtëm tregon se si të shfaqet mesazhi në një kornizë të re duke krijuar një kornizë duke përdorur JFrame,,en,duke përdorur JFrames getContentPanel,,en,metoda për ta shfaqur këtë në kornizë,,en,Këtu klasat Graphics2D dhe Rectangle2D janë bërë për të bërë komponentin fillestar,,en,Dhe pastaj mesazhi shfaqet në një komponent JPanel,,en,import java.awt.font.FontRenderContext,,en,import javax.swing.JPanel,,en,g2.setFont,,en,font i ri,,en,Pantaorisontllyrcenteredtecsht,,hi,g2,,en,Java Burimi,,en,mbrojtur me bojë të zbrazëtHorizontallyCenteredText,,en,Graphics2D g2,,en,noton qendërX,,en,notoj bazenY,,en,FontRenderContext frc = g2.getFontRenderContext,,en,Rectangle2D kufijtë = g2.getFont,,en,getStringBounds,,en,FRC,,en,gjerësia e notave =,,en,bounds.getWidth,,en,centerX,,en,baselineY,,en,JAVA dhe J2EE shfaqen në një kornizë të re,,en,Si të shfaqet një tabelë me byrek duke përdorur një kornizë,,en();

f.pack();

f.setSize(Dimensioni i ri(300, 300));

f.setVisible(i vërtetë);

}

}

The above code sample will produce the following result.

Line is displayed in a frame.

How to display a message in a new frame?

Një: Following example demonstrates how to display message in a new frame by creating a frame using JFrame() & using JFrames getContentPanel(), setSize() & setVisible() methods to display this on the frame. Here Graphics2D and Rectangle2D classes have been ued to make the initial component. And then the message is displayed on a JPanel component.

java.awt importit. *;

import java.awt.font.FontRenderContext;

Shuma java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Main extends JPanel {

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setFont(new Font(“Serif”, Font.PLAIN, 48));

paintHorizontallyCenteredText(g2, “Java Source”, 200, 75);

paintHorizontallyCenteredText(g2, “dhe”, 200, 125);

paintHorizontallyCenteredText(g2, “Support”, 200, 175);

}

protected void paintHorizontallyCenteredText(Graphics2D g2,

String s, float centerX, float baselineY) {

FontRenderContext frc = g2.getFontRenderContext();

Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);

float width = (float) bounds.getWidth();

g2.drawString(s, centerX – width / 2, baselineY);

}

publike statike kryesore void(Varg[] args) {

JFrame f = new JFrame();

f.getContentPane().shtoj(new Main());

f.setSize(450, 350);

f.setVisible(i vërtetë);

}

}

The above code sample will produce the following result.

JAVA and J2EE displayed in a new Frame.







How to display a pie chart using a frame?

Një: Shembulli i mëposhtëm tregon se si të shfaqësh një piechart duke bërë klasë Slices,,en,duke krijuar hark në varësi të feta,,en,Në këtë shembull përdoren komponentët AWT dhe swing për të nxjerrë tabelën,,en,Argumentet kalohen në drawPie,,en,metodë nga kryesore,,en,Dhe grafiku i kompletuar shfaqet në një JFrame,,en,import java.awt.Rectangle,,en,klasë Slice,,en,vlerë të dyfishtë,,en,Ngjyra e ngjyrave,,en,fetë publike,,en,this.value = value,,en,kjo color = color,,en,fetë,,en,feta =,,en,fetë e re,,en,Color.black,,en,Color.green,,en,MyComponent,,en,drawPie,,pl,getBounds,,en,feta,,en,prapavi i pjesshëm,,en,Graphics2D g,,en,Zona e rreshtave,,en,total i dyfishtë = 0.0D,,en,slices.length,,en,total = feta,,en,double curValue = 0.0D,,en,int startAngle =,,en,startAngle =,,sv,curValue,,en,int arcAngle =,,en,g.fillArc,,en,area.x,,en,area.y,,en,area.width,,en,area.height,,en,kënd filluar,,sv,arcAngle,,es,curValue = feta,,en,argv,,en,frame.getContentPane,,en,Shfaq një piechart në një kornizë,,en & creating arc depending on the slices. In this example AWT and swing components are used to draw the chart. Arguments are passed to the drawPie () method from the main () metodë. And the completed chart is displayed on a JFrame.

import java.awt.Color;

import java.awt.Graphics,,en,importi java.awt.Graphics2D,,en,import java.awt.RenderingHints,,en,import javax.swing.JComponent,,en,JFrame frame = JFrame e re,,en,frame.add,,en,ri MyComponent,,en,frame.setSize,,en,frame.setVisible,,en,class MyComponent shtrihet JComponent,,en,Graphics2D g2 =,,en,Graphics2D,,en,RenderingHints rh = g2d.getRenderingHints,,de,boolean bl = rh.containsValue,,en,RenderingHints.VALUE_ANTIALIAS_ON,,en,me,,mt,g2.setRenderingHint,,ku,RenderingHints,,en,KEY_ANTIALIASING,,en,I rremë,,en,Si të shfaqni ngjyrat në një kornizë,,en,Shembulli i mëposhtëm tregon se si të shfaqni të gjitha ngjyrat në një kornizë duke përdorur metodën setRGB të klasës së imazhit,,en,Imazhi abstrakt i klasës është superklasë e të gjitha klasave që përfaqësojnë imazhe grafike,,en,Imazhi duhet të merret në një mënyrë specifike për platformën,,en,import java.awt.event.WindowAdapter,,en,import java.awt.event.WindowEvent,,en,import java.awt.image.BufferedImage,,en,klasa publike kryesore shtrihet JComponent,,en,Imazhi BufferedImage,,en;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import javax.swing.JComponent;

import javax.swing.JFrame;

class Slice {

double value;

Color color;

public Slice(double value, Color color) {

this.value = value;

this.color = color;

}

}

class MyComponent extends JComponent {

Slice[] slices = { new Slice(5, Color.black),

new Slice(33, Color.green),

new Slice(20, Color.yellow), new Slice(15, Color.red) };

MyComponent() {}

public void paint(Graphics g) {

drawPie((Graphics2D) g, getBounds(), slices);

}

void drawPie(Graphics2D g, Rectangle area, Slice[] slices) {

double total = 0.0D;

për (int i = 0; unë < slices.length; i ) {

total = slices[unë].vlerë;

}

double curValue = 0.0D;

int startAngle = 0;

për (int i = 0; unë < slices.length; i ) {

startAngle = (int) (curValue * 360 / total);

int arcAngle = (int) (slices[unë].vlerë * 360 / total);

g.setColor(slices[unë].color);

g.fillArc(area.x, area.y, area.width, area.height,

startAngle, arcAngle);

curValue = slices[unë].vlerë;

}

}

}

publik klasë Main {

publike statike kryesore void(Varg[] argv) {

JFrame frame = new JFrame();

frame.getContentPane().shtoj(new MyComponent());

frame.setSize(300, 200);

frame.setVisible(i vërtetë);

}

}

The above code sample will produce the following result.

Displays a piechart on a frame.

 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share