Draw triangle in JPanel

Complete source code below will show you, how to draw triangle in JPanel

*********************************************************************
COMPLETE SOURCE CODE FOR : DrawTriangle.java
*********************************************************************


import javax.swing.*;

import java.awt.*;

public class DrawTriangle extends JPanel
{
public DrawTriangle()
{
JFrame frame=new JFrame("Draw triangle in JPanel");
frame.add(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//All triangle corner x coordinate
int[]x={0,150,300};

//All triangle corner y coordinate
int[]y={200,0,200};

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=192
//B=0
//So after this all you draw will use this color
g.setColor(new Color(255,192,0));

//Draw triangle in JPanel
g.fillPolygon(x,y,3);

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=1
//G=1
//B=1
//So after this all you draw will use this color
g.setColor(new Color(1,1,1));

//Set font that will use when draw String
g.setFont(new Font("Arial",Font.BOLD,14));

//Draw String in JPanel
g.drawString("(0,200)",10,200);
g.drawString("(150,0)",150,20);
g.drawString("(300,200)",290,200);
}

public static void main(String[]args)
{
DrawTriangle dt=new DrawTriangle();
}

}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Compare current date with user input date in java

Complete source code below will show you, how to compare current date with user input date in java.

***************************************************************************
COMPLETE SOURCE CODE FOR : GetDate.java
***************************************************************************


import java.util.Date;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GetDate implements ActionListener
{
int year=0;
int month=0;
int date=0;

String [] yearContent={"2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011"};
String [] monthContent={"January","February","March","April","Mei","Jun","July","August","September","October","November","December"};
String [] dateContent={"1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31"};

JComboBox yearComboBox=new JComboBox(yearContent);
JComboBox monthComboBox=new JComboBox(monthContent);
JComboBox dateComboBox=new JComboBox(dateContent);

JButton button=new JButton("Click here to compare to current date");

JFrame frame=new JFrame("Compare date with current date");

public GetDate()
{
button.addActionListener(this);

Box box=Box.createHorizontalBox();
box.add(dateComboBox);
box.add(monthComboBox);
box.add(yearComboBox);

frame.setLayout(new GridLayout(2,1));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(box);
frame.add(button);
frame.setSize(500,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
String tempDate=(String)dateComboBox.getSelectedItem();
date=Integer.parseInt(tempDate);

month=monthComboBox.getSelectedIndex();

String tempYear=(String)yearComboBox.getSelectedItem();
year=Integer.parseInt(tempYear)-1900;

Date currentDateValue=new Date();
Date userChooserDate=new Date(year,month,date);

int currentYear=currentDateValue.getYear();
int currentMonth=currentDateValue.getMonth();
int currentDate=currentDateValue.getDate();

if((date==currentDate)&&(month==currentMonth)&&(year==currentYear))
{
JOptionPane.showMessageDialog(frame,"Same date","SAME",JOptionPane.INFORMATION_MESSAGE);
}
else if(userChooserDate.compareTo(currentDateValue)<0)>0)
{
JOptionPane.showMessageDialog(frame,"Welcome to the future","FUTURE",JOptionPane.INFORMATION_MESSAGE);
}
}
}

public static void main(String[]args)
{
GetDate gd=new GetDate();
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You