static Icon getColorIcon (java.awt.Color color) { BufferedImage image = new BufferedImage(32, 16, BufferedImage.TYPE_INT_RGB); java.awt.Graphics g = image.getGraphics(); g.setColor(color); g.fillRect(1, 1, 30, 14); g.setColor(java.awt.Color.black); g.drawRect(0, 0, 31, 15); return new ImageIcon(image); }
+ "The effect we want is a multi-line label."; JFrame f = new JFrame("Label Render Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Color.orange); imageGraphics.setPaint(gp); imageGraphics.fillRect(0, 0, 400, 300); BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); g.setColor(new Color(255, 255, 255, 128)); g.fillRoundRect( 0, 15, 10); g.setColor(Color.black); textLabel.paint(g); Graphics g2 = image.getGraphics(); JLabel imageLabel = new JLabel(ii); f.getContentPane().add(imageLabel); f.pack(); f.setLocationByPlatform(true); f.setVisible(true);
constraints.fill = GridBagConstraints.BOTH; JLabel l = new JLabel("You have got 2 new Messages."); panel.add(l, constraints); constraints.gridx++; constraints.weightx = 0f; b.setMargin(new Insets(1, 4, 1, 4)); b.setFocusable(false); panel.add(b, constraints); dialog.setUndecorated(true); dialog.setSize(300, 100); final Graphics2D g2d = (Graphics2D) g; g2d.setPaint(lpg); g2d.fillRect(1, 1, getWidth() - 2, getHeight() - 2); g2d.setColor(Color.BLACK); g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); JPanel panel = new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100); } }; frame.add(panel); // Graphics g = panel.getGraphics(); // g.setColor(Color.BLUE); // g.fillRect(0, 0, 100, 100); frame.validate(); // because you added panel after setVisible was called frame.repaint(); // because you added panel after setVisible was called } }
static Icon getColorIcon (java.awt.Color color) { BufferedImage image = new BufferedImage(32, 16, BufferedImage.TYPE_INT_RGB); java.awt.Graphics g = image.getGraphics(); g.setColor(color); g.fillRect(1, 1, 30, 14); g.setColor(java.awt.Color.black); g.drawRect(0, 0, 31, 15); return new ImageIcon(image); }
gui.add(l1); l2.setBackground(Color.YELLOW); l2.setOpaque(true); gui.add(l2); p1.add(new JLabel("Panel 1")); p1.setBorder(brdrRight); p1.setOpaque(false); borderRegion.subtract(area); g2.setClip(borderRegion); g2.setColor(bg); g2.fillRect(0, 0, width, height); g2.setClip(null); g2.setColor(color); g2.setStroke(stroke); g2.draw(area);
JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(600, 400); frame.add(panel); public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(0, 0, 100, 100);
/** * Overrides the painting of the bar's track (the darker part underneath that extends * the full page length). */ @Override protected void paintTrack(Graphics graphics, JComponent jComponent, Rectangle rectangle) { graphics.setColor(trackColor); graphics.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); }
public static void main(String args[]) { FrameTestBase t = new FrameTestBase(); t.add(new JComponent() { public void paintComponent(Graphics g) { String str = "hello world!"; Rectangle2D rect = fm.getStringBounds(str, g); g.setColor(bgColor); g.fillRect(x, y - fm.getAscent(), (int) rect.getWidth(), (int) rect.getHeight()); g.setColor(textColor); g.drawString(str, x, y); t.setDefaultCloseOperation(EXIT_ON_CLOSE); t.setSize(400, 200); t.setVisible(true);
/** * Overrides the painting of the bar's thumb (the lighter part on top that users * use to slide up and down the page). */ @Override protected void paintThumb(Graphics graphics, JComponent jComponent, Rectangle rectangle) { graphics.setColor(thumbColor); graphics.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); }
gui.add(quality, BorderLayout.WEST); dithering = new JCheckBox("Dithering", false); controls.add(antialiasing); controls.add(fractionalMetrics); (float)width, (float)height, Color.orange); g2d.setPaint(gp); g2d.fillRect(0,0, width, height); g2d.setColor(Color.blue); for (int ii=0; ii<width; ii+=10) { g2d.drawLine(ii, 0, ii, height); g2d.setColor(Color.green); for (int jj=0; jj<height; jj+=10) { g2d.drawLine(0, jj, width, jj); g2dText.setColor(Color.black); g2dText.drawString("The quick brown fox jumped over the lazy dog.", 10,50);
final JFrame frame = new JFrame("Nested Layout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); plafComponents.add(plafChooser); plafComponents.add(pack); gui.add(plafComponents, BorderLayout.NORTH); 20f,20f,Color.red, 180f,180f,Color.yellow); g.setPaint(gp); g.fillRect(0,0,200,200); ImageIcon ii = new ImageIcon(bi); JLabel imageLabel = new JLabel(ii); frame.setVisible(true);
g2.setColor(Color.WHITE); g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding); g2.setColor(Color.BLACK); int y1 = y0; if (scores.size() > 0) { g2.setColor(gridColor); g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1); g2.setColor(Color.BLACK); JFrame frame = new JFrame("DrawGraph"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
int cellX = 10 + (fillCell.x * 10); int cellY = 10 + (fillCell.y * 10); g.setColor(Color.RED); g.fillRect(cellX, cellY, 10, 10); g.setColor(Color.BLACK); g.drawRect(10, 10, 800, 500); JFrame window = new JFrame(); window.setSize(840, 560); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(grid); window.setVisible(true); grid.fillCell(0, 0); grid.fillCell(79, 0);