第一个索引设置为null(空),但它不打印正确的输出,为什么?
//set the first index as null and the rest as "High"
String a []= {null,"High","High","High","High","High"};
//add array to arraylist
ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a));
for(int i=0; i<choice.size(); i++){
if(i==0){
if(choice.get(0).equals(null))
System.out.println("I am empty"); //it doesn't print this output
}
}
Run Code Online (Sandbox Code Playgroud) 如何在JPANEL上放置图像背景?
JPanel pDraw = new JPanel(new GridLayout(ROWS,COLS,2,2));
pDraw.setPreferredSize(new Dimension(600,600)); //size of the JPanel
pDraw.setBackground(Color.RED); //How can I change the background from red color to image?
Run Code Online (Sandbox Code Playgroud) 我用线画一个三角形.我怎样才能填上它的颜色?到目前为止,我只能成功地为线条着色但不填充颜色.
public void paintComponent(Graphics g){
super.paintComponents(g);
int k=0;
for (j=0 ; j < numOfLines; j++){ // the values of numOfLines retrieved from other method.
g.setColor(Color.green);
g.drawLine(x[k], x[k+1], x[k+2], x[k+3]);
k = k+4; //index files
}
Run Code Online (Sandbox Code Playgroud) 我有3个txt文件s1.txt, s2.txt, s3.txt.每个都有相同的格式和数量的数据.我想只将3个文件中每个文件的第二列合并为一个文件.在我合并数据之前,我根据第1列对其进行了排序:
UnSorted文件:s1.txt s2.txt s3.txt
1 23 2 33 3 22
4 32 4 32 2 11
5 22 1 10 5 28
2 55 8 11 7 11
Run Code Online (Sandbox Code Playgroud)
排序文件:s1.txt s2.txt s3.txt
1 23 1 10 2 11
2 55 2 33 3 22
4 32 4 32 5 28
5 22 8 11 7 11
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的代码:
BaseFile ='s'
n=3
fid=fopen('RT.txt','w');
for i=1:n
%Open each file consecutively
d(i)=fopen([BaseFile num2str(i)'.txt']);
%read data from file
A=textscan(d(i),'%f%f')
a=A{1}
b=A{2}
ab=[a,b]; …Run Code Online (Sandbox Code Playgroud) 我有三个不同的图像(jpeg或bmp).我试图根据每个图像的颜色数来预测每个图像的复杂程度.我怎么能用Java实现它呢?谢谢.
更新: 这些代码不起作用..输出显示1312种颜色,即使它只是纯红色和白色
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class clutters {
public static void main(String[] args) throws IOException {
ArrayList<Color> colors = new ArrayList<Color>();
BufferedImage image = ImageIO.read(new File("1L.jpg"));
int w = image.getWidth();
int h = image.getHeight();
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int pixel = image.getRGB(x, y);
int red = (pixel & 0x00ff0000) >> 16;
int green = (pixel & 0x0000ff00) …Run Code Online (Sandbox Code Playgroud) 对txt文件中的数据进行了一些修改.我已经尝试了建议的代码,但我没有成功地用这种格式在txt文件中再次写它.我尝试了collection.sort但是它用长线写了数据.
我的txt文件包含以下数据:
Monday
Jessica Run 20mins
Alba Walk 20mins
Amy Jogging 40mins
Bobby Run 10mins
Tuesday
Mess Run 20mins
Alba Walk 20mins
Christy Jogging 40mins
Bobby Run 10mins
Run Code Online (Sandbox Code Playgroud)
如何按升序对这些数据进行排序,并在排序后将其再次存储在txt文件中?
Monday
Alba Walk 20mins
Amy Jogging 40mins
Bobby Run 10mins
Jessica Run 20mins
Tuesday
Alba Walk 20mins
Bobby Run 10 mins
Christy Jogging 40mins
Mess Run 20mins
Jessica Run 20mins
Run Code Online (Sandbox Code Playgroud) String a []= {null,null,null,null,null};
//add array to arraylist
ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a));
System.out.println(choice.size());
Run Code Online (Sandbox Code Playgroud)
choice当所有元素都设置为时,为什么arrayList的大小为5null
如何告诉paint方法仅在JPanel上绘制背景而不是在整个JFrame上绘制背景.我的JFrame大小比JPanel大.当我尝试为JPanel绘制网格背景时,网格似乎遍布JFrame而不仅仅是JPanel.
这部分代码:
public class Drawing extends JFrame {
JPanel drawingPanel;
...........
public Drawing (){
drawingPanel = new JPanel();
drawingPanel.setPreferredSize(new Dimension(600,600));
}
public void paint(Graphics g)
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
paintBackground(g2); //call a METHOD to paint the for JPANEL
}
private void paintBackground(Graphics2D g2)
{
g2.setPaint(Color.GRAY);
for (int i = 0; i < drawingPanel.getSize().width; i += 300)
{
Shape line = new Line2D.Float(i, 0, i, drawingPanel.getSize().height);
g2.draw(line);
}
for (int i = 0; i < drawingPanel.getSize().height; …Run Code Online (Sandbox Code Playgroud) 这个问题与之前的帖子有关. 如何保存文件和阅读
alt text http://freeimagehosting.net/image.php?dc73c3bb33.jpg
只有当鼠标指向不是Null(包含图像)的网格时,如何才能将光标更改为"Hand"?
到目前为止,光标在整个网格上变为"手"(null或非null).
public GUI() {
....
JPanel pDraw = new JPanel();
....
for(Component component: pDraw.getComponents()){
JLabel lbl = (JLabel)component;
//add mouse listener to grid box which contained image
if (lbl.getIcon() != null)
lbl.addMouseListener(this);
}
public void mouseEntered(MouseEvent e) {
Cursor cursor = Cursor.getDefaultCursor();
//change cursor appearance to HAND_CURSOR when the mouse pointed on images
cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
setCursor(cursor);
}
Run Code Online (Sandbox Code Playgroud) 我有一个2乘3的矩阵,我想根据第一列对它进行排序.这是一个例子:
data will change to --> new data
11 33 10 22
22 44 11 33
10 22 22 44
Run Code Online (Sandbox Code Playgroud)
我有这个代码用于排序矩阵,A但它不能很好地工作:
sort(A,1,'ascend');
Run Code Online (Sandbox Code Playgroud)