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);
JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new Balls()); g.setColor(color); g.fillOval(0, 0, 30, 30); //adds color to circle g.setColor(Color.black); g2.drawOval(0, 0, 30, 30); //draws circle
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);
g2.setColor(GRAPH_COLOR); g2.setStroke(GRAPH_STROKE); for (int i = 0; i < graphPoints.size() - 1; i++) { g2.setColor(GRAPH_POINT_COLOR); for (int i = 0; i < graphPoints.size(); i++) { int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2; JFrame frame = new JFrame("DrawGraph"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true);
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("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); String text = "This is a test xyx"; g.setColor(Color.RED); g.drawLine(0, height / 2, getWidth(), height / 2); g.setColor(Color.BLACK);
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 } }
/** * 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); }
int diameter = originalSize.height - (gap * 2); g.setColor(circleColor); g.fillOval(x, y, diameter, diameter); MyButton button = new MyButton("Hello, World!"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); contentPane.add(button); frame.setVisible(true);
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);
g.setColor(Color.WHITE); g.setFont(new Font("serif", Font.BOLD, 60)); g.drawString(s, getWidth() / 2 - g.getFontMetrics().stringWidth(s) / 2, JFrame f = new JFrame(); f.setSize(500, 500); f.setTitle("Sometimes Red, Sometimes Blue"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new TestPanel()); f.setVisible(true);
RenderingHints.VALUE_ANTIALIAS_ON); g2.setStroke(STROKE); g.setColor(POINTS_COLOR); for (List<Point> pointList : pointsList) { if (pointList.size() > 1) { g.setColor(CURRENT_POINTS_COLOR); if (currentPointList != null && currentPointList.size() > 1) { Point p1 = currentPointList.get(0); JFrame frame = new JFrame("DrawStuff"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawStuff()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);